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?
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.