c++ - 强制父进程收割子进程 <defunct>

标签 c++ linux zombie-process

我一直在拼命地试图从父进程中杀死一个子进程。

我试过: 1. kill -15 pid

  1. kill -shotgun pid

  2. 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/

相关文章:

c++ - inotify 多次错误地通知新文件

c++ - 如何自动并行化堆数组?

c++ - 网络正常吗? C++ 软组织/Unix

c - 如何将结构从一个正在执行的程序传递到另一个程序的可执行文件并返回一些字符串

c - 使用 Ctrl+C 停止在前台启动的进程时出现段错误

linux - 如何通过 shell 脚本在 Linux 上杀死已失效的进程

go - 如何在 Go 中编写异步子收割器?

c++ - C++中 vector 和列表的非写成员函数的线程安全

c - 僵尸进程在代码中创建,并在另一部分中被杀死

java -/usr/bin/time 是否包括 java 程序的程序后垃圾收集?