Please consider that I've a Linux bias answering this kind of questions, so don't trust them too much :)

wait basically waits until one of the children exits. It is used to avoid having Zombie processes: if you don't wait() for them, and you go a long time in your parent process before exiting, you'll see these processes in the process list even if they already exited, marked as "zombie" (this means that resources in the kernel associated to the process aren't freed). So, wait is the way by which the parent process acknowledges the kernel of the son's death. It's interesting that this mechanism actually tries to force the programmer not to ignore return values from child processes, much like the good practice of never throwing away return values from functions.

Moreover, wait returns as soon as the first child exits; in the OP's post, (s)he created 8 of them, and had to wait until all of them have exited. According to perldoc -f wait, a call to the wait function returns -1 if, and only if, there are no more children alive(1) - thus the cycle.

(1)Ok, ok, it's not exactly like this. If you happened to set $SIG{'CHLD'} to 'IGNORE', this zombie-ridding happens automatically, and you're likely to have wait always return -1 - but I guess it's not the case with this thread :) Moreover, in this case I would discourage the OP to do that, because (s)he's using wait as a synchronisation mechanism to understand when all the children exited. BTW, this is documented in perldoc perlipc, but I suggest taking a look in perldoc perlport as well.

Flavio (perl -e "print(scalar(reverse('ti.xittelop@oivalf')))")

Don't fool yourself.

In reply to Re^3: Fork Wait and probably grandchildren by polettix
in thread Fork Wait and probably grandchildren by Discordanian

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.