如何等待三个子进程?
我正在尝试从父进程派生 3 个不同的子进程(并在 UNIX 机器上运行它),并且我希望满足以下要求:
父进程必须等到所有 3 个子进程都完成执行。
我使用 wait
进行相同的操作..这是代码片段:
#include <unistd.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
int stat;
/* ... */
最后,在父级中,我这样做:
wait (&stat);
/* ... */
return 0;
}
问题:
我需要调用 wait
三次还是一次调用就足够了? 我需要知道这是如何工作的..
I'm trying to fork 3 different child processes from a parent (and running this on a UNIX box), and I want to have this requirement :
The parent must wait till all the 3 children processes have finished executing.
I'm using wait
for the same .. Here's the code snippet :
#include <unistd.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
int stat;
/* ... */
At last, in the parent, I do this :
wait (&stat);
/* ... */
return 0;
}
Question :
Do I need to call wait
thrice or does a single call suffice?
I need to know how this works..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须发出三个等待。每个
wait
都会阻塞直到子进程退出,或者如果子进程已经退出则不会阻塞。请参阅等待。You have to issue three waits. Each
wait
blocks until a child exits or doesn't block if a child has already exited. See wait.你必须等三遍。
You have to wait three times.
旁注:如果您不想阻止等待每个进程依次终止,您可以安装 SIGCHLD,然后在知道准备就绪后调用 wait() 来收集返回代码。
Side note: If you don't want to block waiting for each to terminate in turn, you can instead install a signal handler for SIGCHLD and then call wait() to collect the return code once you know it is ready.