Tuesday, July 24, 2012

wait():

The parent process will often want to wait until all child processes have been completed. this can be implemented with the wait() function call.
wait(): Blocks calling process until the child process terminates. If child process has already teminated, the wait() call returns immediately. if the calling process has multiple child processes, the function returns when one returns.
waitpid(): Options available to block calling process for a particular child process not the first one. 
 #include <sys/wait.h>
02 
03...
04 
05      pid_t pID = <i>set to child process id with call to fork OR:</i>
06                  // If set <-1, wait for any child process whose process group ID = abs(pID)
07                  // If = -1, wait  for  any child process. Same as wait().
08                  // If =  0, wait for any child process whose process group ID is same as calling process.
09                  // If >  0, wait for the child whose process ID = pID.
10 
11...
12      int childExitStatus;
13 
14      pid_t ws = waitpid( pID, &childExitStatus, WNOHANG);
15 
16      if( WIFEXITED(childExitStatus) )
17      {
18         // Child process exited thus exec failed.
19         // LOG failure of exec in child process.
20         cout << "Result of waitpid: Child process exited thus exec failed." << endl;
21      }

No comments:

Post a Comment