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

3 回答 3

1

我不知道。

但是你可以让你的孩子“异常”地死去。在孩子身上杀死(getpid())?

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

从文档中的单词的声音来看,我会说你做得对。

于 2010-03-29T12:39:54.907 回答
1

是的,你是对的——如果wait成功,那么要么是真的,WIFEXITED()要么WIFSIGNALED()是真的。

于 2010-03-29T12:46:40.480 回答
1

是的。 POSIX.1状态:

如果 stat_loc 指向的信息是通过调用未指定 WUNTRACED 或 WCONTINUED 标志的 waitpid() 或通过调用 wait() 函数存储的,则正好是宏 WIFEXITED(*stat_loc) 和 WIFSIGNALED( *stat_loc) 应评估为非零值。

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

于 2010-03-29T17:09:22.160 回答