Question

I was reading up on perlvar when i came across this -

The status returned by the last pipe close, backtick (`` ) command, successful call to wait() or waitpid(), or from the system() operator. This is just the 16-bit status word returned by the traditional Unix wait() system call (or else is made up to look like it). Thus, the exit value of the subprocess is really ($?>> 8 ), and $? & 127 gives which signal

What is a 16-bit status word? what does the operation '$?>> 8' signify? and how does a 16-bit word like '512' get converted to '2' after i do '$?>> 8' on it?

Était-ce utile?

La solution

A 16-bit word is merely an amount of memory 16-bits in size. The word "word" implies the CPU can read it from memory with one instruction. (e.g. I worked on a machine that had 64K bytes of memory, but the CPU could only access it as 32K 16-bit words.)

Interpreted as an unsigned integer, an 16-bit word would look like a number between 0 and 216-1 = 65,535, but it's not necessarily an unsigned integer. In the case of $?, it's used to stored three unsigned integers.

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| 15| 14| 13| 12| 11| 10|  9|  8|  7|  6|  5|  4|  3|  2|  1|  0|
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

 \-----------------------------/ \-/ \-------------------------/
            Exit code            core     Signal that killed
            (0..255)            dumped         (0..127)
                                (0..1)

If the OS wants to return "Exited with error code 2", it sets $? to (2 << 8) | (0 << 7) | (0 << 0).

                           +---+---+---+---+---+---+---+---+
                           |                             2 | << 8
                           +---+---+---+---+---+---+---+---+
                                                       +---+
                                                       | 0 | << 7
                                                       +---+
                               +---+---+---+---+---+---+---+
                               |                         0 | << 0
                               +---+---+---+---+---+---+---+

=================================================================

+---+---+---+---+---+---+---+---+
|                             2 |
+---+---+---+---+---+---+---+---+
                                +---+
                                | 0 |
                                +---+
                                    +---+---+---+---+---+---+---+
                                    |                         0 |
                                    +---+---+---+---+---+---+---+

=================================================================

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|                             2 | 0 |                         0 |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

If the OS wants to return "killed by signal 5; core dumped", it sets $? to (0 << 8) | (1 << 7) | (5 << 0).

                           +---+---+---+---+---+---+---+---+
                           |                             0 | << 8
                           +---+---+---+---+---+---+---+---+
                                                       +---+
                                                       | 1 | << 7
                                                       +---+
                               +---+---+---+---+---+---+---+
                               |                         5 | << 0
                               +---+---+---+---+---+---+---+

=================================================================

+---+---+---+---+---+---+---+---+
|                             0 |
+---+---+---+---+---+---+---+---+
                                +---+
                                | 1 |
                                +---+
                                    +---+---+---+---+---+---+---+
                                    |                         5 |
                                    +---+---+---+---+---+---+---+

=================================================================

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|                             0 | 1 |                         5 |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

$? >> 8 is simply doing the reverse operation.

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|                             2 | 0 |                         0 |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

                                                             >> 8

=================================================================

                                +---+---+---+---+---+---+---+---+
                                |                             2 |
                                +---+---+---+---+---+---+---+---+

It returns the number stored in bits 8 and up.

Autres conseils

A 16-bit value is a value that can be stored in sixteen binary bits. In hex that is 0 through to FFFF, or 65,535 in decimal.

A 16-bit status word is the value supplied by the Unix wait call that combines the process's exit status in the left-hand (most-significant) eight bits, a single bit indicating whether a core dump of the terminated process was produced, and the number of the signal, if any, that caused it to terminate in the right-hand (least-significant) seven bits.

Conventionally a zero value for the exit status indicates that the process has been successful, while a non-zero value indicates some sort of failure or informational state.

$? >> 8 indicates shifting the value to the right by eight bits, losing the right-hand (least-significant) eight bits (i.e. the core dump bit and signal number) and leaving the left-hand eight bits (the exit status). This is equivalent to dividing by 28 or 256.

Since $? >> 8 is equivalent to dividing $? by 28 or 256, if $? is 512 then $? >> 8 is 512 / 256, giving an exit status of 2.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top