Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is FILE flushed?

I have a good old C FILE file descriptor under Windows that is used by an output stream to write data to. My question is simple and yet I could not find the answer:

When is the content flushed to disc assuming I don't call fflush?

The stream constantly receives data and it seems that the content is flushed quite often, but what is the rule for flushing it?

like image 315
anhoppe Avatar asked Apr 23 '13 07:04

anhoppe


People also ask

What does it mean to flush a file?

file. flush forces the data to be written out at that moment. This is hand when you know that it might be a while before you have more data to write out, but you want other processes to be able to view the data you've already written.

What is flush file system?

NAS Bridge caches the file system data, which can improve client and overall system performance. Flushing a file system causes NAS Bridge to immediately persist the cached data to the StorageGRID Webscale system.

What happens when a buffer is flushed?

A buffer flush is the transfer of computer data from a temporary storage area to the computer's permanent memory. For instance, if we make any changes in a file, the changes we see on one computer screen are stored temporarily in a buffer.


1 Answers

If the library implementation can determine the output stream not to refer to an interactive device (and only then), the stream will be fully buffered, i.e. it will be flushed when the buffer (by default of BUFSIZ size) is full.

If not fully buffered, a stream can be line buffered, i.e. it will be flushed when an '\n' is written (or the buffer is full, if your line is really long), or unbuffered.

(ISO/IEC 9899:1999, chapter 7.19.5.3 "The fopen() function", paragraph 7. Don't have a newer version of the standard at hand, but AFAIK this didn't change.)

What constitutes an "interactive device" is implementation-defined. (Chapter 5.1.2.3 "Program execution", paragraph 6.)

The general idea is that file output should be fully buffered whereas terminal output be line buffered (or unbuffered, as Jesse Good correctly pointed out).

Both the buffering policy and the buffer size can be changed via setvbuf(). Note that any such change must happen before you start accessing the stream, which is somewhat obvious once you think about it.

like image 75
DevSolar Avatar answered Oct 17 '22 23:10

DevSolar