本文介绍了wait4不阻塞父线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个源文件

loop.c中的可执行名称为循环

loop.c which executable name is loop

int main() {
    while(true);
    return 0;
}

和run.c这可执行文件的名称是运行

and run.c which executable name is run

int main() {
    pid_t child_pid = fork();

    int status;
    struct rusage info;

    if (child_pid == 0) {
        ptrace(PTRACE_TRACEME, 0, NULL, NULL);
        execl("loop", "loop", NULL);
        exit(0);
    }

    wait4(child_pid, &status, 0, &info);

    puts("Child exited");
    printf("%ld\n", info.ru_utime.tv_usec);

    return 0;
}

我已经编制既,我已经跑了运行程序。为什么结束的?我读过wait4 ,但事实上并非如此。当我执行 PS 节目循环运行和运行没有(这不是在 PS 和终端似乎完成它是通过给一个输出工作)。

I've compiled both and I've ran run program. Why it terminated? I've read that wait4 suspend, but in fact it does not. When I've executed ps program loop is running and run does not (it's not in ps and terminal seems to finish it's work by giving an output).

我缺少的东西吗?

PS

我用GNU G ++编译器,如果它的事宜。

I used gnu g++ compiler if it's matters.

推荐答案

我想这个问题就在这里。

I suppose the problem is here

ptrace(PTRACE_TRACEME, 0, NULL, NULL);

在这里

wait4(child_pid, &status, 0, &info);

wait4(德$ P $的方式pcated)返回控制,如果进程状态改变。

wait4 (deprecated by the way) return control if process state changed.

ptrace的(PTRACE_TRACEME力送孩子父母SIGTRAP如果一些条件
发生,每次wait4,waitpid函数和相似的功能控制返回给你,
你需要使用WIFEXITED子进程和SIGTRAP条件的出口之间的区别。

ptrace(PTRACE_TRACEME force child send to parent SIGTRAP if some conditionhappen, and every time wait4, waitpid and similar functions return control to you,you need use WIFEXITED to distinguish between exit of child process and sigtrap condition.

您可以通过更换wait4与调用检查我的发言

You can check my statement by replacing wait4 call with:

    if (wait4(child_pid, &status, 0, &info) < 0) {
        perror("wait4 failed");
    } else if (WIFEXITED(status)) {
        printf("process exit\n");
    } else
        printf("child just send signal\n");

这篇关于wait4不阻塞父线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-10 21:28