Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 wait() for it to _exit()) by specifying SIGCHLD signal to be ignored with sigaction() by its parent. However, it seems like SIGCHLD 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
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    if (sigaction(SIGCHLD, &sa, 0) == -1) {
        perror(0);
        exit(1);
    }
    int pid = fork();
    if (pid == 0) { //child process
        _exit(0);
    }
    do_something(); //parent process
    return 0;
}
like image 471
igor Avatar asked Nov 15 '16 03:11

igor


People also ask

Can SIGCHLD be ignored?

When a child process stops or terminates, SIGCHLD is sent to the parent process. The default response to the signal is to ignore it. The signal can be caught and the exit status from the child process can be obtained by immediately calling wait(2) and wait3(3C).

What is the use of SIGCHLD signal?

The SIGCHLD signal is the only signal that the z/TPF system sends to a process. When a child process created by the tpf_fork function ends, the z/TPF system: Sends a SIGCHLD signal to the parent process to indicate that the child process has ended.

Which sigaction flag is used to prevent a system call from being interrupted?

You can set sa_mask in your sigaction call to block certain signals while a particular signal handler runs. This way, the signal handler can run without being interrupted itself by signals.

What is the difference between signal and sigaction?

The signal() function does not (necessarily) block other signals from arriving while the current handler is executing; sigaction() can block other signals until the current handler returns.


1 Answers

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 https://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.

like image 72
Barmar Avatar answered Oct 20 '22 22:10

Barmar