Understanding of Fork in C
c
Solution
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.
Problem
I have one problem with fork that i don't undertsand. ``` #include <stdio.h> #include <string.h> main(){ printf("test 1.\n"); fork(); printf("test 2.\n"); } ``` the output is : test 1. test 2. test 1. test 2. Shouldn't i be getting... : test1 test2 test2 I really don't understand this because fork should create child process after the fork(); and not printing the teste1 again .