in reply to Re: How do you wait for a process to end ?
in thread How do you wait for a process to end ?

Thanks man, I thought that the -1 in the waitpid call would wait for "all" child processes to end. Now its clearer why this is happening. I understand connecting the reaper subroutine to the child , but how would I make it so that I would know that exactly 10 processes have ended ??

p.s I have never used signal handlers before
Waris
  • Comment on Re: Re: How do you wait for a process to end ?

Replies are listed 'Best First'.
Re (3): How do you wait for a process to end ?
by VSarkiss (Monsignor) on Aug 23, 2001 at 19:34 UTC

    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

Re: Re: Re: How do you wait for a process to end ?
by claree0 (Hermit) on Aug 23, 2001 at 19:36 UTC
    You can just keep a running count of your current children - increment on each successful fork, and decrement on a successful call to \&reaper.