【问题标题】:Checking status after wait()wait() 后检查状态
【发布时间】:2010-03-29 12:19:52
【问题描述】:

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

【问题讨论】:

    标签: c unix systems-programming


    【解决方案1】:

    我不知道。

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

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

    从文档中的话我会说你做对了。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        是的。 POSIX.1 状态:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多