[Omaha.pm] Perl, fork, and waitpid()

George Neill georgen at neillnet.com
Mon Apr 20 21:00:11 PDT 2009


Jay,

On Mon, Apr 20, 2009 at 7:42 PM, Jay Hannah <jay at jays.net> wrote:
> On Apr 19, 2009, at 5:42 PM, Dan Linder wrote:
>>
>> I've tried using "waitpid(-1,WNOHANG)" thinking that would return the PID
>> of the last child to die, or "-1" if none had died since last checking, but
>> that seems to just hang _waiting_ for the next death to happen...   I did a
>> "waitpid($pid_of_a_child, 0)" but tha just waits until that specific child
>> dies.
>>
>> Anyone have any example code that can do this?
>
> We use POE for things like this. Specifically, this cookbook recipe:
>
> http://poe.perl.org/?POE_Cookbook/Child_Processes_3
>
> There's a pretty active IRC channel that can help if you get stuck
> (irc.perl.org #poe). I'm sure there's mailing lists too.
>
> I got somebody started from scratch last week and he seemed to take to it
> pretty quickly.

Do you know the POE internals?

# Detect the CHLD signal as each of our children exits.

sub sig_child {
    my ( $heap, $sig, $pid, $exit_val ) = @_[ HEAP, ARG0, ARG1, ARG2 ];
    my $details = delete $heap->{$pid};

    # warn "$$: Child $pid exited";
}

... got me thinking about the back-end.  I am guessing POE might give
some weird results on the old sysV signal implementations as SIGCHLD
has different semantics there.

Later,
George


More information about the Omaha-pm mailing list