如果我运行以下代码:
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid, pid1;
fflush(stdout);
pid = fork();
fflush(stdout);
pid1 = fork();
if(pid==0)
{
printf("%d is the first child\n", getpid() );
}
else if(pid>0)
{
printf("%d is the first parent\n", pid);
wait();
}
if(pid1==0)
{
printf("%d is the second child\n", getpid() );
}
else if(pid1>0)
{
printf("%d is the second child\n", pid1);
wait();
}
return 0;
}
我得到输出:
2896 is the first parent
2896 is the first child
2898 is the first child
2898 is the second child
2898 is the second child
2896 is the first parent
2897 is the second child
2897 is the second child
我无法理解输出。为什么多次打印相同的字符串?