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!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|