Reputation: 2418
I've read couple of questions(here) related to this but I still have some confusion.
My understanding is that write system call puts the data into Buffered Cache
(OS caches as referred in that question). When the Buffered Cache
gets full it is written to the disk.
Buffered IO is further optimization on top of this. It caches in the C RTL buffers
and when they get full a write
system call issued to move the contents to Buffered Cache
. If I use fflush
then data related to this particular file that is present in the C RTL buffers
as well as Buffered Cache
is sent to the disk.
Is my understanding correct?
Upvotes: 4
Views: 3491
Reputation: 409432
How the stdio buffers are flushed is depending on the standard C library you use. To quote from the Linux manual page:
Note that fflush() only flushes the user space buffers provided by the C library. To ensure that the data is physically stored on disk the kernel buffers must be flushed too, for example, with sync(2) or fsync(2).
This means that on a Linux system, using fflush
or overflowing the buffer will call the write
function. But the operating system may keep internal buffers, and not actually write the data to the device. To make sure the data is truly written to the device, use both fflush
and the low-level fsync
.
Edit: Answer rephrased.
Upvotes: 3