简体   繁体   English

Linux,c ++,负退出代码,澄清?

[英]Linux,c++, negative exit codes, clarifiction?

In Linux, c++ can we return exit codes? 在Linux中,c ++可以返回退出代码吗? ie is this code valid: 即此代码有效:

int main()
{
  return -1;
}

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. 我问这个代码是从bash / zsh编译和执行的原因,返回值是255,由于它使用了8位返回代码,我认为这很简单,这意味着只允许返回代码在积极的。

Is this correct? 这个对吗?

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()POSIX本身指定只有给出_exit()的状态的底部8位通常可用:

_exit(status)
The value of status may be 0 , EXIT_SUCCESS , EXIT_FAILURE , or any other value, though only the least significant 8 bits (that is, status & 0377 ) shall be available from wait() and waitpid() ; status的值可以是0EXIT_SUCCESSEXIT_FAILURE或任何其他值,但wait()waitpid()只能使用最低有效8位(即status & 0377 waitpid() ;

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. 虽然它继续说“可以从waitid()获得完整的值”,但在Linux上,我似乎仍然只得到低八位。

NO, exit code passed to exit(2) is merged with data for cases in which your program is interrupted by means of a signal sent to it. 否,传递给exit(2)退出代码与数据合并,用于通过发送给它的信号中断程序的情况。 exit(2) return code is only the least signifiant byte of the code returned by wait(2) . exit(2)返回代码只是 wait(2)返回的代码中最不重要的字节。 See pages for wait(2) and exit(2) for an explanation of how the return code is formed. 有关返回代码如何形成的说明,请参见wait(2)exit(2)页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM