When you call printf
, it doesn't print any text immediately. Instead it waits until you've printed a lot of text, or you call fflush(stdout)
, or the program exits. (Edit: There are also other things that will cause buffered text to be printed)
When the process forks, it copies the buffer of un-printed text (which contains "test 1.\n"). Both processes then print "test 1.\n" when they exit.
Calling fflush(stdout)
before fork()
fixes this, by making sure "test 1.\n" is actually printed before the process forks.