Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Waiting..

by mikfire (Deacon)
on Feb 23, 2001 at 03:35 UTC ( #60390=note: print w/replies, xml ) Need Help??

in reply to Waiting..

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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://60390]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2017-09-24 07:59 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (273 votes). Check out past polls.