Raising Signals

A UNIX signal can be raised from your application by the use of the kill(2) system call. Its synopsis is as follows:

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

int kill(pid_t pid, int sig);

This function raises the signal sig in the process pid (by process ID). You must have permission to raise the signal in the indicated process to succeed. To raise the signal SIGUSR1 in your own application, you can code:

kill(getpid(),SIGUSR1); /* Raise SIGUSR1 in current process */

The value 0 is returned for success and -1 if it fails (check errno).

The value of sig is permitted to be 0. When it is, kill(2) allows your process to detect if the process pid exists. For example

pid_t PID = 1234;    /* Process ID 1234 */

if ( kill(PID,0) == -1 ) {
    if ( errno == ESRCH )
        puts("Process 1234 is not executing.");
    else
        perror("kill(2)");
} else
    puts("Process 1234 is executing.");

When kill(2) returns success in the example, then the program has determined that process ID 1234 existed at the time of the test. The errno code ESRCH indicates that no process matching argument pid exists.

Note

ESRCH—No such process This error is returned by kill(2) when the process indicated does not exist.


The argument pid can be given as 0. When it is, all processes within your process group are signaled with the signal sig.

When kill(2) argument pid is -1, the signal is sent to all but system processes if the caller has super user privileges. When the caller does not have super user privileges, the signal is delivered to all processes with a real user ID that matches the caller's effective user ID. The calling process is not signaled, however.

Note

There is also a raise(3) function, which can be used to send a signal to the current process.

#include <signal.h>

int raise(int sig);

This function is implemented in terms of calls to getpid(2) and kill(2):

kill(getpid(),sig);

Since this is easily written, raise(3) is perhaps unnecessary.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.163.58