igor igor - 1 year ago 88
Linux Question

What is the use of ignoring `SIGCHLD` signal with `sigaction(2)`?

It turns out that we can prevent appearing of a zombie process (i.e. the one whose parent doesn't

for it to
) by specifying
signal to be ignored with
by its parent. However, it seems like
is ignored by default anyway. How come does this work?

int main (void) {
struct sigaction sa;
sa.sa_handler = SIG_IGN; //handle signal by ignoring
sa.sa_flags = 0;
if (sigaction(SIGCHLD, &sa, 0) == -1) {
int pid = fork();
if (pid == 0) { //child process
do_something(); //parent process
return 0;

Answer Source

The default behavior of SIGCHLD is to discard the signal, but the child process is kept as a zombie until the parent calls wait() (or a variant) to get its termination status.

But if you explicitly call sigaction() with the disposition SIG_IGN, that causes it not to turn the child into a zombie -- when the child exits it is reaped immediately. See http://stackoverflow.com/a/7171836/1491895

The POSIX way to get this behavior is by calling sigaction with handler = SIG_DFL and flags containing SA_NOCLDWAIT. This is in Linux since 2.6.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download