SIGCHLD
|
|
It has been suggested that this article or section be merged into Unix signal . (Discuss) Proposed since January 2012. |
| Description | Child process terminated or stopped | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Default action | Ignore the signal | ||||||||||||
| SA_SIGINFO macros | |||||||||||||
|
|||||||||||||
On POSIX-compliant platforms, SIGCHLD is the signal sent to a process when a child process terminates. The symbolic constant for SIGCHLD is defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across platforms.
On Linux, SIGCLD is a synonym for SIGCHLD.
[edit] Etymology
SIG is a common prefix for signal names. CHLD and CLD are abbreviations for child.
[edit] Usage
In Unix, a process can have children created by fork/vfork or similar system calls . When the child terminates a SIGCHLD signal is sent to the parent. By default the signal is simply ignored.[1] However commonly wait system call implemented in a handler for the SIGCHLD, so that the parent may act upon the exit status of the child.
When a child process terminates before the parent has called wait, the kernel retains some information about the process to enable its parent to call wait later [2]. Because the child is still consuming system resources but not executing it is known as a zombie process.
POSIX.1-2001 allows a parent process to elect for the kernel to automatically reap child processes that terminate by setting the disposition of SIGCHLD to SIG_IGN (which is the default) or by setting the SA_NOCLDWAIT flag for the SIGCHLD signal; Linux 2.6 kernels adhere to this behavior, and FreeBSD supports both of these methods since version 5.0 [3]. However, because of historical differences between System V and BSD behaviors with regard to ignoring SIGCHLD, calling wait remains the most portable paradigm for cleaning up after forked child processes.[4][2]
Listed below is code that will elect automatic child reaping, shown in a number of programming languages:
| Language | Syntax |
|---|---|
| C (classic) | signal(SIGCHLD, SIG_IGN); |
| C | sigaction(SIGCHLD, &(struct sigaction){.sa_handler = SIG_IGN}, NULL); |
| OCaml | Sys.set_signal Sys.sigchld Sys.Signal_ignore |
| Pascal | fpsignal(SIGCHLD, SIG_IGN); |
| Perl | $SIG{CHLD} = 'IGNORE'; |
| PHP | pcntl_signal(SIGCHLD, SIG_IGN); |
| Python | signal.signal(signal.SIGCHLD, signal.SIG_IGN) |
| Ruby | Signal.trap('CHLD', 'IGNORE') |
[edit] References
- ^ : overview of signals – Linux Conventions and Miscellany Manual
- ^ a b : wait for process to change state – Linux System Calls Manual
- ^ http://fuse4bsd.creo.hu/localcgi/man-cgi.cgi?signal+3
- ^ : examine and change a signal action – Linux Library Functions Manual
|
||||||||