Fork and \n

Hi,
I wrote a simple program for understanding the fork command. The code is as below

int main(void)
{
	fork(); printf("hi 1 \n");
	fork(); printf("hi 2 \n");
	fork(); printf("hi 3 \n");
}

I am getting a variation in the number of times the printf is called if i remove the \n from each of the printf statements :confused:. Can anyone explain me why this is happening.

Thanks in Advance,

bye.

i see whats happening ..

put those 3 fork calls in a function and call that function from main ..
and after the function call add one printf("\n");

that would explain better :slight_smile:

What sort of variation you are getting.

  • sequence of print is changing OR
  • Getting many printfs ? OR
  • something else is there
  • Dheeraj