简体   繁体   English

为什么甚至需要 WEXITSTATUS?

[英]Why is WEXITSTATUS even needed?

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?为什么返回码不能只存储在 int 变量中? Why does it have to be converted with the function WEXITSTATUS?为什么必须用 function WEXITSTATUS 转换? What does the value of the unconverted int variable represent?未转换的 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 if exit() was called ( WIFEXITED ), etc. int不仅包含退出代码 - 它还存储有关进程如何终止的信息,例如是否发出信号( WIFSIGNALED )或是否调用了exit()WIFEXITED )等。

The W macros are used to extract the various pieces of information from the int . W宏用于从int中提取各种信息。

status contains not only the return value of the process, but also why the wait(2,3p) call returned (which may not always be normal exiting of the process). status不仅包含进程的返回值,还包含wait(2,3p)调用返回的原因(可能并不总是进程的正常退出)。 The various W*() macros are used to break up the returned value into its constituent pieces.各种W*()宏用于将返回值分解为其组成部分。

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

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