SIGCHLD

SIGCHLD
Description Child process terminated or stopped
Default action Ignore the signal
SA_SIGINFO macros
CLD_EXITED child has exited
CLD_KILLED child has terminated abnormally and did not create a core file
CLD_DUMPED child has terminated abnormally and created a core file
CLD_TRAPPED traced child has trapped
CLD_STOPPED child has stopped
CLD_CONTINUED stopped child has continued

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.

Etymology

SIG is a common prefix for signal names. CHLD and CLD are abbreviations for child.

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 struct sigaction sa = {.sa_handler = SIG_IGN}; sigaction(SIGCHLD, &sa, 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')

References

  1. ^ signal(7): overview of signals – Linux Conventions and Miscellany Manual
  2. ^ a b wait(2): wait for process to change state – Linux System Calls Manual
  3. ^ http://fuse4bsd.creo.hu/localcgi/man-cgi.cgi?signal+3
  4. ^ sigaction(3): examine and change a signal action – Linux Library Functions Manual