Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: $? is -1???

by dga (Hermit)
on Jun 21, 2004 at 13:39 UTC ( #368435=note: print w/replies, xml ) Need Help??

in reply to $? is -1???

waitpid returns -1 'if there is no such child process'. If you call it with none of the children still living, this seems like a reasonable expectation.

Unless it has changed a lot in 5.8 the old advice was to 'Do as little as possible in your signal handler, like writing a status into an already defined and allocated variable' or 'Pretend like the signal stuff is reentrant but be aware that it may not always work that way.' I know the p5p people are working on solid fully reentrant signals but I do not know the progress toward that goal in 5.8.

How about preparing a hash for the children when they are forked and then saving the exit status uninterpreted into that data space?

while((my $pid = waitpid(-1, &WNOHANG)) > 0) { $children{$pid}=$?; # $children--; # pushing your luck # print $children children running\n"; # really pushing }

I think having a print in a signal handler may be really too much time spent away waiting to be interupped by another signal. If you decremented the variable but did not print then maybe the parent could run a polling loop say once a second and just print out the $children variable which you would have to arrange so that the signal handler knows about the same $children as the parent, maybe with a file lexical or something. This would get the print out of the handler and the print of the parent will probably skip numbers as more than 1 child exits during a second but it should kind of keep up. Also you could count the number of children with statuses in the hash and subtract that from the total children to get the count which would make the handler only update the children hash.

Replies are listed 'Best First'.
Re^2: $? is -1???
by kscaldef (Pilgrim) on Jun 21, 2004 at 15:52 UTC

    I've cut the handler down as much as I think I can to just:

    while ((my $pid = waitpid(-1, &WNOHANG)) > 0) { $children--; # need this to tell when we're done $exits{$pid} = $?; # need this to see the problem }

    then I dump %exits at the end of the script. However, I still see output like:

    18106 0
    18116 0
    18113 -1
    18105 0

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2021-06-14 21:27 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (66 votes). Check out past polls.