当前位置: 动力学知识库 > 问答 > 编程问答 >

unix - How do I wait on three child processes?

问题描述:

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

网友答案:

You have to wait three times.

网友答案:

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.

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: