Tuesday, March 11, 2008

[Programming] Processes (3) - Signals

A Signal is a special message sent to a process to respond to some conditions. Some examples of the conditions are: illegal instructions, broken pipe, termination etc. Those signals are defined in the header file - singal.h. They are all with prefix SIG. The following table is a list of some signals.

Signal NameDescription
SIGILLIllegal instruction, indicates the program's stack is corrupted
SIGINTTerminal interrupt from keyboard by pressing Ctrl+C
SIGKILLEnds a process immediately, which cannot be caught or ignored
SIGPIPEAttempts to write on a broken pipe
SIGQUITQuit from keyboard
SIGTERMRequests a process to terminate


For a full list of signals, you can find it by typing the command below:

% man 7 signal

Generally speaking, signals can be used in the next three situations:
1) send a signal to processes in response to some errors, such as SIGBUS(bus error), SIGSEGV(segmentation violation)
2) send a signal to another process, such as SIGTERM(terminate), SIGKILL(kill the process)
3) send a command to a running program using user-defined signals, such as SIGUSR1

When a process receives a signal, it will take some actions immediately - either to call another function or ignore it (ignore is also a kind of action). When it calls another function, the current executing program will be paused and the new function will be executed. When the function returns, the paused program will resume again.

You can aslo set or modify the actions to be taken on receipt of signals by using function sigaction().

No comments: