fork 100同时处理,有时一些进程变成僵尸

时间:2017-08-02 07:20:28

标签: c++ linux fork zombie-process waitpid

我尝试在以下代码中同时启动100个进程:

int cnt = 0;

void sig_handler(int signo) {
    pid_t pid;
    int stat;
    pid = wait(&stat);
    cout << "cnt:" << ++cnt << ", pid:" << pid << " signal:" << signo << endl;
}

int main() {
    signal(SIGCHLD, sig_handler);
    for (int i = 0; i < 100; ++i) {
        if (fork() == 0) {
            sleep(1);
            exit(0);
        }
    }
    printf("wait\n");
    while (1);
}

我在SIGCHLD中捕获sig_handler信号,结果不同:有时所有进程都返回OK;有时1到4个进程变成了僵尸。

[vinllen@my-host]$ ./a.out
wait
cnt:1, pid:4383 signal:17
cnt:2, pid:4384 signal:17
cnt:3, pid:4385 signal:17
cnt:4, pid:4386 signal:17
cnt:5, pid:4387 signal:17
…
cnt:94, pid:4476 signal:17
cnt:95, pid:4477 signal:17
cnt:96, pid:4478 signal:17
cnt:97, pid:4479 signal:17
cnt:98, pid:4480 signal:17

[vinllen@my-host ~]$ ps aux | grep a.out
Vinllen       4382 96.2  0.0  13896  1084 pts/8    R+   15:14   0:03 ./a.out
Vinllen       4481  0.0  0.0      0     0 pts/8    Z+   15:14   0:00 [a.out] <defunct>
Vinllen       4482  0.0  0.0      0     0 pts/8    Z+   15:14   0:00 [a.out] <defunct>
Vinllen       4493  0.0  0.0 105300   864 pts/9    S+   15:14   0:00 grep a.out

我想原因是不止一个进程同时退出并触发某些东西。谁能告诉我详细的原因并告诉我如何解决这个问题。

在我的理解中,双叉和忽略SIGCHLD是解决这个问题的两种有效方法。但是,如何解决仍在调用wait的代码。

1 个答案:

答案 0 :(得分:4)

信号未排队。如果SIGCHLD在一个挂起时被引发(可能是您的代码在write系统调用中),程序将只收到一个通知。

处理此问题的正确方法是在处理程序中循环,直到收到所有已完成的子项:

void sig_handler(int signo) {
    pid_t pid;
    int stat;
    while ((pid = waitpid(-1, &stat, WNOHANG) > 0)
    if (WIFEXITED(stat))
    {
        // Don't actually do this: you should
        // avoid buffered I/O in signal handlers.
        std::cout << "count:" << ++cnt
                  << ", pid:" << pid
                  << " signal:" << signo
                  << std::endl;
    }
}

如评论中所述,您应该坚持使用信号处理程序中记录的async-signal-safe functions。缓冲I / O(包括使用std::cout)可能存在风险,因为信号处理程序可以在操作其内部结构时调用。避免问题的最佳方法是限制自己使用volatile sig_atomic_t变量与主代码进行通信。