I am working on process control in linux operating system using c.
the fork() function really confuse me .
what I know : when fork() is called
1) whatever the code just after fork() , is copied to the child process.
2) we can not determine which (parent or child) will run first.
I run the following code.
int main() {
printf("Hi");
fork();
return 0;
}
my first question is: why I get Hi two times? it is explained in Working of fork() in linux gcc , but still I want someone to explain with more simplicity.
my second doubt: when I redirect my output to somefile.txt even if I use newline character (\n) the output is:
hi
hi
please explain ...
please provide me some detail if I missed , in understanding the fork()
If you write:
"Hi"won't be printed onstdout, since the standard ouput stream is line-buffered by default. This means that you have to add a'\n'to flush this buffer.With
fork, the father process' buffers will be copied into the child process.