为什么子进程将无法正常退出?

时间:2018-11-06 06:35:56

标签: c operating-system multiprocessing

为什么以下程序无法正常运行?

int cnt = 0;
void deal(int sig) {
    ++cnt;
}
int main() {
    signal(SIGUSR1, deal);
    pid_t child = fork();
    if (child == 0) {
        ptrace(PTRACE_TRACEME, 0, NULL, NULL);
        raise(SIGSTOP);
        freopen("ab.out", "w", stdout);
        printf("child : %d\n", cnt);
    } else {
        int app_status, app_sig, fd;
        struct user_regs_struct app_reg;
        struct rusage app_ruse;
        waitpid(child, &app_status, 0);
        printf("father: waitpid: %d\n", app_status);
        while (1) {
            ptrace(PTRACE_SYSCALL, child, NULL, NULL);
            wait4(child, &app_status, 0, &app_ruse);
            if (WIFEXITED(app_status)) {
                printf("father: child exit normally, pid - %d\n", child);
                break;
            }
            if (WIFSIGNALED(app_status) || (WIFSTOPPED(app_status)
                                        && WSTOPSIG(app_status) != SIGTRAP)) {
                ....
                break;
            }
            ptrace(PTRACE_GETREGS, child, NULL, &app_reg);
            if (app_reg.orig_rax == SYS_open && (fd = app_reg.rax) >= 0) {
                kill(child, SIGUSR1);
            }
        }
    }
    return 0;
}

运行程序时,得到了跟踪结果

enter image description here

为什么子进程在收到信号后立即退出?

完整的代码可以在这里找到。

https://pastebin.ubuntu.com/p/FddVZpfGH7/

0 个答案:

没有答案