Signals
Signals are a simple UNIX mechanism for controlling processes. A signal is a 5-bit message to a process that requires immediate attention. Each signal has associated with it a default action; for some signals, you can change this default action. Signals are generated by exceptions, which include:
- Attempts to use illegal instructions
- Certain kinds of mathematical operations
- Window resize events
- Predefined alarms
- The user pressing an interrupt key on a terminal
- Another program using the kill() l or killpg() system calls
- A program running in the background attempting to read from or write to its controlling terminal
- A child process calling exit or terminating abnormally
The system default may be to ignore the signal, to terminate the process receiving the signal (and, optionally, generate a core file), or to suspend the process until it receives a continuation signal. Some signals can be caught - that is, a program can specify a particular function that should be run when the signal is received. By design, UNIX supports exactly 31 signals. They are listed in the files /usr/include/signal.h and /usr/include/sys/signal.h. Table 27.4 contains a summary.
Signal Name | Number[7] | Key | Meaning[8] |
---|---|---|---|
SIGHUP | Hangup (sent to a process when a modem or network connection is lost) | ||
SIGINT | Interrupt (generated by CTRL-C (Berkeley UNIX) or RUBOUT (System V). | ||
SIGQUIT | * | Quit | |
SIGILL | * | Illegal instruction | |
SIGTRAP | * | Trace trap | |
SIGIOT | * | I/O trap instruction; used on PDP-11 UNIX | |
SIGEMT | * | Emulator trap instruction; used on some computers without floating-point hardware support | |
SIGFPE | * | Floating-point exception | |
SIGKILL | ! | Kill | |
SIGBUS | * | Bus error (invalid memory reference, such as an attempt to read a full word on a half-word boundary) | |
SIGSEGV | * | Segmentation violation (invalid memory reference, such as an attempt to read outside a process's memory map) | |
SIGSYS | * | Bad argument to a system call | |
SIGPIPE | Write on a pipe that has no process to read it | ||
SIGALRM | Timer alarm | ||
SIGTERM | Software termination signal (default kill signal) | ||
SIGURG | @ | Urgent condition present | |
SIGSTOP | +! | Stop process | |
SIGTSTP | + | Stop signal generated by keyboard | |
SIGCONT | @ | Continue after stop | |
SIGCHLD | @ | Child process state has changed | |
SIGTTIN | + | Read attempted from control terminal while process is in background | |
SIGTTOU | + | Write attempted to control terminal while process is in background | |
SIGIO | @ | Input/output event | |
SIGXCPU | CPU time limit exceeded | ||
SIGXFSZ | File size limit exceeded | ||
SIGVTALRM | Virtual time alarm | ||
SIGPROF | Profiling timer alarm | ||
SIGWINCH | @ | tty window has changed size | |
SIGLOST | Resource lost | ||
SIGUSR1 | User-defined signal #1 | ||
SIGUSR2 | User-defined signal #2 |
[7] The signal number varies on some systems.
[8] The default action for most signals is to terminate.
Key:
* | If signal is not caught or ignored, generates a core image dump. |
@ | Signal is ignored by default. |
+ | Signal causes process to suspend. |
! | Signal cannot be caught or ignored. |
Signals are normally used between processes for process control. They are also used within a process to indicate exceptional conditions that should be handled immediately (for example, floating-point overflows).