I am redirecting stdout
on a process with freopen()
, and as long as it's just one process, everything's fine.
However, if I do something like this:
freopen("stdout.txt", "a+", stdout);
printf("Initial line.\n");
int i=0;
while(i<1000)
{
if(fork())
wait(NULL);
else
printf("Line %d.\n", i);
i++;
}
The first printed lines are re-printed on the file over and over. Is there anything particular I should do to avoid this from happening?
This is because the standard C library applies buffering to the standard output stream when using
stdio
functions. In particular, whenstdout
is redirected to a file, the buffering mode changes from line-buffered to fully buffered (with a buffer of size defined by the library). In line-buffered mode, the buffer gets flushed (and the data actually written) only when a newline is encountered, but in fully buffered mode this only happens when the maximum buffer size is reached.When you create multiple processes, they all share the same buffer of the parent. Since you do not reach the end of the buffer before spawning new children, and you do not empty it, they will all have the same buffer content. After that, when each of the children dies, the buffer will be automatically flushed by the standard library.
Yes. Flush the buffer before creating children.
You also probably want to
exit()
before the next cycle after the child has done its job: