简体   繁体   中英

Write system call writes data to disk directly?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM