So I\'m getting to know how processes work and have written some simple code.
#include
#include
#include
#i
child is getting back into the main instead of getting terminated by the exit
..no, that's not the case.
There are many issues with your code.
\Child
will give you error in terms of "unknown escape sequence", change to \nChild
.stdlib.h
for exit()
.unistd.h
for fork()
\n
to printf("Heeeyoooo!");
to flush the output buffer.After 1,2 and 3, the main problem in your code is, there is no newline escape sequence
present in your printf()
which is why your output buffer is not flushed. So, to flush out the standard output buffer before next print, add a newline escape sequence [\n
] which will flush the buffer.
Worth of mentioning, from the man page of fork()
The child process shall have its own copy of the parent's open directory streams. Each open directory stream in the child process may share directory stream positioning with the corresponding directory stream of the parent
which means, without the flushing of the buffer, Heeeyoooo!
is still present in child's output stream and hence it is printed again.
If you write
printf("Heeeyoooo!");
fflush(stdout);
and then fork, the error goes away. The reason is that fork()
clones the output buffer for stdout while "Heeeyoooo!"
is still in it, so it is subsequently printed twice.