perl16-bitperlvar

16-bit status word perlvar


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?


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.