Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Process exit status

by Dirk80 (Pilgrim)
on Jul 22, 2011 at 11:15 UTC ( [id://916099]=perlquestion: print w/replies, xml ) Need Help??

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

Hello wise monks,

I want to check the process exit status of a child program (child.pl) from my parent program. (parent.pl)

parent.pl:

my $exit_status = system("child.pl"); print "Exit Status: $exit_status\n";

child.pl:

exit(2);

The strange thing is that in the parent program the process exit code seems to be multiplicated with 256.

So in this example the printed process exit code is 512 and not 2 as I would have expected.

If I change the parent program as follows, then I get the desired result:

parent_changed.pl:
my $exit_status = system("child.pl"); $exit_status /= 256; print "Exit Status: $exit_status\n";

My question is: Why is the process exit code, when I get in the parent program a multiple of 256 of the real process exit code of the child program?

Thank you.

Greetings,

Dirk

Replies are listed 'Best First'.
Re: Process exit status
by Anonymous Monk on Jul 22, 2011 at 11:23 UTC

    The strange thing ... Why is the process exit code, when I get in the parent program a multiple of 256 of the real process exit code of the child program?

    Because that is how system is documented to work

    See also IPC::System::Simple

      $ perl -le " print system( qw[ perl -e exit(2) ] )" 512 $ perl -le " print system( qw[ perl -e exit(2) ] ) >> 8" 2 $ perl -MIPC::System::Simple=system -le " system( qw[ perl -e exit(2) + ] ) " "perl" unexpectedly returned exit value 2 at -e line 1 $ perl -Mautodie=system -le " system( q[perl -e exit(2) ] ) " "perl -e exit(2) " unexpectedly returned exit value 2 at (eval 10) lin +e 13 at -e line 1
Re: Process exit status
by jethro (Monsignor) on Jul 22, 2011 at 11:29 UTC
    Because the lower 8 bits are used to indicate the child died instead of exiting normally. See system
Re: Process exit status
by JavaFan (Canon) on Jul 22, 2011 at 12:04 UTC
    Ok, I got to ask.

    Why did you think posting this question gave you a quicker result than reading the output of perdoc -f system? It's mentioned in the third paragraph (which for many people will show up on the first screen of output), with a pointer to a more detailed explanation further down.

      Yes, you're right. It's mentioned in the docs of system. But it still does not help me to understand why the process exit code is shifted and why not only one byte is used although it would fit into one byte. I don't understand the why.

      I wanted to have background information of you. Of course I can do the reading of the docs myself. Perhaps the formulation of my question (using the words unexpected and strange) was leading to the assumption that I did not read the docs of system before posting here a question.

        Perhaps the formulation of my question (using the words unexpected and strange) was leading to the assumption that I did not read the docs of system before posting here a question.
        It certainly did. Specially since the documentation says that the lower 8 bits don't have to be 0 - they are non-zero if the program exits because of receiving a signal, and/or dumped core. Given this, dividing the exit code by 256 is wrong, as Perl doesn't do integer arithmetic by default. The documentation says one should shift 8 bits - which you don't do. So, not only does the phrasing of your question suggests you haven't read the documentation, your code does the same.
Re: Process exit status
by zentara (Archbishop) on Jul 22, 2011 at 12:47 UTC

      Thank you. This post lead me to the following website, which could answer my question.

      http://perldoc.perl.org/perlfaq8.html#Why-can't-I-get-the-output-of-a-command-with-system()%3f

      system runs a command and returns exit status information (as a 16 bit value: the low 7 bits are the signal the process died from, if any, and the high 8 bits are the actual exit value).

      The answer to my original question: 16 bits are needed instead of 8 bits because also the signal id which killed the process has to be stored.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://916099]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found