Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I wait on three child processes?

Tags:

unix

process

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

like image 554
halluc1nati0n Avatar asked Dec 14 '25 12:12

halluc1nati0n


2 Answers

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.

like image 117
wallyk Avatar answered Dec 16 '25 00:12

wallyk


You have to wait three times.

like image 20
Chris Jester-Young Avatar answered Dec 16 '25 00:12

Chris Jester-Young



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!