Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

  • waitpid() is used because wait() does not have a non-blocking option, meaning your program would pause while waiting for a child to reap.
  • Since I have no need to record child statuses, I can just use $SIG{CHILD} = 'IGNORE';
  • The WNOHANG flag means to return 0 if there are no dead children.


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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 23:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found