wait/waitpid returns -1 when there are no children or the children are automatically being reaped ( according to perldoc -f wait ). So the loop is waiting for all of the children to exit

I personally don't like this child count decrementing business... I'd rather explicitly check if the pid that I just got is the pid that I'm waiting for, i.e., my child. That may just be me...

## XXX - I use hashes in this type of situation ## because I usually associate more meta data to a pid ## which must be used by the parent when the child ## exits... and delete $hash{ $foo } is cleaner ## than splice() to me :) use POSIX; my %pids; for( 1 .. $numchild ) { my $pid = fork(); ## check if $pid id defined, etc. if( $pid ) { ## parent $pids{ $pid } = 1; } else { do_interesting_stuff(); exit; } } while( ( my $pid = waitpid(-1, POSIX::WNOHANG() ) ) > 0 ){ if( exists $pids{ $pid } ) { delete $pids{ $pid }; } else { print STDERR "Unknown child $pid\n"; } }

In reply to Re: what is this doing? - while (wait() != -1) by lestrrat
in thread what is this doing? - while (wait() != -1) by c

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.