in reply to Re: Forking and waiting
in thread Forking and waiting

Right, I should have mentioned that I looked at both of those, too.

I just tried this variation with some success:

my ($i, $return, $pid, @pids);
for ($i = 1; $i <= 3; $i++) {
  if ($pid=fork) {
    push @pids, $pid;
  } elsif (defined $pid) {
    $return = &my_function($i);
    print "Returned: $return\n";
    exit;
  }
}

foreach my $childpid (@pids) {
  waitpid($childpid, 0);
}

print "All is done!\n";
Does that seem a reasonable/efficient way to handle this?

Thanks.

Replies are listed 'Best First'.
RE: RE: Re: Forking and waiting
by slayven (Pilgrim) on May 29, 2000 at 19:16 UTC
    just wait for the CHILD in the parent:

    my ($i, $return, $pid, @pids); for ($i = 1; $i <= 3; $i++) { if ($pid=fork) { push @pids, $pid; waitpid($pid,0); } elsif (defined $pid) { $return = &my_function($i); print "Returned: $return\n"; exit; } } print "All done.\n";
      ++
      Thanks slayven for waitpid($pid,0);
RE: RE: Re: Forking and waiting
by takshaka (Friar) on May 29, 2000 at 22:44 UTC

    Y'know, for ($i = 1; $i <= 3; $i++) {} is a horribly un-Perlish way to write for (1..3) {}

      i thought it was easier to drop the waitpid-statement into his code
      instead of explaining where he has to put it into :)
      You'll note that I use the $i variable further down in the code.
        Replace it with $_ :)