C语言 僵尸进程与孤儿进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20688982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Zombie process vs Orphan process
提问by MarkAWard
A Zombie is created when a parent process does not use the wait system call after a child dies to read its exit status, and an orphan is child process that is reclaimed by init when the original parent process terminates before the child.
当父进程在子进程死亡后不使用 wait 系统调用读取其退出状态时,就会创建一个僵尸进程,而当原始父进程在子进程之前终止时,孤儿是由 init 回收的子进程。
In terms of memory management and the process table how are these processes handled differently, specifically in UNIX?
在内存管理和进程表方面,这些进程的处理方式有何不同,特别是在 UNIX 中?
What is an example or extreme case when the creation of zombies or orphans can be detrimental to the greater application or system?
当僵尸或孤儿的创建可能对更大的应用程序或系统有害时,有什么例子或极端情况?
回答by Rob Napier
When a child exits, some process must waiton it to get its exit code. That exit code is stored in the process table until this happens. The act of reading that exit code is called "reaping" the child. Between the time a child exits and is reaped, it is called a zombie. (The whole nomenclature is a bit gruesome when you think about it; I recommend not thinking about it too much.)
当子进程退出时,必须wait在其上执行某个进程才能获取其退出代码。该退出代码存储在进程表中,直到发生这种情况。阅读退出代码的行为称为“收割”孩子。在孩子退出和收获之间的时间,它被称为僵尸。(当你想到它时,整个命名法有点可怕;我建议不要考虑太多。)
Zombies only occupy space in the process table. They take no memory or CPU. However, the process table is a finite resource, and excessive zombies can fill it, meaning that no other processes can launch. Beyond that, they are bothersome clutter, and should be strongly avoided.
僵尸只占用进程表中的空间。它们不占用内存或 CPU。然而,进程表是一种有限资源,过多的僵尸可以填满它,这意味着没有其他进程可以启动。除此之外,它们是令人烦恼的杂乱,应该强烈避免。
If a process exits with children still running (and doesn't kill its children; the metaphor continues to be bizarre), those children are orphans. Orphaned children are immediately "adopted" by init(actually, I think most people call this "reparenting," but "adoption" seems to carry the metaphor better). An orphan is just a process. It will use whatever resources it uses. It is reasonable to say that it is not an "orphan" at all since it has a parent, but I've heard them called that often.
如果一个进程退出时子进程仍在运行(并且没有杀死它的子进程;这个比喻仍然很奇怪),那么这些子进程就是孤儿。孤儿立即被“领养” init(实际上,我认为大多数人称之为“重新养育”,但“领养”似乎更适合比喻)。孤儿只是一个过程。它将使用它使用的任何资源。可以说它根本不是“孤儿”,因为它有父母,但我经常听到他们这样称呼。
initautomatically reaps its children (adopted or otherwise). So if you exit without cleaning up your children, then they will not become zombies (at least not for more than a moment).
init自动收获它的孩子(领养或其他)。所以如果你不清理你的孩子就退出,那么他们就不会变成僵尸(至少不会超过片刻)。
But long-lived zombies exist. What are they? They're the former children of an existing process that hasn't reaped them. The process may be hung. Or it may be poorly written and forgets to reap its children. Or maybe it's overloaded and hasn't gotten around to it. Or whatever. But for some reason, the parent process continues to exist (so they aren't orphans), and they haven't been waited on, so they live on as zombies in the process table.
但是长寿的僵尸是存在的。这些是什么?它们是尚未收获它们的现有流程的前子项。进程可能会挂起。或者它可能写得不好,忘记收获它的孩子。或者它可能已经超载并且没有解决它。管他呢。但是由于某种原因,父进程继续存在(所以它们不是孤立的),并且它们没有被等待,所以它们作为进程表中的僵尸继续存在。
So if you see zombies for longer than a moment, then it means that there is something wrong with the parent process, and something should be done to improve that program.
因此,如果您看到僵尸的时间超过片刻,则意味着父进程有问题,应该采取措施改进该程序。
回答by opticod
When a process terminates, its resources are deallocated by the operating system. However, its entry in the process table must remain there until the parent calls wait(), because the process table contains the process's exit status. A process that has terminated, but whose parent has not yet called wait(), is known as a zombie process. All processes transition to this state when they terminate, but generally they exist as zombies only briefly. Once the parent calls wait(), the process identifier of the zombie process and its entry in the process table are released.
当一个进程终止时,它的资源被操作系统释放。但是,它在进程表中的条目必须保留在那里,直到父进程调用 wait(),因为进程表包含进程的退出状态。已终止但其父进程尚未调用 wait() 的进程称为僵尸进程。所有进程在终止时都会转换到此状态,但通常它们只是短暂地作为僵尸存在。一旦父进程调用wait(),僵尸进程的进程标识符及其在进程表中的条目就会被释放。
Now consider what would happen if a parent did not invoke wait() and instead terminated, thereby leaving its child processes as orphans. Linux and UNIX address this scenario by assigning the init process as the new parent to orphan processes. The init process periodically invokes wait(), thereby allowing the exit status of any orphaned process to be collected and releasing the orphan's process identifier and process-table entry.
现在考虑如果父进程没有调用 wait() 而是终止,从而将其子进程作为孤儿进程会发生什么。Linux 和 UNIX 通过将 init 进程分配为孤儿进程的新父进程来解决这种情况。init 进程定期调用wait(),从而允许收集任何孤立进程的退出状态并释放孤立进程标识符和进程表条目。
Source: Operating System Conceptsby Abraham, Peter, Greg
来源:操作系统概念由亚伯拉罕,彼得·格雷格
回答by Linkon
An orphan processis a computer process whose parent processhas finished or terminated, though it (child process) remains running itself.
A zombie processor defunct process is a process that has completed execution but still has an entry in the process table as its parent process didn't invoke an wait()system call.
一个孤儿进程是计算机的过程,其parent process已完成或终止,尽管它(子进程)仍然运行本身。
一个僵尸进程或解散的过程已完成执行,但仍然在进程表中的父进程没有调用条目的进程wait()的系统调用。
回答by alk
There are no orphans but the process using PID 1.
From the running process' point of view it makes no difference whether it was started directly and therefore has PID 1 as parent or got inherited by PID 1 because its original parent (being different from PID 1) ended. It is handled like any other process.
Each process goes through some sort of zombie state, when ending, namely the phase between announcing its end by issuing
SIGCHLDand having its processing (delivery or ignorance) acknowledged.
没有孤儿,只有使用 PID 1 的进程。
从正在运行的进程的角度来看,它是直接启动并因此将 PID 1 作为父进程还是由于其原始父进程(与 PID 1 不同)结束而被 PID 1 继承并没有区别。它的处理方式与任何其他过程一样。
每个进程在结束时都会经历某种僵尸状态,即通过发布宣布结束
SIGCHLD和确认其处理(交付或无知)之间的阶段。
When the zombie state had been entered the process is just an entry in the system's process list.
当进入僵尸状态时,进程只是系统进程列表中的一个条目。
The only significant resource a zombie is exclusively using is a valid PID.
僵尸唯一使用的重要资源是有效的 PID。
回答by omkar
Orphan - Parent exit , Init process becomes the parent of child process. So child is terminated and process table gets deleted by os.
Orphan - Parent exit ,Init 进程成为子进程的父进程。所以子进程被终止,进程表被 os 删除。
Zombie - When the child terminates it gives exit status to parent. Meanwhile time suppose your parent is in sleep state and unable to recive any status from child. Though the child exit but the process occupies space in process table
僵尸 - 当子进程终止时,它会向父进程提供退出状态。同时假设您的父母处于睡眠状态并且无法从孩子那里收到任何状态。子进程虽然退出但是进程占用进程表空间
check out this command in linux ubuntu >>ps -eo pid,ppid,status,cmd
在 linux ubuntu >>ps -eo pid,ppid,status,cmd 中查看此命令
If you found something like defunc at the end i.e your process is zombie and occupying space.
如果你在最后发现类似 defunc 的东西,即你的进程是僵尸并占用空间。
回答by SAURABH DUBEY
A process which has finished the execution but still has the entry in the process table to report to its parent process is known as a zombie process. A process whose parent process no more exists i.e. either finished or terminated without waiting for its child process to terminate is called an orphan process
一个已经完成执行但在进程表中仍有要报告给其父进程的条目的进程被称为僵尸进程。其父进程不再存在的进程,即在不等待其子进程终止的情况下完成或终止的进程称为孤儿进程
回答by Justice Bringer
I would like to add 2 code snippets featuring an orphan and a zombie process. But first, I will post the definition of these processes as stated in the book "Operating System Concepts" by Silberschatz, Galvin and Gagn:
我想添加 2 个具有孤立进程和僵尸进程的代码片段。但首先,我将发布 Silberschatz、Galvin 和 Gagn 在“操作系统概念”一书中所述的这些流程的定义:
If no parent waiting (did not invoke wait()) process is a zombie
If parent terminated without invoking wait , process is an orphan
如果没有父等待(没有调用 wait())进程是僵尸进程
如果父进程在没有调用 wait 的情况下终止,进程是一个孤立进程
Orphan
孤儿
// A C program to demonstrate Orphan Process.
// Parent process finishes execution while the
// child process is running. The child process
// becomes orphan.
#include <stdio.h> //printf
#include <stdlib.h> //exit
#include <sys/types.h> //fork
#include <unistd.h> //fork and sleep
int main()
{
// Fork returns process id
// in parent process
pid_t child_pid = fork();
// Parent process didn't use wait and finished before child
// so the child becomes an orphan process
// Parent process
if (child_pid > 0) {
printf("I finished my execution before my child");
}
else // Child process
if (child_pid == 0) {
sleep(1); //sleep for 1 second
printf("This printf will not be executed");
}
else{
//error occurred
}
return 0;
}
Output
输出
I finished my execution before my child
我在我孩子之前完成了我的死刑
Zombie
僵尸
// A C program to demonstrate Zombie Process.
// Child becomes Zombie as parent is not waiting
// when child process exits.
#include <stdio.h> //printf
#include <stdlib.h> //exit
#include <sys/types.h> //fork
#include <unistd.h> //fork and sleep
int main()
{
// Fork returns process id
// in parent process
pid_t child_pid = fork();
// Parent process didn't use wait
// so the child becomes a zombie process
// Parent process
if (child_pid > 0){
sleep(1); //sleep for 1 second
printf("\nI don't wait for my child");
}
else // Child process
if(child_pid == 0){
printf("My parent doesn't wait me");
exit(0);
}
else{
//error occurred
}
return 0;
}
Output
输出
My parent doesn't wait me
I don't wait for my child
我的父母不等我
我不等我的孩子
Edit: Source and inspiration taken from here
编辑:从这里获取的来源和灵感

