如何检查是否有PID X中的过程是僵尸? [英] How to check if a process with a pid X is a zombie?

查看:104
本文介绍了如何检查是否有PID X中的过程是僵尸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个进程的PID,我需要检查它是否是使用POSIX系统僵尸在C调用我应该怎样做呢?

I got the PID of a process and I need to check if it is a zombie using POSIX system calls in C. How should I do that?

我遇到的问题是,我有一个过程,它叉成许多孩子,孩子都这样做高管,有时我想做的事情在后台EXEC所以我真的不能等待()是要走的孩子在后台。我可以(在我的主循环一次)等待()定期,但我需要知道哪些进程是僵尸因此在等待那些不会很快结束孩子爸爸过程中不挂起。

The problem I'm having is that I have a process and it forks into many children, the children all do execs and sometimes I want to do the exec in background so I can't really wait() the children that go in background. I could wait() periodically (once in my main loop) but I need to know which processes are zombie so my father process doesn't hang while waiting for children that are not going to end soon.

如果你很好奇,我建立一个UNIX外壳,它是在外壳的性质有孩子异步处理行为。

If you are curious, I'm building a unix shell and it is in the shell nature to have the children processes behaving asynchronously.

推荐答案

您不能检查过程是否与POSIX纯僵尸呼叫 - 除非你是父进程,并配有收获它的 家庭电话。所以,你必须找到一个好地方,等待孩子。

You can't check whether a process is a zombie with pure POSIX calls - except where you're the parent process, and reaping it with a wait family call. So you'll have to find a good place to wait for the child.

一种选择是设置一个 SIGCHLD 处理程序,并做了 waitpid函数(0,&安培;状态,WNOHANG)那里。一定要循环,直到不再发现任何进程 - 如果两个孩子很短的时间间隔内死了,你只能得到一个 SIGCHLD

One option is to set a SIGCHLD handler and do a waitpid(0, &status, WNOHANG) there. Be sure to loop until it no longer finds any processes - if two children die within a short interval, you may only get one SIGCHLD.

另一个选择是双叉 - 即叉(),将孩子再次(称之为子A)叉,有第二个孩子(孩子B) EXEC。然后孩子立即退出。同时,家长的wait() ING儿童答:一旦孩子走了,该系统的init进程会照顾孩子收获B当它最终死亡的。这种方法比较简单,但是当Child B级死你的shell将没有办法知道,所以如果你需要的信息,使用前一种方法。

Another option is to double fork - that is, fork(), have the child (call it child A) fork again, have the second child (child B) exec. Then child A immediately exits. Meanwhile the parent is wait()ing for child A. Once child A is gone, the system's init process will take care of reaping child B when it eventually dies. This method is simpler, but your shell will have no way of knowing when child B dies, so if you need that information, use the former method.

这篇关于如何检查是否有PID X中的过程是僵尸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆