Taylor, Andrew (ASPIRE) wrote:
> Hello
>
> I have a script that, much like the Little Old Lady who lived in a shoe,
> has so many children it's not sure what to do.
>
> Basically, the script does some stuff, then kicks off multiple copies of
> an external process that all run in parallel. It then has to wait until
> all of them have completed (successfully) then carry on with the next
> bit.
>
> What I've cobbled together so far (i.e. swiped of the web then
> modified...)
>
> use strict;
> use warnings;
>
> # Do some stuff here
>
> for (my $i=1; $i <=$num_processes; $i++)
for my $i ( 1 .. $num_processes )
> {
> defined(my $pid = fork) or die( "Cannot fork : $!\n");
>
> # Put all the pids into an aray
> if ($pid)
> {
> push @pids, $pid;
> } else {
> exec "external giggery pokery"
> }
> }
>
> foreach my $child_pid (@pids)
> {
> waitpid($child_pid, 0);
my $kid = waitpid($child_pid, 0);
> unless( 0==$? )
> {
> die ("Oh No! An external process has failed!!\n");
> }
redo if $kid != $child_pid;
> }
On some systems, waitpid may return for any interrupt, not just when the
child terminates.
--
Just my 0.00000002 million dollars worth,
Shawn
Programming is as much about organization and communication
as it is about coding.
I like Perl; it's the only language where you can bless your
thingy.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/