>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.

Hello

Thanks for your pointers.

One question (I've never used redo before.)

Am I rignt in assuming the redo should be before the $? test (in case
this rogue pid has returned a status other than 0)?, like so:

foreach my $child_pid (@pids)
{
  waitpid($child_pid, 0);
  my $kid = waitpid($child_pid, 0);

  redo if $kid != $child_pid;  # So goes back to start of loop from here

  unless( 0==$? )  # And never performs this test
  { 
    die ("Oh No! An external process has failed!!\n");
  }
}


That makes most sense to me but I'm not sure how to test if my
assumption is correct.  (I'd have to somehow get the system to return a
pid/status for a different process)

Cheers
Andy

Capgemini is a trading name used by the Capgemini Group of companies which 
includes Capgemini UK plc, a company registered in England and Wales (number 
943935) whose registered office is at No. 1 Forge End, Woking, Surrey, GU21 6DB.
This message contains information that may be privileged or confidential and is 
the property of the Capgemini Group. It is intended only for the person to whom 
it is addressed. If you are not the intended recipient, you are not authorized 
to read, print, retain, copy, disseminate, distribute, or use this message or 
any part thereof. If you receive this message in error, please notify the 
sender immediately and delete all copies of this message.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to