2

Possible Duplicate:
Need to know how fork works???

I am trying the following C code:

int main()  
{  
    printf("text1\n");  
    fork();  
    printf("text2\n");  
    return 0;  
}  

I was expecting to get the output where i get two "text1" and two "text2", like:

text1  
text1  
text2  
text2  

But, i am, instead, getting:

text1  
text2  
text2  

only one "text1"??? If child process executes from the fork(), then why do i get two "text1" for following:

int main()  
{  
    printf("text1");  
    fork();  
    printf("text2\n");  
    return 0;  
}  

the output now is:

text1text2  
text1text2  

If the child process starts after the fork, output should be:

text1text2  
text2  
Community
  • 1
  • 1
mandavi
  • 485
  • 4
  • 10

0 Answers0