Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Signal Handling 2

Status
Not open for further replies.

SwapSawe

Programmer
Apr 24, 2001
148
0
0
US
Dear All,

I am required to send a signal from one program to another program so as to execute the specified function. I am familiar with raise() and signal() ie. what they do. But I am unable to use them. Can anyone be kind enough to give me a sample source code.

Thanx,
SwapSawe.
 
Set up the signal handler in the process that wants to catch the signal:

#include <signal.h>

/* ... */

signal(SIGUSR1,handler);

In the process that sends the signal, call raise():

#include <signal.h>

if (raise(SIGUSR1)!=0) {
/* failure */

/* ... */

SIGUSR1 might not be available for your system though, in which case you'll obviously have to use a different signal.

Russ
bobbitts@hotmail.com
 
There is also another way of sending signals .

Set up the signal handler in the process that wants to catch the signal:

#include <signal.h>

/* ... */

signal(SIGUSR1,handler);

The handler fucntion will be called as soon as the signal SIGUSR1 is invoked .

In the process that sends the signal, call kill();
This takes 2 parameters .
#include <signal.h>

kill(PID,SIGNAL-TO-BE-SENT);
eg: kill(pid,SIGUSR1);

regards
sramki
 
Right (what was I thinking), of course if you're sending a signal to another process you'll need kill() as raise() will only send a signal to the process that calls raise().

Russ
bobbitts@hotmail.com
 
kill would be a better choice than raise .

more efficient and reliable also

regards
ramki
 
It is possible to send a signal to a specified process group using the kill system call .
where as raise sends a signal to the current process
only .

regards
ramki
 
The signals can be sent to the program through the system calls, keyboard and through command line. We can use the raise() and kill() system calls to send a signal, Ctrl C is an example for sending signal through keyboard and kill command is used to send signals form command line itself to a specific process.

Program for Handling the Ctrl C.

#include <signal.h>
#include <sys/types.h>
#include <unistd.h>

void Handle_function()
{
...
printf(&quot;Ctrl Pressed...&quot;);
...
}

int main()
{
...
/* Handler to catch the INT ( Ctrl C)
signal(SIGINT, Handle_function);
...
}


Now the signal (Ctrl C) can be send to the program by pressing Ctrl C, or by giving the command kill -INT {process ID of the above program } or by calling the kill or raise system call from another program.

If we want to send a signal to a another program then the process Id of that program is required

kill( {process Id of the program [ signal handler ] }, SIGINT);

Regards
Maniraja S
 
Maybe this is a misunderstanding of language. raise() is just as efficient and reliable as kill() given that your intent is to send a signal() to the same process that you call raise() in. In fact one could argue that raise() is even more efficient and reliable than kill() in this context because it doesn't require the programmer to call getpid() to get its process id. raise() is equivalent to calling kill(getpid(),SIGNAL_TO_SEND)

You might say kill() is more /versatile/ than raise() because you're not limited to sending signals just to the calling process, and I suspect this is what you meant after seeing your explanation.

Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top