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

Dear Monks, I have a process that forks into a parent that runs a monitor and a child that again runs another grandchild process. The grandchild process can terminate with 0 or non-zero exit code, which I can receive in the forked process, and return to the parent. But if the grandchild terminated due to a TERM signal, the exit code seen by the parent monitor is zero.
elsif ($pid == 0) { # .... my $exitstatus = ... my $returnvalue = $exitstatus >> 8; my $signalled = ($exitstatus & 127); print "status=$exitstatus, signal=$signalled\n"; return($returnvalue); }
In the above, I would like to return the full $exitstatus so the monitor can know the reason the grandchild quit. One possibility, although I am unsure how unconventional, is to return ($returnvalue | $signalled). Any help is greatly appreciated.

Replies are listed 'Best First'.
Re: Exit codes and signals
by kennethk (Abbot) on Feb 06, 2016 at 15:14 UTC
    Do you mean exit $returnvalue;, or am I missing something?

    exit


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      yes, exit($returnvalue). The point being that I want to inform the monitor (gp) that the whole thing stopped early due to signal as opposed to normal termination indicate by exit(0).
Re: Exit codes and signals
by ikegami (Patriarch) on Feb 07, 2016 at 05:25 UTC
    bash does
    exit($? & 0x7F ? ($? & 0x7F) | 0x80 : $? >> 8);
Re: Exit codes and signals
by Anonymous Monk on Feb 06, 2016 at 15:39 UTC
    One possibility, although I am unsure how unconventional, is to return ($returnvalue | $signalled).
    bash, for instance, returns 128+n if the command is terminated by signal n. It works well enough in practice... otoh, $returnvalue | $signalled is probably less useful:
    $ perl -mPOSIX -E 'say for POSIX::SIGHUP, POSIX::EXIT_FAILURE' 1 1

    Another possiblity is to use a more sophisticated form of IPC, some kind of message queue, perhaps (but that's maybe overkill in this case).