1

I have this code:

  pid_t pid1 = fork();

  if (pid1 == 0)
  {
    //do some stuff
    if (something)
        exit(0);
    else
        exit(1);
  }
  else if (pid1 == -1)
    printf("error\n");

  pid_t pid2 = fork();

  if (pid2 == 0)
  {
    //do some stuff
    if (something)
        exit(0);
    else
        exit(1);
  }
  else if (pid2 == -1)
    printf("error\n");

//here I want to check both exit codes

The child processes will run in parallel. What I need is to check both exit codes whether they're 1 or 0. I thought I could use something like this:

pid_t pid;
int status;
while( (pid = wait(&status)) > 0)
   printf("%d exit code: %d\n", pid, WEXITSTATUS(status));

I am new to parallel programming so I am not sure if this is a correct solution. Isn't there possibility that one of the child processes exits before parent process reaches cycle so it won't get exit code?

sykatch
  • 301
  • 1
  • 2
  • 13

3 Answers3

2

If a process exits before the parent process reaches wait(), it will become a defunct process (or zombie process). This is OK, because if a wait function is called later, it will get that exit code and the zombie process will be terminated successfully.

Instead of using wait() which will just wait for any process, you can use waitpid() to wait for a specific process, as determined by its specific PID, since you have to wait for two processes.

pid_t waitpid(pid_t pid, int *status, int options);

The wait() system call suspends execution of the calling process until one of its children terminates. The call wait(&status) is equivalent to:

waitpid(-1, &status, 0);

The waitpid() system call suspends execution of the calling process until a child specified by pid argument has changed state. By default, waitpid() waits only for terminated children, but this behavior is modifiable via the options argument, as described below. The value of pid can be:

< -1

meaning wait for any child process whose process group ID is equal to the absolute value of pid.

-1

meaning wait for any child process.

0

meaning wait for any child process whose process group ID is equal to that of the calling process.

> 0

meaning wait for the child whose process ID is equal to the value of pid.

codingEnthusiast
  • 3,800
  • 2
  • 25
  • 37
2

If one or both of the child processes exit before the parent first calls wait(), then the OS will hold the exit code and/or reason for termination until the parent reaches the wait(). This is exactly what a "zombie process" is.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

See here: http://linux.die.net/man/2/wait

wait() will block until one of the two processes ends, and return info about that process. You can either call wait() until there are no more children, or use waitpid to get info about the specific PIDs.

So to answer your question -- your code looks fine. wait() takes care of the race condition for you, as it blocks execution until one of the children exits (or is stopped by another means).

Colin Nichols
  • 714
  • 4
  • 12