Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: return value from system call, exit status, shift right 8, bitwise and, $?

by ikegami (Patriarch)
on Feb 11, 2010 at 09:10 UTC ( [id://822610]=note: print w/replies, xml ) Need Help??


in reply to return value from system call, exit status, shift right 8, bitwise and, $?

and the low 8 are the signal id. Is that correct?

No, the low 7 bits indicate the signal that ended the process. Bit 7 indicates whether a core dump was saved.

What does it mean to check if it's -1?

system itself encountered an error. For example, system will return -1 and set $! if the program to execute is not found.

or would [system(...) == 0 or die "Failed!";] suffice?

It can also be written as

system(...) and die "Failed!";

It will die on any error, but not saying what failed and not saying why it failed seems insufficient to me. The user should at least be told what failed for an error that's not a programming error; a line number doesn't cut it.

Now, detecting whether a code dump occurred or not is going overboard.

This is pretty minimal:

sub _system { my $prog = shift; my $rv = system( { $prog } $prog => @_ ); if ($rv == -1) { die("Can't launch $prog: $!\n"); } elsif (my $s = $rv & 127) { die("$prog died from signal $s\n"); } elsif (my $e = $rv >> 8) { die("$prog exited with code $e\n"); } }

Replies are listed 'Best First'.
Re^2: return value from system call, exit status, shift right 8, bitwise and, $?
by Anonymous Monk on Feb 11, 2010 at 15:03 UTC

    Ok. I think I see now. If the system() call returns "-1", this is the same as a binary number of all 1's (since the system presumably uses the two's-complement representation), and it looks like it is:

    printf '%b', -1; print "\n"; # Output is: # 11111111111111111111111111111111

    So, if we get -1 back, then I can just stop checking bits right there.

    If the system() call does not return -1, but returns anything at all in those first 7 bits, then -- again -- I can stop checking anything else, because if those first 7 bits contain anything, that indicates that the program that was called received a signal and that's why it ended.

    Finally, if the system() call does not return -1, and those first 7 bits are all zeros, then only at that point do those next 8 bits contain anything of interest (the exit status of the program called).

    And to wrap this all up nicely, if the system() call returns 0, then that means all bits are off, thus:

    1. the value is not -1, so the program it called at least launched,
    2. the lower 8 bits are all zeros, so there was no signal caught (nor was a core dump saved),
    3. the higher 8 bits are all zeros, so exit status was zero too.

    and therefore the program that system() called evidently ran successfully.

    Thank you!

Re^2: return value from system call, exit status, shift right 8, bitwise and, $?
by 7stud (Deacon) on Feb 11, 2010 at 10:18 UTC
    and the low 8 are the signal id. Is that correct?
    No, the low 7 bits indicate the signal that ended the process. Bit 7 indicates whether a core dump was saved.

    By my math, the 8th bit signals the core dump. I assume that's a typo.

      No typo. The 8th bit is bit 7. They're numbered from 0

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-25 08:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found