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 recieves 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.

In perl, you would do one of the following:

1. wait till child finishes


if (fork()==0) {

  1. ==0 implies child process

do stuff... exit;

}

  1. if fork returned -1, there was an error forking (but we'll ignore that)
  2. if here, assume it is the parent processes
  3. wait() call won't return until the child process has finished.

wait(); # when this returns, the child has been "reaped"

  1. could use waitpid() with WNOHANG to return -1 if child isn't finished yet
  2. 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.