I am just experimenting with fork() c++ function.but I found Surprising results
#include<stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h>
int main()
{
int x=0;
int i;
for( i=0;i<2;i++)
{
fork();
x=x+5;
printf("%d\n",x);
sleep(2);
}
return 0;
}
This code is giving output as expected 5
5
10
10
10
10
but same code behaves very differently when I replaced new line in printf with space
int main()
{
int x=0;
int i;
for( i=0;i<2;i++)
{
fork();
x=x+5;
printf("%d ",x);
sleep(2);
}
return 0;
}
5 10 5 10 5 10 5 10
Can anyone tell why so abnormal result came when I replaced new line with space.
I am using gcc compiler gcc version 4.1.2 20080704 (Red Hat 4.1.2-48)
Thanks a lot in advance