reyjrar has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use strict; my $MAX_PROCESSES=10; my $npids=0; for(1..15) { my $pid; print "Forking: $_\n"; sleep 1; $pid=fork(); if($pid > 0) { #we forked successfully $npids++; if($npids>=$MAX_PROCESSES) { my $wait=wait(); if($wait) { $npids--; } } elsif(undef $pid) { # we didn't fork successfully print "fork error!\n"; } else { #what do we want to do? &doit($_); exit(0); # free this pid } exit(0); # we shouldn't get to this point } } # if we have any stragglers, lets wait for them to finish. for(1..$npids){ my $wt=wait(); if($wt==-1){ #print "hey $!\n"; redo; } } sub doit { my $num = shift; sleep 5; print "$num DONE\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: For all your forking needs..
by chromatic (Archbishop) on Oct 13, 2000 at 01:33 UTC | |
by reyjrar (Hermit) on Oct 13, 2000 at 20:12 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |