Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( #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.


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

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, 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, 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (4)
    As of 2019-12-10 06:09 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found