M
Manohar S
It is a problem related to printf, and buffering to the printf statements
Look through this program.
main()
{
int pid;
int loop,max=3;
for(loop = 0; loop <max;loop++)
{
pid = fork();
if(pid < 0)
printf("\nThe greatest fork error of the decade");
else if(pid==0)
{
printf("\nI am a child");
goto out;
}
else
{
printf("\nI have grown up");
}
}
out:
}
output:
I am a child
I have grown up
I am a childI have grown up
I have grown up
I am a childI have grown up
I have grown up
the same program with '\n' placed at the end of printf
main()
{
int pid;
int loop,max=3;
for(loop = 0; loop <max;loop++)
{
pid = fork();
if(pid < 0)
printf("The greatest fork error of the decade\n");
else if(pid==0)
{
printf("I am a child\n");
goto out;
}
else
{
printf("I have grown up\n");
}
}
out:
}
output:
I am a child
I have grown up
I am a child
I have grown up
I am a child
I have grown up
why is this difference???
could anyone please explain.....
Look through this program.
main()
{
int pid;
int loop,max=3;
for(loop = 0; loop <max;loop++)
{
pid = fork();
if(pid < 0)
printf("\nThe greatest fork error of the decade");
else if(pid==0)
{
printf("\nI am a child");
goto out;
}
else
{
printf("\nI have grown up");
}
}
out:
}
output:
I am a child
I have grown up
I am a childI have grown up
I have grown up
I am a childI have grown up
I have grown up
the same program with '\n' placed at the end of printf
main()
{
int pid;
int loop,max=3;
for(loop = 0; loop <max;loop++)
{
pid = fork();
if(pid < 0)
printf("The greatest fork error of the decade\n");
else if(pid==0)
{
printf("I am a child\n");
goto out;
}
else
{
printf("I have grown up\n");
}
}
out:
}
output:
I am a child
I have grown up
I am a child
I have grown up
I am a child
I have grown up
why is this difference???
could anyone please explain.....