gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:09:49 25 4
gpt4 key购买 nike

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

孤儿进程是指其父进程已完成但仍在运行的进程(其父进程已“去世”但仍“活着”)。在这种情况下,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 秒,但是当父进程完成时它的状态是什么?孤儿僵尸?

进程表中的条目会发生什么情况?

“orphan-zombies”(如上面的)是否也被 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/59012518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com