mjoyce has asked for the wisdom of the Perl Monks concerning the following question:

I'm forking several child processes at once, and then "processing" them individually as they complete. I'd like for the "processing" subroutine to have some information about the child, in order to identify the child, but I don't know how to do this without globalizing my entire data structure

My first thought was to pass arguments to the reapChildren subroutine, but I get a "Not a subroutine reference"error

I then, tried:  sub {reapChildren($app)}, but  reapChildren was only getting the LAST app in the list.

Any help is greatly appreciated...even if there's a totally better way to do this. Thanks. Michele

Here's a snippet

:
use POSIX qw(:sys_wait_h :errno_h :signal_h); use strict; sub runChildren { my $obj = $_[0]; my $runlist = $obj->{'runlist'}; foreach my $app (@$runlist) { my $pid; if ($pid = fork) { $SIG{CHLD} = \&reapChildren; } else { my $appargs = $app->{'args'}; exec(@$appargs); } } } sub reapChildren { my $pid; $pid = waitpid(-1,&WNOHANG); if ($pid == -1 || WIFEXITED($?)) { processApplication; } $SIG{CHLD} = \&reapChildren; } sub processApplication { #Need to know which application needs processing }

Replies are listed 'Best First'.
Re: Passing args to a subroutine reference
by tilly (Archbishop) on Mar 04, 2009 at 18:33 UTC
    You need to store the $pid to $app relationship in a hash, then when you reap a child look up the $app in that hash.
Re: Passing args to a subroutine reference
by locked_user sundialsvc4 (Abbot) on Mar 04, 2009 at 22:21 UTC

    I agree. “The parent knows more about the child than the child itself does,” namely, why the child was created and for what purpose. As long as the (expired... may he rest in peace...) child can be properly identified by the parent, the child really has no reason to know. The parent remembers all.