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};
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.