web-dev-qa-db-ja.com

すべての子プロセスが終了するまで親を待機させる方法は?

親がALL子プロセスが終了するのを待ってからフォークの後に続行する方法について、誰かが光を当てることを望んでいます。実行したいクリーンアップコードがありますが、子プロセスはこれが発生する前に戻る必要があります。

for (int id=0; id<n; id++) {
  if (fork()==0) {
    // Child
    exit(0);      
  } else {
    // Parent
    ...
  }
  ...
}
39
Donatello
pid_t child_pid, wpid;
int status = 0;

//Father code (before child processes start)

for (int id=0; id<n; id++) {
    if ((child_pid = fork()) == 0) {
        //child code
        exit(0);
    }
}

while ((wpid = wait(&status)) > 0); // this way, the father waits for all the child processes 

//Father code (After all child processes end)

waita子プロセスが終了するまで待機し、その子プロセスのpidを返します。エラーの場合(たとえば、子プロセスがない場合)、-1が返されます。したがって、基本的に、コードは、waitingがエラーになるまで、子プロセスが終了するのを待ち続けます。

46
adrisons

POSIXは関数wait(NULL);を定義します。これはwaitpid(-1, NULL, 0);の省略形で、子プロセスが終了するまで呼び出しプロセスの実行を中断します。ここで、waitpidの最初の引数は、子プロセスが終了するまで待機することを示します。

あなたの場合、あなたのelseブランチ内から親にそれを呼ばせてください。

26
longdeqidao

次のようにwaitpid()を使用します。

pid_t childPid;  // the child process that the execution will soon run inside of. 
childPid = fork();

if(childPid == 0)  // fork succeeded 
{   
   // Do something   
   exit(0); 
}

else if(childPid < 0)  // fork failed 
{    
   // log the error
}

else  // Main (parent) process after fork succeeds 
{    
    int returnStatus;    
    waitpid(childPid, &returnStatus, 0);  // Parent process waits here for child to terminate.

    if (returnStatus == 0)  // Verify child process terminated without error.  
    {
       printf("The child process terminated normally.");    
    }

    if (returnStatus == 1)      
    {
       printf("The child process terminated with an error!.");    
    }
}
22
Jason Enochs