I tried to understand forks and tried to follow in C:
#include<stdio.h> #include <unistd.h> void forker() { printf("%d: A\n",(int)getpid()); fork(); wait(); printf("%d: B\n",(int)getpid()); printf("%d: C\n",(int)getpid()); fork(); wait(); printf("%d: D\n",(int)getpid()); } int main(void) { forker(); return 0; }
When I compiled and ran the resulting a.out, here is what I noticed:
> ./a.out 3560: A 3561: B 3561: C 3562: D 3561: D 3560: B 3560: C 3563: D 3560: D
However, when I do the following:
> ./a.out > t.txt
something strange happens:
> cat t.txt 3564: A 3565: B 3565: C 3566: D 3564: A 3565: B 3565: C 3565: D 3564: A 3564: B 3564: C 3567: D 3564: A 3564: B 3564: C 3564: D
Can someone explain this behavior? Why is the output different when redirecting to a file?
I am using Ubuntu 10.10, gcc version 4.4.5.
Salil source share