0

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

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

什么是 16 位状态字?'$?>> 8' 操作是什么意思?在我对它执行 '$?>> 8' 之后,如何将像 '512' 这样的 16 位字转换为 '2'?

4

2 回答 2

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)

如果操作系统想要返回“Exited with error code 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 位及更高位中的数字。

于 2013-04-26T20:19:53.870 回答
3

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

16 位状态字是 Unixwait调用提供的值,它将进程的退出状态组合在左侧(最重要的)八位中,一位指示是否产生了终止进程的核心转储,以及信号的编号,如果有的话,导致它以右手(最低有效)七位终止。

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

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

因为$? >> 8相当于除以$?2 8或 256,如果$?是 512 那么$? >> 8是 512 / 256,退出状态为 2。

于 2013-04-26T18:49:27.020 回答