为什么当我将Perl中的退出代码$ ?,在Perl中移位8位时,我预计它会变为-1时是255?为什么在Perl中退出代码255而不是-1?
回答
'wait()'返回的退出状态是一个16位的值。在这16位中,高位8位来自'exit()'返回值的低8位 - 或者从main()
返回的值。如果程序自然死亡,则16的低8位全部为零。如果程序因信号而死亡,则低位8位将对信号编号进行编码,并指示是否发生核心转储。对于一个信号,退出状态被视为零 - 类似shell的程序倾向于将低位非零解释为失败。
15 8 7 0 Bit Position
+-----------------+
| exit | signal |
+-----------------+
大多数机器实际上将32位整数中的16位值存储起来,并且这是用无符号算术处理的。如果进程用'exit(-1)'退出,则16的高阶8位可能全为1,但当向右移位8位时,该进程将显示为255。
如果您确实想将该值转换为带符号的数量,则必须根据第16位做一些位调换。
$status >>= 8;
($status & 0x80) ? -(0x100 - ($status & 0xFF)) : $status;
你在哪个方向移动它?请提供一个代码示例。
也:
perldoc -f system
给出了如何处理$做一个很容易理解的例子吗?
此外,http://www.gnu.org/s/libc/manual/html_node/Exit-Status.html
退出值应255之间0和你的换档与如何负值由计算机实际上是存储应该给一些见解组合。
我正在向右移动8位 – syker 2010-04-28 05:14:27
的Perl中相同的方式为C运行时库的宏WEXITSTATUS
,其具有在wait(2)
下面的描述返回一个子流程退出代码:
WEXITSTATUS(status) evaluates to the least significant eight bits of the return code of the child which terminated, which may have been set as the argument to a call to exit() or as the argument for a return statement in the main program. This macro can only be evaluated if WIFEXITED returned non-zero.
重要这里部分是至少显著八位。这就是为什么你得到255的退出代码perlvar
手册页介绍$?
如下:
$? The status returned by the last pipe close, backtick (‘‘) com- mand, successful call to wait() or waitpid(), or from the sys- tem() operator. This is just the 16-bit status word returned by the 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, if any, the process died from, and "$? & 128" reports whether there was a core dump.
这里没有特殊的处理在退出代码负数。
太棒了! – syker 2010-04-28 05:15:01
- 1. 为什么'os.system'退出代码为1?
- 2. 为什么有些人在出错时退出-1而不是退出1?
- 3. 什么是“1”在Perl源代码中?
- 4. 代码退出状态255
- 5. Taskkill退出代码:255
- 6. 为什么输出下面的代码1而不是0?
- 7. 为什么不能退出此代码?
- 8. FabActUtil.exe退出代码为-1
- 9. “aspnet_compiler.exe”退出,代码为1
- 10. 为什么我成功运行Perl脚本后退出代码1?
- 11. 从Java执行Perl脚本tomcat返回255退出代码
- 12. ILMerge - 用代码255退出的命令
- 13. PHPStorm - 进程完成退出代码255
- 14. 是否有可能用perl处理大于255的退出码?
- 15. perl system退出代码36096
- 16. 为什么会在Perl中的waitpid返回错误的退出代码?
- 17. 为什么这段代码输出是'thread:Thread ..',而不是'runnable:Thread ..'?
- 18. perl - 为什么退出代码0如果错误文件没有找到,我可以让它的代码1?
- 19. 为什么retainCount是0而不是1?
- 20. 为什么为(;;)而不是while(1)?
- 21. ngen build退出代码-1
- 22. JNI_CreateJavaVM退出代码-1?
- 23. csc2.exe退出代码1
- 24. “tsc.exe” 退出,代码1
- 25. JVMterminated退出代码= -1
- 26. 契约退出代码1
- 27. VB复制退出,代码为1
- 28. 错误“aapt.exe”退出,代码为1
- 29. libtool失败,退出代码为1
- 30. vNext建立退出代码为1
也许你可以解释为什么你期望退出代码是-1。 – 2010-04-28 02:25:19
请显示Perl代码。什么程序/脚本'发出'退出代码,哪个脚本报告它? – lexu 2010-04-28 02:27:01
'perl -e“exit -1”; echo $?'=> 255。 – jrockway 2010-04-28 04:29:13