我有关于僵尸进程的一些问题
提前致谢
僵尸程序概念有什么好处?
僵尸进程只是一个pid,一个退出状态,以及一些会计信息,直到父母使用系统调用的一个wait
系列来获得其最终状态。 在家长呼叫wait
,孩子的进程ID必须保持标记为已使用状态,以便不能为其分配任何其他进程。 如果另一个进程被分配了一个回收的pid,那么很难区分它和以前有相同pid的进程。 一旦wait
被父母调用并返回最终的退出状态,可以假设没有人再次在该pid处查找孩子,所以现在可以重新使用pid。 (我认为在Linux上,如果父母离开SIGCHLD作为SIG_IGN,内核不会僵尸,但是重新注册SIGCHLD的处置方式不会有相同的效果)
– 知道内核为僵尸进程保留(PID,终止状态,资源使用信息)“资源使用信息”的含义是什么
这些信息中的一部分是运行一个程序的:
time my_program
会报告。 这些值通常在SIGCHLD的siginfo结构中报告(这不是一个wait
的调用),也可以通过调用waitid
形式的系统调用(在某些系统上)获得。 看看关于这个结构的信息man sigaction
。
– 如何僵尸的PPID()= 1,它仍然僵尸,(初始回收僵尸,因为它等待()默认情况下)
一个僵尸的ppid = 1不应该僵持很长时间,因为init应该很快收获。 一个僵尸程序在僵尸死后不久(通过exit
或者一个无用的信号杀死它),直到它的父母的呼叫wait
并获得最后的状态。 这意味着,即使init不做任何事情,但一次又一次地调用init可能会有一个进程可能显示为一个僵尸的时间少量。 如果进程显示为init的子进程(0 = ppid)长时间(秒),那么可能是错误的。
– 任何一个可以写一些C代码来做一个僵尸它的父母是初始化?
这不清楚,但我想你想要:
pid_t f = fork(); if (f > 0) { exit(0); // this is the parent dying, so the child will be an orphan // and get adopted by init } else if (f == 0) { sleep(100); // This is the child doing something that takes enough time for // its parent to commit suicide (exit(0)) and then for you to // observe that it has now been adopted by init exit(0); // And now it dyes as well, so init should reap its status, but // it may be a zombie for a short amount of time first. } else /* error condition would be handled here */
– 僵尸可以拒绝释放内存锁吗?
僵尸不能容纳任何东西。 他们失去了所有的内存页面,打开文件句柄,等等。 几乎所有的操作系统可以弄清楚如何释放应该被释放。 这不是一个错误,但要记住,操作系统必须知道这是应该被释放的东西。 在用户空间创建资源非常容易,当程序死亡时,应该释放操作系统不知道应该释放的资源。
僵尸进程纯粹是一个pid和退出状态值。 由于资源(pid)“属于”父项,因此不能释放pid。 如果它被释放,另一个进程可能会被分配到相同的PID,然后父母可能最终发送信号到一个不相关的进程; 即使家长第一次等待确定孩子是否已经离开,也没有办法避免出现种族问题。
如果你有兴趣看到正在运行的进程列表中的僵尸进程使用这个:
#include <stdio.h> #include <unistd.h> #include <sys/types.h> int main(void) { pid_t p = fork(); // creates child process and stores the returned PID if (p != 0) // executed by parent process { sleep(1); /* the child process terminates while the parent process sleeps, the child then becomes a zombie process because the returned status of the terminated child process isn't checked via a wait() */ system("ps -eo pid,ppid,stat,cmd"); // prints a list of processes in your terminal } else // executed by child process { exit(0); // the child process terminates immediately } return 0; }
您可以通过列表中的Z +来识别僵尸进程:
注意:如果您使用的是Windows,您将不得不修改代码。