unix - Concepts on fork() system call -


if call fork() executed successfully, unix make 2 identical copies of address spaces, 1 parent , other child. both processes start execution @ next statement following fork() call.[ref:http://www.csl.mtu.edu/cs4411.ck/www/notes/process/fork/create.html].

so,if execute following code:

#include <stdio.h> int main(void) {     printf("hello\n");     fork();     printf("world\n");     return 0; } 

i think print

hello world world 

but when run program prints

hello world hello world 

please explain lacking in concept?

this buffering. when print "hello" doesn't go output immediately. instead, goes buffer. it's still there during fork, so, when each task terminates , flushes buffer, there 2 copies send output.

to counter this, specify unbuffered i/o or call fflush before fork flush buffer.


Comments