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??
Sigh. Look. You want to reap the children yourself, right? Stop invoking the magic of the signal handler and do it your own bad self. Oh, and stop re-installing the signal handler until you know you need to do it.

The idea would be like this

# Fork a child sub SPAWN { if ( $cpid = fork ) { $tracker{$cpid} = 1; } elsif ( defined $cpid ) { # do interesting things } else { die "What a world, what a world!"; } } # Spawn all your children SPAWN() for ( @jobs ); # Wait for them to die do { select( undef, undef, undef, 0.5 ); # 1/2 second sleep $counter++; $dpid = waitpid(-1,1); # non-blocking waitpid if ( defined( $dpid ) && $dpid != 1 ) { if ( defined( $tracker{$dpid} ) ) { print "Child $dpid is dead\n"; delete $tracker{$dpid}; } else { print "I am now terribly confused.\n"; } } until ( $dpid == -1 || $counter > 2000 ); if ( @temp = keys %tracker ) { print "Timed out while waiting for these pids:@temp\n"; }
Obviously, this is pretty quickly written code - several declarations and initializations are left as an excercise to the reader :) Notice I wait 1,000 seconds before giving up. You really ought to make the a variable set on the command line. The sleep loop is actually pretty tight. You could, depending on how long lived the processes are, make it longer. Use sleep() if the interval is greater than 1 second.

The spawning loop can be much fancier as well - I have several examples that wait for a child to finish and then spawn another off until there are no more jobs to be done. You can pass information into the SPAWN routine ( like the name of the file the child process is handling ) and print nicer messages.

I have several refinements on this idea, if you are interested but I haven't the free time to go into all of them. Email me ( mik at speed dot stdio dot com ) if you would like to see them.

mikfire


In reply to Re: Waiting.. by mikfire
in thread Waiting.. by spaz

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 musing on the Monastery: (4)
As of 2024-03-29 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found