gpt4 book ai didi

wait() 后检查状态

转载 作者:太空宇宙 更新时间:2023-11-04 07:42:22 25 4
gpt4 key购买 nike

创建子进程并立即退出(_exit())后,我想执行等待并检查状态。现在我想知道是否在 if/else 构造的“else”分支中我还需要检查 WIFSIGNALED。据我了解,如果我执行等待,a) 可能会发生错误 (-1),子进程可能已被(exit() 或 _exit())正常终止,或者它可能已被信号,所以检查可以省略,对吧?

//remainder omitted

int status;

pid_t t_pid = wait(&status);

if (t_pid == -1) {
perror("wait");
exit(EXIT_FAILURE);
}

if (WIFEXITED(status)) {
printf("child terminated normally, status = %d\n",
WEXITSTATUS(status)
);
} else { // <-- do it have to check for WIFSIGNALED() here?
printf("child was terminated by a signal, signum = %d\n",
WTERMSIG(status)
);
}

最佳答案

我不知道。

但是你可以让你的 child “非正常”死去。 kill(getpid()) 在 child 身上?

http://publib.boulder.ibm.com/infocenter/tpfhelp/current/index.jsp?topic=/com.ibm.ztpf-ztpfdf.doc_put.cur/gtpc2/cpp_wifsignaled.html

从文档中的文字来看,我会说你做的是正确的。

关于wait() 后检查状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2537735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com