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){} };
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Best way to kill a child process
by Eliya (Vicar) on Sep 21, 2011 at 13:58 UTC | |
by Marshall (Canon) on Sep 21, 2011 at 15:31 UTC | |
by Eliya (Vicar) on Sep 21, 2011 at 18:41 UTC | |
by Marshall (Canon) on Sep 22, 2011 at 00:50 UTC | |
by flexvault (Monsignor) on Oct 10, 2011 at 15:16 UTC | |
|
In Section
Seekers of Perl Wisdom