my %children;
my $max_children = 3;
my @todo_list;
foreach my $dir(qw/lmo lin lmf bsu ssu sst/)
{
fork_download($dir);
}
complete_downloads();
sub fork_download
{
my $dir = shift;
if ( ( scalar keys %children ) < $max_children )
{
my $pid = fork;
if ( $pid )
{
# this is parent process
$children{$pid} = $dir;
}
else
{
if ( not defined $pid )
{
die "failed to fork!\n";
}
# this is child process
my $link = "ftp://ftp.<Path_to_dir>.jp/$dir/*";
system("wget -nH -nd --timestamping $link");
# exit child process
exit 0;
}
}
else
{
# too much child labor! queue for later
push @todo_list, $dir;
}
}
sub complete_downloads
{
while (%children)
{
my $childpid = wait();
print "child [$childpid][" . $children{$childpid}. "] is done\
+n";
delete $$children{$childpid};
if ( @todo_list )
{
my $nextDir= shift @todo_list;
fork_download($nextDir);
}
}
}
-HTH
Update: forgot to exit child process!