Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: controlling child processes

by bluto (Curate)
on Apr 04, 2006 at 23:03 UTC ( #541238=note: print w/replies, xml ) Need Help??

in reply to controlling child processes

It is easy to get these problems wrong, so I second using Parallel::ForkManager. In your solution you need to keep looping not only if there is more work to spawn, but also if there are children to reap. Update: That's confusing to read -- it just means you'll need to reap 3 children at the end.

I like to use a hash to maintain the children. Here's an untested solution, probably with bugs ...

use strict; use warnings; $SIG{CHLD} = 'IGNORE'; my $max_procs = 3; # don't use strings when you want numbers my @work = qw(fw1 fw2 fw3 fw4 fw5); my %kids; while (@work or keys %kids) { if (@work and $max_procs > keys %kids) { my $fw = shift @work; my $pid = fork; die "Can not fork!" if ! defined $pid; if ($pid) { $kids{$pid} = $fw; } else { exec "remotelogfile $fw logfile > /var/$fw.log"; die; } } else { my $pid = waitpid(-1, 0); delete $kids{$pid}; } }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://541238]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2021-10-21 01:32 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (82 votes). Check out past polls.