为什么需要 WEXITSTATUS?
以下代码将等待子进程完成,然后打印其返回代码。
int status;
wait(&status);
cout << "return code = " << WEXITSTATUS(status) << endl;
为什么返回码不能直接存储在 int 变量中?为什么要用函数WEXITSTATUS来转换呢?未转换的int变量的值代表什么?
The following code will wait for a child process to finish and then print its return code.
int status;
wait(&status);
cout << "return code = " << WEXITSTATUS(status) << endl;
Why can't the return code just be stored in the int variable? Why does it have to be converted with the function WEXITSTATUS? What does the value of the unconverted int variable represent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
int
不仅仅保存退出代码 - 它还存储有关进程如何终止的信息,例如是否发出信号 (WIFSIGNALED
) 或如果exit( )
被称为 (WIFEXITED
),等等。W
宏用于从int
中提取各种信息。The
int
holds more than just the exit code - it also stores information about how the process terminated, for example if it was signalled (WIFSIGNALED
) or ifexit()
was called (WIFEXITED
), etc.The
W
macros are used to extract the various pieces of information from theint
.status
不仅包含进程的返回值,还包含 为什么wait(2,3p)
调用返回(这可能并不总是进程正常退出)。各种W*()
宏用于将返回值分解为其组成部分。status
contains not only the return value of the process, but also why thewait(2,3p)
call returned (which may not always be normal exiting of the process). The variousW*()
macros are used to break up the returned value into its constituent pieces.