【问题标题】:Parent process waits for all child processes to finish before continuing父进程等待所有子进程完成后再继续
【发布时间】:2016-06-13 20:18:32
【问题描述】:

我希望我的父进程在继续之前等待所有子进程完成,我有一个解决方案。

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;

但似乎我必须等待每个子进程完成之前,有什么办法可以让所有子进程都必须能够并行运行?

【问题讨论】:

标签: c linux process


【解决方案1】:

使用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;    
    }

子进程并行。有可能一个子进程会快速运行并在下一次运行之前终止,在这种情况下,子进程实际上是串行运行。

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

【讨论】:

    【解决方案2】:

    您可以启动所有孩子(并保留他们的 pid),然后,您将循环使用 waitpid(请参阅等待任何孩子的选项),直到他们没有孩子为止。

    你觉得好听吗?

    编辑:

    #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);
    }
    

    你可以这样保留他们的 pid。

    【讨论】:

      【解决方案3】:

      您只需在循环中启动这些进程,然后在原始进程中循环等待,直到没有更多活着的孩子。像这样:

      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);
      }
      

      所以孩子们将同时生活,而父母将等待他们的终止。

      【讨论】:

      • 我尝试在没有while循环的情况下使用waitpid(-1,&amp;status,0);,但它失败了,我认为可以等待所有子进程。我不知道为什么。
      • waitpid 只等待一个孩子(如果第一个 arg 等于 -1,则提前确定一个)...我的代码运行良好。
      • 很好的解决方案@Jean-BaptisteYunès。只需等待 wait(2) 返回错误即可。由于退出的未等待进程成为僵尸,因此无需进一步。
      猜你喜欢
      • 1970-01-01
      • 2013-04-25
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多