On Nov 9, 3:31 am, shawnhco...@gmail.com (Shawn H Corey) wrote:
> 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;

Hm, I'm not sure why you'd want to 'redo' in the case of a
blocking wait.  As I read the docs, a blocking wait on a
specific pid returns only that pid if reaped normally or -1 if
there's no such pid  (or that pid's been already reaped). In
either case, I'd think you'd just want to continue processing
the pid loop.

In contrast (at least on POSIX compliant OS's),  a non-blocking
blocking wait with -1 rather than a specific pid  could potentially
reap other pid's:

     use POSIX ":sys_wait_h";
      #...
     do {
        $kid = waitpid(-1, WNOHANG);
     } while $kid > 0;

This might reap an extraneous process such as one launched via
backticks maybe in an forked pid for instance.

> On some systems, waitpid may return for any interrupt, not just when the
> child terminates.

Also the POSIX non-blocking wait has the advantage in that case by
providing macro's that can reveal some additional exit/termination
status:

        use POSIX ":sys_wait_h";
        ...
        $kid = waitpid( -1, WNOHANG);
        print "$kid: signal termination" if WIFSIGNALED($?);

--
Charles DeRykus


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