System Interface Guide

Installing a Handler

The functions signal(3C), sigset(3C), signal(3UCB), and sigvec(3UCB) can all be used to install a signal handler. All return the previous action for the signal. There is one major difference between the four interfaces: signal(3C) results in System V signal semantics (the same signal can interrupt its handler); sigset(3C), signal(3UCB), and sigvec(3UCB) all result in BSD signal semantics (the signal is blocked until its handler returns). In addition, both signal(3C) and signal(3UCB) can flag that the signal is to be ignored or that the default action be restored. Example 4-1 illustrates a simple handler and its installation.


Example 4-1 Signal handler installation

#include <stdio.h>
#include <signal.h>
#define TRUE 1

void sigcatcher()
{
		printf ("PID %d caught signal.\n", getpid());
}

main()
{
		pid_t ppid;

		signal (SIGINT, sigcatcher);
		if (fork() == 0) {
			sleep( 5 );
			ppid = getppid();
			while( TRUE )
				if (kill( ppid, SIGINT) == -1 )
					exit( 1 );
		}
		pause();
}

Trying to install a handler or set SIG_IGN for the signals SIGKILL or SIGSTOP results in an error. Trying to set SIG_IGN for the signal SIGCONT also results in an error, since it is ignored by default.

After a signal handler is installed, it remains so until it is explicitly replaced by another call to signal(3C), sigset(3C), signal(3UCB), or sigvec(3UCB).