我一直在拼命地试图从父进程中杀死一个子进程。
我试过: 1. kill -15 pid
kill -shotgun pid
kill -9 pid
他们都决定将子进程写成: 在 linux 中使用 ps -A 时“defunct”(僵尸)。
如何终止进程并强制将其从进程表中清除。我必须清理它,因为它在进程表中缺少记录是我在代码中验证进程已死的方式。
谢谢:-)
最佳答案
如果你想收集子进程,你必须使用waitpid
请求它的退出代码。从联机帮助页:
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.
用法有点像:
#include <sys/types.h>
#include <sys/wait.h>
...
waitpid(child_pid, 0, 0);
如果你想立即返回,即使 child 还没有退出:
waitpid(child_pid, 0, WNOHANG);
如果您只想收集所有僵尸 child ,而不是查找有关特定 child 的信息,您可以:
waitpid(-1, 0, WNOHANG);
您可以在循环中执行此操作,直到 waitpid
返回负数(即错误)并且 errno 指示 ECHILD。
顺便说一下,这还可以让您真正找出进程的状态,阅读联机帮助页以获取更多信息。
关于c++ - 强制父进程收割子进程 <defunct>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13331570/