in reply to Possible waitpid problem - pid reuse?

The exited child processes will hang around in Z[ombie] state until wait or waitpid collects their exit status. That means their pid is still in use until you release it.

The exception occurs when you have a $SIG{CHLD} handler. That makes the system think you've handled it already. On some systems you can make nobody care what happens to the kids by setting the handler to 'IGNORE', but that's clearly not your situation.

After Compline,
Zaxo

  • Comment on Re: Possible waitpid problem - pid reuse?

Replies are listed 'Best First'.
Re^2: Possible waitpid problem - pid reuse?
by Anonymous Monk on Sep 13, 2004 at 11:28 UTC
    > The exception occurs when you have a $SIG{CHLD} handler. That makes the system think you've handled it already.

    Not true. The program below forks, the child exits immediately. You will see that the signal handler is fired, but the child process stays around (as a zombie) until it's reaped by the waitpid call.

    $SIG{CHLD} = sub {print("SIGCHLD\n")}; exit unless $pid = fork; sleep 1; system "ps -p $pid"; waitpid $pid, 0; system "ps -p $pid"; __END__ SIGCHLD PID TTY TIME CMD 29260 pts/32 00:00:00 perl <defunct> SIGCHLD PID TTY TIME CMD SIGCHLD
    The signal handler fires three times in total: one for the child process, and once each for the calls to system.

    It's only when you explicitely set $SIG{CHLD} to IGNORE that child processes stay behind.

    It's noteworthy to remember that POSIX does not specify what should happen if $SIG{CHLD} is ignored. (Which makes it a joy to program platform independently on Unix)