gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:04:41 27 4
gpt4 key购买 nike

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/

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