http://qs1969.pair.com?node_id=144188


in reply to Re: making fork() a flag, and wait()ing properly
in thread making fork() a flag, and wait()ing properly

Nice! Your last example of:

foreach my $node ( @devices ) { if( $do_fork ) { my $pid = fork(); if( !$pid ) { do_exciting_things(); exit 0; } } else { do_exciting_things(); } }

looks very much like my most recent rewrite after starting this thread, so i am very encouraged that I am actually starting to understand what it is you dudes actually are talking about :-)

maybe though you could clear up the fog for me on this:

use POSIX; my $do_fork = 1; $SIG{CHLD} = sub { while( ( my $pid = waitpid(-1, POSIX::WNOHANG()) ) > 0 ){ ... } }

this one has me scratching my head. if you'd rather point to a perldoc reference, i'm up for that as well. it looks as though you're defining a subroutine as a hash value, but i'm not certain where/how its being called.

thanks -c

Replies are listed 'Best First'.
Re: Re: Re: making fork() a flag, and wait()ing properly
by lestrrat (Deacon) on Feb 08, 2002 at 19:41 UTC

    Are you asking me about just the syntax? or about signal handling with %SIG ?

    As far as the syntax goes, my code is equivalent to something like this:

    my $coderef = sub{ .... }; ## now $coderef is a reference to a subroutine, ## which can be executed like this: ## ## $coderef->(); # $coderef->( @args ) ## $SIG{ CHLD }= $coderef;

    So that means you can actually create an anonymous subroutine, and assign it to a scalar. Which in turn can be assigned to a hash value :)

    As for the signal handling portion, perldoc perlipc will give you more information. Not sure what is the best resource for that... ( I could try to explain, but I'm not confident that my knowledge is 100% correct )