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

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

by 7stud (Deacon)
on Feb 11, 2010 at 06:10 UTC ( [id://822591]=note: print w/replies, xml ) Need Help??


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

For the if expression, if the exit status is 16 bits, then the value goes from 0 to 655535. What does it mean to check if it's -1?

-1 is not an 'exit status' of a system() call. -1 means system() was unable to execute the specified command. Because the command never executed, it did not generate an exit status.

EDIT: Apparently the -1 return value has some twists to it. The perl system() docs say that system() returns the return value of wait(). The perl wait() docs say:

Behaves like the wait(2) system call on your system: it waits for a child process to terminate and returns the pid of the deceased process, or "−1" if there are no child processes. The status is returned in $? and "${^CHILD_ERROR_NATIVE}". Note that a return value of "−1" could mean that child processes are being automatically reaped, as described in perlipc.

The perlipc docs say:

On most Unix platforms, the CHLD (sometimes also known as CLD ) signal has special behavior with respect to a value of 'IGNORE' .... Calling wait() with $SIG{CHLD} set to 'IGNORE' usually returns -1 on such platforms.

As far as I can tell, if you set $SIG{CHLD} = 'IGNORE', then you have told perl you don't want to wait for any children to finish executing. In that case, system() executes the command, but system() immediately returns -1 because your program is too impatient to wait and get the exit status from the child.

Therefore, system() can return -1 when system() successfully started the command.

For the elsif expression, why are they bitwise anding it with 127? 127 in binary is 0b0111_1111

That checks whether any of the first 7 bits is 'on'.

So, that's only giving me the bottom 7 bits of $?, not 8. Why throw away that top bit in the bottom byte?
Presumably, the agreed upon protocol is to use only the first 7 bits to indicate which signal terminated the program. In the old ascii system, the maximum number of characters that could be represented by one byte was 128 (0-127) because the 8th bit was used for other purposes. Maybe that is why the protocol uses only the first 7 bits to indicate which signal terminated the process.

In addition, I can imagine a system where there were only 2 possible signals. With that protocol, you would only need to write ($? & 3) to determine if a signal terminated the process. Maybe there were only 7 signals when the protocol was decided upon?

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

Replies are listed 'Best First'.
Re^2: return value from system call, exit status, shift right 8, bitwise and, $?
by ikegami (Patriarch) on Feb 11, 2010 at 08:57 UTC

    Therefore, system() can return -1 when system() successfully started the command.

    Yes. It could mean it was unsuccessful in reaping the child. However, that will only happen when $SIG{CHLD} is overridden, and you shouldn't be using system when you do that.

Log In?
Username:
Password:

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

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

    No recent polls found