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) == ...

Get Advanced UNIX Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.