tags:

views:

1155

answers:

4

i am using message queue as an ipc between 2 programs. Now i want to send data from one program to another using message queue and then intimate it through a signal SIGINT.

I dont know how to send a signal from one program to another . Can anybody pls provide a sample code if they have the solution.

+2  A: 

Signal in linux can be send using kill system call just check this link for documentation of kill system call and example. you can see man -2 kill also. and it's not advisable to use SIGINT use SIGUSR1 or SIGUSR2

serioys sam
+6  A: 
#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig);
vartec
how to get the pid of other process in this program.
Chaithra
there are a number of ways to do this. If one is the parent of the other, the parent gets the child's pid when it calls fork(), and the child can get the parent's with getppid(). If this is not the case, you can put the pid in an agreed location. For instance, a file that one writes for the other.
Nathan Fellman
yes. ok. but if it is some other program..? then how to send the processid to that program.. can we send using a socket ?
Chaithra
Typically, the other process would have to write it's PID to a file for parsing. But, if the 2 programs share some relationship (parent / child, or they are threads in the same executable) then the PID will be known at creation time.
slacy
A: 
system("kill -2 `pidof <app_name_here>` ");
teriz
This is a *very* messy way to send a signal from one process to another. How about using the kill() or sigqueue() system calls?
slacy
+1  A: 

Note that by using the sigqueue() system call, you can pass an extra piece of data along with your signal. Here's a brief quote from "man 2 sigqueue":

The value argument is used to specify an accompanying item of data (either an integer or a pointer value) to be sent with the signal, and has the following type:

     union sigval {
         int   sival_int;
         void *sival_ptr;
     };

This is a very convenient way to pass a small bit of information between 2 processes. I agree with the user above -- use SIGUSR1 or SIGUSR2 and a good sigval, and you can pass whatever you'd like.

You could also pass a pointer to some object in shared memory via the sival_ptr, and pass a larger object that way.

slacy