wait() 之后检查状态

发布于 2024-08-26 13:21:06 字数 603 浏览 4 评论 0 原文

创建子进程并立即退出(_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)
    );
}

After creating a child process and exiting it immediately (_exit()), I want to perform a wait and check the status. Now I wonder if in the 'else' branch of the if/else construct I also need to check for WIFSIGNALED. As far as I understand, if I perform a wait, a) an error could have occured (-1), the child could have terminated normally by an (exit() or _exit()), or it could have been terminated by a signal, so the check could be omitted, right?

//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)
    );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝對不後悔。 2024-09-02 13:21:06

我不知道。

但你可以让你的孩子“非正常”死亡。杀死(getpid())孩子?

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

从文档中单词的发音来看,我想说你做得正确。

I don't know.

But you could make your child die "abnormally". kill(getpid()) in the child?

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

From the sound of the words in the docs I'd say you are doing it correctly.

琉璃梦幻 2024-09-02 13:21:06

是的,您是对的 - 如果 wait 成功,则 WIFEXITED()WIFSIGNALED() 将为 true。

Yes, you are correct - if wait succeeded then either WIFEXITED() or WIFSIGNALED() will be true.

分分钟 2024-09-02 13:21:06

是的。 POSIX.1 指出:

如果信息指向
stat_loc 是通过调用存储的
waitpid() 没有指定
WUNTRACED 或 WCONTINUED 标志,或通过
确切地说,调用 wait() 函数
宏 WIFEXITED(*stat_loc) 之一
和 WIFSIGNALED(*stat_loc) 应
评估为非零值。

waitpid() 与 WUNTRACED 或 WCONTINUED 一起使用,可以得到WIFSTOPPED(*stat_loc) 或 WIFCONTINUED(*stat_loc) 为 true 的状态。

Yes. POSIX.1 states:

If the information pointed to by
stat_loc was stored by a call to
waitpid() that did not specify the
WUNTRACED or WCONTINUED flags, or by a
call to the wait() function, exactly
one of the macros WIFEXITED(*stat_loc)
and WIFSIGNALED(*stat_loc) shall
evaluate to a non-zero value.

Using waitpid() with WUNTRACED or WCONTINUED, it is possible to get a status where WIFSTOPPED(*stat_loc) or WIFCONTINUED(*stat_loc) is true instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文