views:

132

answers:

2

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.

+4  A: 

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.

freespace
A: 

fflush(file)

BCS