2

I want my parent process waits for all child processes to finish before continuing, and I have one solution.

int status;

pid_t pid = 0;

int num = 0;


for (int i = 0; i < NUMBER_OF_PROCESSES; i++)
{
    pid = fork();

    if (pid == 0)
    {
        //printf("Hello from Child\n");
        sleep(5 - i);
        printf("Hello from Child %d\n",i + 1);
        num++;
        return 0;
    }
    else if (pid)
    {
        waitpid(pid, &status, 0);
        continue;
    }
    else
    {
        printf("Error\n");
        exit(1);
    }


}

printf("Hello from the process, currentPid : %d, pid : %d\n", getpid(), pid);

return 0;

But it seems that I have to wait each child process before it finish, is there any way can make all child processes have to be able to run in parallel?

1ang
  • 321
  • 2
  • 3
  • 11
  • See this: http://stackoverflow.com/questions/1510922/waiting-for-all-child-processes-before-parent-resumes-execution-unix – Marievi Mar 01 '16 at 12:10

3 Answers3

1

You can launch all your child (and retain their pid), and after, you will use waitpid (see option for waiting any child) in a loop until their is no child left.

That sound good for you ?

Edit :

#define NB_PROCESSES 5

int main(void)
{
    pid_t pidChild[NB_PROCESSES];
    pid_t stoppedChild;
    int   nbChild                = 0;

    printf("Launching all child.\n");
    for (int i = 0; i < NB_PROCESSES; ++i) {
        if ((pidChild[i] = fork()) == -1) {
            printf("Error while fork the %d child : errno = '%s'.\n", i, strerror(errno));
        } else {
            if (pidChild[i] == 0) {
                sleep(NB_PROCESSES - i);
                printf("Hello from Child %d\n",i);
                return (0);
            } else {
                ++nbChild;
            }
        }
    }

    printf("Waiting all child.\n");
    while (nbChild) {
        stoppedChild = waitpid(WAIT_ANY, NULL, 0);
        for (int i = 0; i < NB_PROCESSES; ++i) {
            if (stoppedChild == pidChild[i]) {
                printf("Child %d stopped.\n", i);
            }
        }
        --nbChild;
    }


    printf("Hello from the process, currentPid : %d\n", getpid());

    return (0);
}

You can retain their pid like that.

Tom's
  • 2,448
  • 10
  • 22
1

You just have to launch the processes in a loop and after that, in the original process, to loop on wait until there is no more living child. Like this:

for (int i = 0; i < NUMBER_OF_PROCESSES; i++) {
    pid = fork();   
    if (pid == 0) { // child
        sleep(5 - i);
        printf("Hello from Child %d\n",i + 1);
        num++;
        return 0;
    }
    else if (pid==-1) {
        printf("Error\n");
        break; // out on failure
    }
}
// try to wait for any children while there exists at least one
while ((pid=waitpid(-1,&status,0))!=-1) {
  printf("Process %d terminated\n",pid);
}

So children will live concurrently and the parent will wait their termination.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • I tried to use `waitpid(-1,&status,0);` without the while loop, but it fails, I thought that can wait all child process. I don't know why. – 1ang Mar 01 '16 at 12:49
  • waitpid only waits for one child (undetermined one in advance if first arg equals to -1)... My code works well. – Jean-Baptiste Yunès Mar 01 '16 at 13:40
  • 1
    Good solution @Jean-BaptisteYunès. Just wait up to `wait(2)` returning an error. As exiting not waited processes become zombies, no need to go further. – Luis Colorado Mar 02 '16 at 07:45
-1

Use waitpid

    waitpid(childPid, &returnStatus, 0);  // Parent process waits here for child to terminate.

    if (returnStatus == 0)  // Verify child process terminated without error.  
    {
       std::cout << "The child process terminated normally." << std::endl;    
    }

    if (returnStatus == 1)      
    {
       std::cout << "The child process terminated with an error!." << std::endl;    
    }

Child processes in parallel.It is possible that one child will run quickly and terminate before the next runs, in which case the children are effectively running serially.

for( int n = 0; n < 4; ++n ) {
    switch( fork()) { 
      /* do stuff, but don't wait() or terminate */
    } 
}
FallAndLearn
  • 4,035
  • 1
  • 18
  • 24