The wait call is a form of sleep. It sleeps until SIGCHLD if there are no completed children queued up for attention. If there are, it returns the pid of the one it serviced.
Your application might look like this skeleton,
our %kid;
sub forkus {
my $task = shift; # it's a hash ref
defined(my $cpid = fork) or warn $! and return;
$kid{$cpid} = $_->id, return $cpid if $cpid;
exec $_->cmd;
exit -1;
}
forkus($_) for (@first_tasks);
while (%kid) {
my $cpid = wait; # sleep until somebody's done
forkus(next_task($cpid)) if more($cpid); # suitably defined
delete $kid{$cpid};
}
I've glossed over the design of the task hashes and the next_task() and more() functions. They will be application dependent.
POE may be just what you need, but I find it somewhat difficult to work with. Lack of practice, perhaps.
|