linux之基于信号解决僵尸进程的写法

#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>

void sigchld_handler(int signo) {
    // 回收所有退出的子进程
    while (waitpid(-1, NULL, WNOHANG) > 0);
}

int main() {
    signal(SIGCHLD, sigchld_handler);
    for (int i = 0; i < 5; ++i) {
        if (fork() == 0) {
            sleep(1);
            _exit(0);
        }
    }
    sleep(10);
    return 0;
}
posted @ 2025-11-24 18:17  我不是萧海哇~~~  阅读(4)  评论(0)    收藏  举报