I have written a server in perl, waiting for tcp connections. Every time the server receives one, it forks a new child in charge of it and continues listening for another connection.( And so it cannot wait for the SIGCHILD. ) The child does what it is supposed to do and exits, but continues to "live" as a zombie. What is the solution please ?
Asked
Active
Viewed 130 times
1
-
Have you tried `wait()`/`waitpid()`? – el.pescado - нет войне Jan 03 '14 at 13:04
-
thanks for your answer el.pescdo the problem is that waitpid blocks the parent. My parent has something else to do. – babak Jan 03 '14 at 13:12
-
`waitpid (-1, &WNOHANG)` should not block. See below – grebneke Jan 03 '14 at 13:13
1 Answers
5
Set $SIG{CHLD} = 'IGNORE';
to have the system handle them for you automatically.
Or set $SIG{CHLD}
to a subroutine and waitpid
:
use POSIX qw( WNOHANG );
$SIG{CHLD} = sub {
while( ( my $child = waitpid( -1, WNOHANG ) ) > 0 ) {
print "SIGNAL CHLD $child\n";
}
};
If you really want to understand how it works (of course you do! :), you should read perlipc
There is also more info here on Stack Overflow