如何检测 Linux 上失效的进程?

发布于 2024-08-27 04:03:29 字数 246 浏览 4 评论 0原文

我有一个用 C 语言编写的父进程和子进程。在父进程的某个地方,HUP 信号被发送到子进程。我希望我的父进程检测孩子是否死亡。但是当我发送 SIGHUP 时,子进程就变成了僵尸。如何检测子进程是否是父进程中的僵尸?我尝试下面的代码,但它没有返回我想要的结果,因为子进程仍然存在,但它已经失效。

kill(childPID, 0);

还有一个问题;我可以在不杀死父母的情况下杀死僵尸孩子吗?

谢谢。

I have a parent and a child process written in C language. Somewhere in the parent process HUP signal is sent to the child. I want my parent process to detect if the child is dead. But when I send SIGHUP, the child process becomes a zombie. How can I detect if the child is a zombie in the parent process? I try the code below, but it doesn't return me the desired result since the child process is still there but it is defunct.

kill(childPID, 0);

One more question; can I kill the zombie child without killing the parent?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡月浅 2024-09-03 04:03:29

来自维基百科

在 Unix 和类 Unix 计算机操作系统上,僵尸进程或失效进程是已完成执行但在进程表中仍有条目的进程。仍然需要此条目来允许启动(现在是僵尸)进程的进程读取其退出状态。

如果父进程通过调用 wait、waitpid 等获取退出状态,则僵尸应该消失。

您可以通过等待函数(man wait)检测进程是否存活。

from wikipedia:

On Unix and Unix-like computer operating systems, a zombie process or defunct process is a process that has completed execution but still has an entry in the process table. This entry is still needed to allow the process that started the (now zombie) process to read its exit status.

If the parent fetches the exit status by calling wait, waitpid or the like, the zombie should disappear.

You can detect whether a process is alive through the wait functions (man wait).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文