No, the -1 says "any" process, not "all processes". Usually you use waitpid to wait for a specific pid (hence the name ;-). But in both cases, it will return upon detecting one terminated child process.
To create a throttle in the signal handler, you need to make the parent stop when your list of pids reaches ten elements (like you're doing). Then you need to make the signal handler routine (reapchild in the example above) shorten the list every time waitpid returns something other than -1. So:
sub reapchild
{
while (waitpid(-1, &WNOHANG) > 0)
{
pop @pids;
}
}
This is quick-and-dirty in that your array of pids may not correspond to what's really out there. It would be better to take the returned pid from waitpid and delete that particular element from the array. TIMTOWTDI, but here's a simple one:
sub reapchild
{
my ($pid, $index);
PID:
while (($pid = waitpid(-1, &WNOHANG)) > 0)
{
foreach my $i (0..$#pids)
{
if ($pids[$i] == $pid)
{
splice @pids, $i, 1;
next PID;
}
}
warn "Unexpected $pid returned!\n";
}
}
HTH |