是否有 3 个子进程和 1 个父进程?这两个不同的waitpid是做什么的,为什么会有两个?
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;
}
waitpid 之后的第二个 printf 打印 3、5、6、34、52、61(不包括分号)。我不确定如何打印两位数。我知道第二个数字可能来自 while 循环中的 printf。