How do you clear output buffers when the program forks?

I have a program that writes to a FILE *cgiOut and just after it has written to the stream, I need to fork and run a background process. The trouble is that after the fork, the FILE * stream seems to flush out sometimes and I get duplicated output (after the fork, all open files are closed which I guess causes the buffers to be flushed). How can I avoid this? I don't want to close the file in the master process as it is opened in a library and it is a socket or pipe I think.

Answers


Not quite sure I understand your question, but if you want to make sure your cgiOut buffers are empty before you fork() you can use fflush() on the stream just before you fork(). That should force the buffers to be flushed.

It maybe suitable to use an unbuffered stream, by changing the buffering settings via setvbuf() and friends. This way you won't (read: shouldn't) have any buffering issues, but it might not be what you want.


fflush(file)


Need Your Help

MATLAB how to write header in text file

file matlab file-io header text-files

How to write a text header in text file? for example in the example below, how to write the header code salay month just once?

std::function and shared_ptr

c++ std-function loki

I have been using Loki's Functor for a while and I recently asked a question about it (still unanswered...)