我在孩子中以-1退出,但是父母选择了255。有没有办法让父母识别-1而不是?这是我的代码
pid = fork();
// if fork fails
if (pid < 0){
exit(EXIT_FAILURE);
}
else if (pid == 0){
// purposely exit
_exit(-1);
}
else{
int status;
int corpse = wait(&status);
if (WIFEXITED(status))
{
int estat = WEXITSTATUS(status);
if (estat == 0){
printf("Command was successfully executed\n");
}
else{
printf("Error: child exited with status %d\n", estat);
}
}
else
printf("signalled\n", corpse);
答案 0 :(得分:1)
来自wait
:
WEXITSTATUS(status)
返回子项的退出状态。这包括 孩子的状态参数的最低有效8位 在调用exit(3)或_exit(2)或作为参数时指定 对于main()中的return语句。这个宏应该是 仅在WIFEXITED返回true时使用。
-1
的最低8位是0xFF
(255
)。
答案 1 :(得分:0)
从函数wait
返回的状态不仅包含子进程的退出代码的信息,还包含终止原因等。因此,不可能得到子进程的确切退出代码,只表示子进程代码的至少8位。
答案 2 :(得分:0)
状态将为0xFF00。