On 29 Jun 2001 09:52:16 +0100, Pierre Smolarek wrote:
> Small problem with the below code.... how can you control the MAX amount of
> children you have?
> 
<snip />

Well, that is harder.  One way (and there are probably better ones)
would be to work with many subsets of the machines getting the pids for
each set and then waiting for them [the children] to finish before
forking off more.

NOTE: This code has not been tested; there may be typos or logic bugs in
it.

<code>
use constant MAX => 10;

my @machines = get_machines();

while (@machines) { #while there are still machines left
        my @pids;
        foreach my $machine (splice(@machines, 0, MAX)) {
                push @pids, fork;
                if ($pids[-1] == 0) { #I am a child
                        check_machine($machine);
                } elsif (not $pids[-1]) {
                        #if parent then $pid = process id, if error then
                        #$pids[-1] = undef
                        pop @pids; #get rid of the undef
                        error();
                }
        }
        #reap the children
        foreach my $pid (@pids) {
                waitpid $pid, 0; #wait for each fork to finish
        }
}
</code>
 
--
Today is Setting Orange, the 34th day of Confusion in the YOLD 3167
All Hail Discordia!


Reply via email to