16-bit status word perlvar
Asked Answered
K

2

0

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?

Kell answered 26/4, 2013 at 17:48 Comment(3)
The decimal value 512, in binary can be expressed as 1000000000. Shift 1000000000 to the right eight places and you have binary 0000000010, which is decimal 2.Paratroops
possible duplicate of Extracting information from $?Halogen
@Paratroops - thanks, that explains the transformation.Kell
M
4

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.

Menzies answered 26/4, 2013 at 20:19 Comment(0)
T
3

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.

Tace answered 26/4, 2013 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.