c - 僵尸进程在其父进程死亡后会去哪里?

标签 c linux linux-kernel wait zombie-process

Zombie process 是已经执行完毕,但在进程表中仍有一个条目的进程(父进程还没有读取它的退出代码,或者换句话说,它还没有被“收割”)。

孤儿进程是其父进程已结束的进程,尽管它仍在运行(其父进程已“过世”但仍“活着”)。在这种情况下,init 将采用它并等待它。

考虑一下:

int main(int argv, char *argc[]) {

    pid_t p=fork();

    if (p<0) {
        perror("fork");
    }

    // child
    if (p==0) {
        exit(2);
    }

    // parent sleeps for 2 seconds
    sleep(2);
    return 1;
}

这里创建的子进程将成为僵尸 2 秒,但是当父进程结束时它的状态是什么?孤儿僵尸?

它在进程表中的条目会发生什么变化?

“孤儿僵尸”(如上)是否也被 init 收养并被收割?

最佳答案

根据 man 2 wait :

A child that terminates, but has not been waited for becomes a "zombie". The kernel maintains a minimal set of information about the zombie process (PID, termination status, resource usage information) in order to allow the parent to later perform a wait to obtain information about the child. As long as a zombie is not removed from the system via a wait, it will consume a slot in the kernel process table, and if this table fills, it will not be possible to create further processes. If a parent process terminates, then its "zombie" children (if any) are adopted by init(8), which automatically performs a wait to remove the zombies.

当父进程结束时,子进程(即使是僵尸进程)将被init收养。然后,正如您所说,initwait() 以获取其退出状态。

所以,我不认为“孤儿僵尸”是什么特例。

关于c - 僵尸进程在其父进程死亡后会去哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24346126/

相关文章:

module - 具有多个源文件的内核模块中缺少组件

multithreading - Linux 内核模块中 printk 的奇怪行为

linux - 共享标准 C 库是否首先由内核初始化?

c - 使用 C 编程禁用 arduino 微 Controller 上的键盘

c - 函数返回结构错误

linux - AWK命令打印反转

linux - 如何将递归目录复制到平面目录

c - 为什么这个嵌套的 for 循环比展开相同的代码慢得多?

c - 优化我自己的atoi

linux - v4l2 可以用于从同一设备读取音频和视频吗?