How do I clear output buffers when the program forks?

I have a program that writes a FILE * cgiOut file and immediately after writing it to a stream, I need to unblock and start a background process. The problem is that after the fork, the FILE * stream sometimes gets erased and I get duplicate output (after the fork, all open files are closed, which I think causes the buffers to be flushed). How can I avoid this? I don't want to close the file in the main process as it opens in the library and I think it is a socket or a pipe.

+1


source to share


2 answers


Not sure if I understand your question, but if you want your buffers to cgiOut

be empty before fork()

then you can use fflush()

in the stream directly in front of you fork()

. This should cause the buffers to be flushed.



It might be suitable for using an unbuffered stream by changing the buffering settings with help setvbuf()

and friends. This way you won't (read: shouldn't) have any buffering issues, but that might not be what you want.

+5


source


fflush(file)



0


source







All Articles