Are there 3 child processes and 1 parent process? What does the two different waitpid do, and why are there two of them?
int main()
{
pid_t pid;
int status, counter = 4;
while(counter > 0)
{
pid = fork();
if(pid)
{
counter /= 2;
}
else
{
printf("%d", counter); /* (1) */
break;
}
}
if(pid)
{
waitpid(-1, &status, 0);
counter += WEXITSTATUS(status);
waitpid(-1, &status, 0);
counter += WEXITSTATUS(status);
printf(";%d", counter); /* (2) */
}
return counter;
}
the second printf after the waitpid prints 3, 5, 6, 34, 52, 61 (excluding the semicolon). Im not sure how there are two digits printing. I know the second digit is probably coming from the printf in the while loop.