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.

#!/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);
Example test file:
sleep 4 sleep 2 sleep 6 sleep 3 sleep 4 sleep 2
Test run: perl multi.pl 2 < test_file_above

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.