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


In reply to Re (3): How do you wait for a process to end ? by VSarkiss
in thread How do you wait for a process to end ? by jalebie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.