次のコードを実行すると:
#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
出力が理解できません。同じ文字列が複数回出力されるのはなぜですか?