Better yet, if you're going to try to do something with multiple children, here's an example to speed you up. Create a file for input with a number of sleep commands with random values, then test.
Example test file:#!/usr/bin/perl # multi.pl # usage: multi.pl [max_tasks] < job_list # max_task defaults to 4 # # do all the jobs as quickly as possible in parallel, but no more than # max_tasks at a time. use warnings; use strict; sub mysystem { my $cmd = shift; my $pid; #print "forking: $cmd\n"; if (($pid = fork()) == 0) { exec($cmd); } # child return $pid; # parent } # how many tasks can we do at once my $max_tasks = 4; if (defined($ARGV[0])) { $max_tasks = shift(@ARGV); } # num of curr tasks my $num_tasks = 0; # pids running, and command run my %pids = (); my ($pid, $cmd); # read tasks while ($cmd = <>) { chomp($cmd); # execute the task print "starting: $cmd\n"; $pid = mysystem($cmd); #print "forked pid $pid\n"; $pids{$pid} = $cmd; $num_tasks++; # if we're doing all we should, wait for 1 to end while ($num_tasks >= $max_tasks) { $pid = wait(); #print "reaped pid $pid\n"; last if ($pid == -1); # error? print "finished: $pids{$pid}\n"; delete($pids{$pid}); $num_tasks--; } } # wait for all children to end while ($num_tasks > 0) { $pid = wait(); last if ($pid == -1); # error? print "finished: $pids{$pid}\n"; delete($pids{$pid}); $num_tasks--; } exit(0);
Test run: perl multi.pl 2 < test_file_abovesleep 4 sleep 2 sleep 6 sleep 3 sleep 4 sleep 2
In reply to Re^2: exec function does not return command prompt
by kbrannen
in thread exec function does not return command prompt
by casman46
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |