Since after reopen
the stream is non-interactive, it's fully buffered and doesn't flush on '\n'
. Before fork
is called the buffer still contains the message, and after fork
this buffered message was duplicated (because both processes got their own copies of stdout
) and then flushed by both the parent and the child. See part 7.19.3 of C standard.
You can avoid such behavior by calling fflush
just before fork
.