Linux,C ++,否定退出代码,说明? [英] Linux,c++, negative exit codes, clarifiction?
问题描述
在Linux中,我们可以返回c ++退出代码吗?即该代码是否有效:
In Linux, c++ can we return exit codes? i.e is this code valid:
int main()
{
return -1;
}
我要询问是否从bash/zsh编译并执行此代码的原因是,返回值为255,由于使用8位返回码这一事实,所以我将其稀疏,这意味着返回码是只能在正数之间.
The reason I am asking if this code is compiled and executed from bash/zsh, the return value would be 255, I thin this due to the fact that its using an 8 bit return code, and that implies that return codes are only allowed to be between positive.
这正确吗?
推荐答案
The relevant system call is _exit()
, and POSIX itself specifies that only the bottom 8 bits of the status given to _exit()
are normally usable:
_exit(状态)
状态值可以是0
,EXIT_SUCCESS
,EXIT_FAILURE
或任何其他值,尽管只有最低有效8位(即status& 0377
)应可从wait()
和waitpid()
获得;
_exit(status)
The value of status may be0
,EXIT_SUCCESS
,EXIT_FAILURE
, or any other value, though only the least significant 8 bits (that is,status & 0377
) shall be available fromwait()
andwaitpid()
;
尽管继续说应该从 waitid()
中获得全部值",但是在Linux上,我似乎仍然只得到低8位.
Though it goes on to say that "the full value shall be available from waitid()
", but on Linux, I seem to still get just the low eight bits.
这篇关于Linux,C ++,否定退出代码,说明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!