16位状态字perlvar

时间:2013-04-26 17:48:41

标签: perl 16-bit perlvar

当我遇到这个时,我正在阅读perlvar -

  

最后一个管道关闭,反引号(``)命令,成功调用wait()或waitpid()或来自system()运算符返回的状态。这只是传统Unix wait()系统调用返回的16位状态字(或者看起来像是这样)。因此,子流程的退出值实际上是($?>> 8),而$? &安培; 127给出了哪个信号

什么是16位状态字?什么操作'$?>> 8'表示?在我做'$?>>之后,像'512'这样的16位字如何转换为'2'? 8'就可以了?

2 个答案:

答案 0 :(得分:5)

16位字仅仅是16位大小的存储量。 “word”一词意味着CPU可以通过一条指令从内存中读取它。 (例如,我在一台具有64K字节内存的机器上工作,但CPU只能以32K 16位字的形式访问它。)

解释为无符号整数,16位字看起来像0到2之间的数字 16 -1 = 65,535,但它不一定是无符号整数。在$?的情况下,它用于存储三个无符号整数。

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| 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)

如果操作系统想要返回“退出错误代码2”,它会设置$?到(2 << 8) | (0 << 7) | (0 << 0)

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

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

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

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

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

如果操作系统想要返回“被信号5杀死;核心被丢弃”,它会设置$?到(0 << 8) | (1 << 7) | (5 << 0)

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

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

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

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

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

$? >> 8只是在进行相反的操作。

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

                                                             >> 8

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

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

它返回存储在第8位及以上的数字。

答案 1 :(得分:3)

16位值是可以存储在16个二进制位中的值。以十六进制为0FFFF,或十进制为65,535。

16位状态字是Unix wait调用提供的值,它将进程的退出状态组合在左侧(最高有效)的8位中,单个位指示核心转储是否为产生了终止的进程,以及导致它以右手(最不重要)7位终止的信号的数量(如果有的话)。

传统上,退出状态的零值表示该过程已成功,而非零值表示某种失败或信息状态。

$? >> 8表示将值向右移位8位,丢失右侧(最低有效)8位(即核心转储位和信号编号)并保留左侧8位(退出状态)。这相当于除以2 8 或256。

由于$? >> 8相当于将$?除以2 8 或256,如果$?为512,则$? >> 8为512/256,退出状态为2。