Signals are a simple but important means of interprocess communication. Interprocess communication sounds fancy, but it's really a simple concept: it's the means by which one program sends a message to another program. It's common to think of signals as special messages sent by the Unix kernel (Section 1.10) but, in fact, any program can signal any other program.
What kinds of messages can you send with a signal? Relatively few, in reality. Signals aren't "arbitrary" messages, like letters; they are a small group of pre-defined messages, each with its own special meaning. System V Unix supports 16 signals, each of which is assigned a number; BSD-derived Unix implementations and SVR4 have 32 signals. Table 24-2 lists some of the more commonly used signals. It also lists keyboard characters that send common signals on BSD systems (these can be changed; see Section 5.8).
Signal name |
Number |
Meaning and typical use |
---|---|---|
HUP |
1 |
Hangup -- stop running. Sent when you log out or disconnect a modem. |
INT |
2 |
Interrupt -- stop running. Sent when you type CTRL-c. |
QUIT |
3 |
Quit -- stop running (and dump core). Sent when you type CTRL-\. |
KILL |
9 |
Kill -- stop unconditionally and immediately; a good "emergency kill." |
SEGV |
11 |
Segmentation violation -- you have tried to access illegal memory. |
TERM |
15 |
Terminate -- terminate gracefully, if possible. |
STOP |
17* |
Stop unconditionally and immediately; continue with CONT. |
TSTP |
18* |
Stop -- stop executing, ready to continue (in either background or foreground). Sent when you type CTRL-z. stty (Section 5.8) calls this susp. |
CONT |
19* |
Continue -- continue executing after STOP or TSTP. |
CHLD |
20* |
Child -- a child process's status has changed. |
Note that signal numbers -- especially the ones above 15, marked with an asterisk in Table 24-2 -- vary system-to-system. Use the signal name wherever you can.
While the list in Table 24-2 isn't definitive, it shows you the types of things signals can do. Many signals, like SIGSEGV, are warning or error messages. You've probably seen the frustrating "segmentation violation" message. That message came when the kernel detected something wrong and sent your program a SIGSEGV signal; in response, your program quit. Others signals, like SIGTSTP, are generated in response to special characters on the keyboard. And a lot of signals just say, "Your time is up, goodbye!"
When a process receives a signal, it can take a number of actions; for example:
It can take whatever default action is specified for the signal. By default, some signals kill the process that receives them. For some signals, the default action is to stop running and dump core. (SIGQUIT is an example of this.) Other signals have no effect by default.
It can trap (Section 35.17) the signal and run a special "signal handling" function -- in which case, it can do whatever it wants. A signal handler often does whatever's necessary to shut the program down nicely: make sure that files are closed and left in a consistent state, and so on.
It can ignore the signal, in which case nothing happens.
You've probably read that the command kill -9 is guaranteed to kill a process. Why? Two special signals in Table 24-2 can't be caught or ignored: the KILL and STOP signals.
The kill (Section 24.12) command doesn't kill -- it really does nothing more than send signals. As you now know, signals often bring death and destruction, but there's no necessary reason for them to do so.
-- ML
Copyright © 2003 O'Reilly & Associates. All rights reserved.