Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Welcome to the Monastery
 
PerlMonks  

Re: SIG CHLD IGNORE and wait at same time

by rjt (Chaplain)
on Aug 03, 2013 at 11:08 UTC ( #1047696=note: print w/ replies, xml ) Need Help??


in reply to SIG CHLD IGNORE and wait at same time

You have a race condition in your parent code:

kill (POSIX::SIGUSR2, $pid); $SIG{CHLD} = 'IGNORE';

(You send the signal to your child before you set the $SIG{CHLD} handler.)

Try swapping those two statements, for a start.

Beyond that, I wonder why you set $SIG{CHLD} = 'IGNORE' but then loop on wait() in the normal execution flow? It's unlikely that's causing your bug, but the docs are a little non-committal on this (emphasis mine):

Calling wait() with $SIG{CHLD} set to "IGNORE" usually returns -1 on such platforms.

'IGNORE' is most useful when you really don't care. But if you don't want to exit until all of your children exit, I'd recommend just using your own wait() or waitpid() loop, perhaps in conjunction with a custom signal handler if you need out-of-band child exit notifications.


Comment on Re: SIG CHLD IGNORE and wait at same time
Select or Download Code
Re^2: SIG CHLD IGNORE and wait at same time
by vsespb (Hermit) on Aug 03, 2013 at 18:03 UTC
    Try swapping those two statements, for a start.
    No, it did not help.
    You have a race condition in your parent code:
    I don't think it was race condition in this case, because this code should work with and without SIG CHLD IGNORE statement.
    IGNORE' is most useful when you really don't care.
    Well, for example I want to reap zombies while program run, but at the same time I want to wait all child processes at exit.
    I'd recommend just using your own wait() or waitpid() loop, perhaps in conjunction with a custom signal handler if you need out-of-band child exit notifications.
    Yes, as I told, this fixes problem:
    $SIG{CHLD} = sub { while( wait() != -1 ){ print STDERR "wait0\n"}; };
    so probably there is a workaround.
    usually returns -1
    Yes, indeed. Ok, thanks.
      I don't think it was race condition in this case, because this code should work with and without SIG CHLD IGNORE statement.

      Fair enough; your further testing speaks to that. I'd still recommend setting up the handler before the kill, as a matter of good practice if nothing else.

      Well, for example I want to reap zombies while program run, but at the same time I want to wait all child processes at exit.

      OK. Luckily, that's quite easy to do, with only one wait in your code. See perlipc for more info, but here's a handler inspired by that:

      use POSIX ':sys_wait_h'; # WNOHANG my %pids; # Parent's hash of child pids $SIG{CHLD} = sub { local ($?, $!); # Don't change $! or $? outside handler until (-1 == (my $pid = waitpid(-1, WNOHANG))) { return if $pid == 0; # Processes still running next unless delete $pids{$pid}; say "$pid exited with status $?"; } };

      In your parent, just set $pids{$pid} = 1 every time you fork, and the SIGCHLD handler will clean them up. In your parent's main loop, you then know not to exit until %pids is empty. You need to track your pids somehow, if you intend to send signals to them, so you may as well take advantage of that.

        In your parent's main loop, you then know not to exit until %pids is empty.

        Yes, I already have %pid, but "until %pids is empty." means either "sleep()" call or 100% cpu "while(1)" loop. Something like usleep(10_000) while(%pids) will work.

        But this would be just workaround over non-working wait() call.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1047696]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-04-21 04:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (490 votes), past polls