Signals
Description
A signal is a notification sent to a process or thread to indicate that a particular type of event has occurred. Processes can specify the action to be taken when a given signal is raised. This could be calling a given handler function, or ignoring the signal, or performing a default action (usually ignoring the signal or terminating the process). Common uses of signals include:
- handling
SIGINT
to prevent a program from being terminated by control-C, or alternatively, to provide for an orderly shutdown. Similarly forSIGTERM
andSIGQUIT
; - handling
SIGPIPE
to prevent termination when writing to a broken pipe or disconnected socket; - handling
SIGCHLD
to detect when there are zombie processes in need of reaping; and - handling
SIGHUP
as a signal to a daemon that it should reload its configuration.
Some care is needed when writing handler functions because they may be invoked at any point during the execution of a program. They cannot safely access global data unless it is of type sig_atomic_t
, or call system functions outside of a limited subset. If a signal handler interrupts a system function that was executing in the foreground then the latter may return early, typically with the error code EINTR
.
microHOWTOs
- Ignore SIGPIPE without affecting other threads in a process
- Prevent a process from terminating when writing to a broken pipe
- Reap zombie processes using a SIGCHLD handler
Further reading
- Signal Concepts, Base Specifications, Issue 7, The Open Group, 2008
- Signal Handling, GNU C Library Reference Manual, The GNU Project