Penguin
Note: You are viewing an old revision of this page. View the current version.

Signal: Child process

Child process has changed state. This signal is raised whenever a child process receives a signal or terminates. This can be used to call wait4(2) to cleanup any ZombieProcesses that the process may have. On some Unixes it is possible to explicitly set SIGCHLD to "SIG_IGN" (ignore) and child processes will be cleaned up automatically. The default action (SIG_DFL) for this signal is to ignore it (but this does not reap the process until the parent wait()s).

From the signal(2) manpage:
According to POSIX (B.3.3.1.3) you must not set the action for SIGCHLD to SIG_IGN. Here the BSD and SYSV behaviours differ, causing BSD software that sets the action for SIGCHLD to SIG_IGN to fail on Linux.
From the sigaction(2) manpage:
POSIX.1-1990 disallowed setting the action for SIGCHLD to SIG_IGN. POSIX.1-2001 allows this possibility, so that ignoring SIGCHLD can be used to prevent the creation of zombies (see wait(2)). Nevertheless, the historical BSD and System V behaviours for ignoring SIGCHLD differ, so that the only completely portable method of ensuring that terminated children do not become zombies is to catch the SIGCHLD signal and perform a wait(2) or similar.

In perl, you would do one of the following:

1. wait till child finishes


  if (fork()==0) {
     # ==0 implies child process
     do stuff...
     exit;
  }

  # if fork returned -1, there was an error forking (but we'll ignore that)
  # if here, assume it is the parent processes
  # wait() call won't return until the child process has finished.
  wait(); # when this returns, the child has been "reaped"
  # could use waitpid() with WNOHANG to return -1 if child isn't finished yet
  # when here, the child process has finished.
  ...

2. Don't care about the child process finishing


 $SIG{CHLD}='IGNORE'; # see the perlipc(1) man page for this...

  if (fork()==0) {
    do child stuff...
    exit;
  }

  continue in parent....

If you don't do either of wait() or set the signal handler, then the child will remain a ZombieProcess.