perlipc doc / another race (?)

N

Nitzan Shaked

This is realted to the perlipc but not necessarily to perl itself so
much as to plain unix signals. Still, I hope you accomodate this.

Reading perlipc, the first part (re signals) there is this code example
towards the end of that part:

use POSIX ":sys_wait_h";
sub REAPER {
my $child;
# If a second child dies while in the signal handler caused by the
# first death, we won't get another signal. So must loop here else
# we will leave the unreaped child as a zombie. And the next time
# two children die we get another zombie. And so on.
while (($child = waitpid(-1,WNOHANG)) > 0) {
$Kid_Status{$child} = $?;
}
$SIG{CHLD} = \&REAPER; # still loathe sysV
}
$SIG{CHLD} = \&REAPER;
# do something that forks...

==> It says that it loops in the handler to catch and other SIGCHLD's
that might happen during the execution of the handler, and for which
the handler will not be invoked again.

It seems to me like if a child terminates after the while's closing
brace (1 uSec after...) then the signal is lost! It (the child) will
not be reaped by the current handler, but will also not be signalled.

How am I suppose to reap such children, then?
 
X

xhoster

Nitzan Shaked said:
This is realted to the perlipc but not necessarily to perl itself so
much as to plain unix signals. Still, I hope you accomodate this.

Reading perlipc, the first part (re signals) there is this code example
towards the end of that part:

use POSIX ":sys_wait_h";
sub REAPER {
my $child;
# If a second child dies while in the signal handler caused by
the # first death, we won't get another signal. So must loop here
else # we will leave the unreaped child as a zombie. And the next
time # two children die we get another zombie. And so on.
while (($child = waitpid(-1,WNOHANG)) > 0) {
$Kid_Status{$child} = $?;
}
$SIG{CHLD} = \&REAPER; # still loathe sysV
}
$SIG{CHLD} = \&REAPER;
# do something that forks...

==> It says that it loops in the handler to catch and other SIGCHLD's
that might happen during the execution of the handler, and for which
the handler will not be invoked again.

It seems to me like if a child terminates after the while's closing
brace (1 uSec after...) then the signal is lost! It (the child) will
not be reaped by the current handler, but will also not be signalled.

How am I suppose to reap such children, then?

Because of the loop, any child that slips through the cracks will get
reaped when the next child exits.

I rarely find $SIG{CHLD} useful, other than with "IGNORE". If I need to
wait, I find some other place to do it than in a signal handler.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top