Hey all, I'm writing a script that needs to fork() and i want to be sure that I'm cleaning up all my zombies.

Between perlipc and Programming Perl, I've gotten a bit of zombie reaping code that I mostly understand, but need some clarification on.

The code I'm using is:

our $zombies = 0; $SIG{CHLD} = sub { $zombies++ }; sub reaper { my $zombie; our %Kid_Status; # store each exit status $zombies = 0; while (($zombie = waitpid(-1, WNOHANG)) != -1) { $Kid_Status{$zombie} = $?; } } while (1) { reaper() if $zombies; ... }
Because I'm not all that familiar with waitpid() - or wait(), for that matter - I'm not clear on whether or not this code has the parent process hanging around until child processes are reaped. Also, I'm not sure if this is preventing the script from fork()'ing another process before previous children are reaped.

Finally, would I be better off just running the reaper() function w/o checking for zombies first, so that the script constantly is checking on child procs. The docs for waitpid() said that it could return 0 if child procs are still running, which seems like it might be helpful insofar as making sure child processes are reaped before the parent exits. This seems like it would account for any scenarios where the parent process finished it's logic and exits before receiving a CHLD signal.

UPDATE: The cookbook has a pretty good bit on reaping zombies. Three things cleared up in recipe 16.19 of The Cookbook First Edition.


dsb
This @ISA my( $cool ) %SIG

In reply to Reaping Zombies by dsb

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.