Next: Times Up!! Up: Interrupts and Signals Previous: Sending Signals - kill()

Receiving signals - signal()

int (*signal(int sig, void (*func)()))() - that is to say the function signal() will call the func functions if the process receives a signal sig. Signal returns a pointer to function func if successful or it returns an error to errno and -1 otherwise.

func() can have three values:

- a pointer to a system default function SID_DFL(), which will terminate the process upon receipt of sig.
- a pointer to system ignore function SIG_IGN() which will disregard the sig action (UNLESS it is SIGKILL).
A function address
- a user specified function.

SIG_DFLand SIG_IGN are defined in signal.h (standard library) header file.

Thus to ignore a ctrl-c command from the command line. we could do:

signal(SIGINT, SIG_IGN);

TO reset system so that SIGINT causes a termination at any place in our program, we would do:

signal(SIGINT, SIG_DFL);

So lets write a program to trap a ctrl-c but not quit on this signal. We have a function sigproc() that is executed when we trap a ctrl-c. We will also set another function to quit the program if it traps the SIGQUIT signal so we can terminate our program:

Finally lets write a program that communicates between child and parent processes using kill() and signal().

fork() creates the child process from the parent. The pid can be checked to decide whether it is the child (== 0) or the parent (pid = child process id).

The parent can then send messages to child using the pid and kill().

The child picks up these signals with signal() and calls appropriate functions.

An example of communicating process using signals is sig_talk.c in Appendix .
Fri May 20 13:40:49 BST 1994