In Perl, how can I block for a bunch of system calls to finish?
perl
Solution
The easiest way to do this is probably to `fork` a new child process for each system call, and then wait for them to finish. Here's a simplified example:
my @commands = ( "sleep 5 && echo step 1 done",
"sleep 3 && echo step 2 done",
"sleep 7 && echo step 3 done" );
my @pids;
foreach my $cmd( @commands ) {
my $pid = fork;
if ( $pid ) {
# parent process
push @pids, $pid;
next;
}
# now we're in the child
system( $cmd );
exit; # terminate the child
}
wait for @pids; # wait for each child to terminate
print "all done.\n";
Problem
I'm doing a bunch of system calls that I want to run in parallel: ``` system(" sleep 5 && echo step 1 done &"); system(" sleep 3 && echo step 2 done &"); system(" sleep 7 && echo step 3 done &"); // block here ``` How can I block the program flow until all the system calls are finished?