http://www.perlmonks.org?node_id=927139


in reply to Best way to kill a child process

When a child dies, the O/S sends a CHLD signal to the parent to let it know that one of its kids has died. There will be an entry in the process table with the exit status of the child or children. This entry in the process table is called a "zombie". This entry takes up system resources and should be removed by the parent. You get rid of the entry by reading the status - most of the time nobody cares what this status is and it is thrown away. This is called "reaping" the child.

Anyway, the following line of code installs a signal handler for the CHLD signal. When that signal happens, the while loop runs which will read and discard the status of any children who have died (in general case might be more than one). It is ok to put this at the beginning of the code (which means that the child will get one of these too) - but it won't be getting CHLD signals itself.

$SIG{CHLD} = sub {while (waitpid(-1, WNOHANG) > 0){} };