C语言 waitpid、wnohang、wuntraced。我如何使用这些
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33508997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
waitpid, wnohang, wuntraced. How do I use these
提问by 8this
I am a bit confused. As I understand, waitpid with a pid of -1 means that I wait for all child's to finish but if I add an option to the waitpid of WNOHANG, that options says to exit immediately if none have finished...These seems extremely confusing.
我有点困惑。据我了解,pid 为 -1 的 waitpid 意味着我等待所有孩子的完成,但如果我向 WNOHANG 的 waitpid 添加一个选项,则该选项表示如果没有完成则立即退出......这些似乎非常令人困惑。
Why would I tell the computer to wait for child processes to finish and then immediately afterwards tell it to exit immediately if none of the childs have finished?
为什么我要告诉计算机等待子进程完成,然后在没有子进程完成的情况下立即告诉它立即退出?
Can someone explain this option and the WUNTRACED options? I don't know what it means to be traced.
有人可以解释这个选项和 WUNTRACED 选项吗?我不知道被追踪是什么意思。
采纳答案by fuz
If you pass -1and WNOHANG, waitpid()will check if any zombie-children exist. If yes, one of them is reaped and its exit status returned. If not, either 0is returned (if unterminated children exist) or -1is returned (if not) and ERRNOis set to ECHILD(No child processes). This is useful if you want to find out if any of your children recently died without having to wait for one of them to die. It's pretty useful in this regard.
如果你通过-1and WNOHANG,waitpid()将检查是否存在任何僵尸孩子。如果是,则收获其中之一并返回其退出状态。如果不是,0则返回(如果存在未终止的子进程)或-1返回(如果不存在)并ERRNO设置为ECHILD(无子进程)。如果您想知道您的孩子最近是否死亡,而不必等待其中一个死亡,这将非常有用。在这方面它非常有用。
The option WUNTRACEDis documented as below, I have nothing to add to this description:
该选项WUNTRACED记录如下,我没有什么可添加到此描述中:
WUNTRACEDThe status of any child processes specified by pid that are stopped, and whose status has not yet been reported since they stopped, shall also be reported to the requesting process.
WUNTRACEDpid 指定的任何已停止的子进程的状态,以及自停止后其状态尚未报告的状态,也应报告给请求进程。
回答by Jemenake
You usually use WNOHANG and WUNTRACED in different cases.
您通常在不同情况下使用 WNOHANG 和 WUNTRACED。
Case 1: Suppose you have a process which spawns off a bunch of children and needs to do other stuff while the children are running. These children sometimes exit or are killed, but the kernel will hold onto their exit status until some other process claims it via wait() or waitpid(). So, your parent process needs to call wait()/waitpid() on occasion to let the kernel rid itself of the remains of the child. But we don't want wait()/waitpid() to block, because, in this case, our process has other things that it needs to do. We just want to collect the status of a dead process ifthere are any. That's what WNOHANG is for. It prevents wait()/waitpid() from blocking so that your process can go on with other tasks. If a child died, its pid will be returned by wait()/waitpid() and your process can act on that. If nothing died, then the returned pid is 0.
案例 1:假设您有一个进程产生了一堆孩子,并且需要在孩子们运行时做其他事情。这些子进程有时会退出或被杀死,但内核会保持它们的退出状态,直到其他进程通过 wait() 或 waitpid() 声明它为止。因此,您的父进程有时需要调用 wait()/waitpid() 来让内核摆脱子进程的残余。但是我们不希望 wait()/waitpid()阻塞,因为在这种情况下,我们的进程还有其他需要做的事情。我们只想收集一个死进程的状态,如果有任何。这就是 WNOHANG 的用途。它可以防止 wait()/waitpid() 阻塞,以便您的进程可以继续执行其他任务。如果孩子死了,它的 pid 将由 wait()/waitpid() 返回,您的进程可以对此采取行动。如果没有死亡,则返回的 pid 为 0。
Case 2: Suppose your parent process, instead, wants to do nothingwhile children are running. You don't want to just have it do some thumb-twidling for-loop, so you use a normal wait()/waitpid() without WNOHANG. Your process is taken out of the execution queue until one of the children dies. But what if one of your children is stoppedvia a SIGSTOP? Your child is no longer working on the task you have set it to, but the parent is still waiting. So, you've got a deadlock, in a sense, unless the child is continued by some means external to your parent and that child. WUNTRACED allows your parent to be returned from wait()/waitpid() if a child gets stoppedas well as exiting or being killed. This way, your parent has a chance to send it a SIGCONT to continue it, kill it, assign its tasks to another child, whatever.
情况 2:假设您的父进程在子进程运行时不想做任何事情。你不想让它做一些拇指旋转的 for 循环,所以你使用没有 WNOHANG 的普通 wait()/waitpid()。您的进程从执行队列中取出,直到其中一个子进程死亡。但是,如果您的一个孩子通过 SIGSTOP被阻止怎么办?您的孩子不再从事您为其设置的任务,但父母仍在等待。所以,从某种意义上说,你已经陷入了僵局,除非孩子通过某种方式在你的父母和那个孩子之外继续下去。如果孩子被停止,WUNTRACED 允许你的父母从 wait()/waitpid() 返回以及退出或被杀。这样,你的父母有机会向它发送一个 SIGCONT 来继续它,杀死它,将它的任务分配给另一个孩子,等等。

