问题
There is page cache before we write data to disk.
So if I have two operations.
write(fileA)
write(fileB)
Then if the system is suddenly shutdown. We don't initiative call the sync() call.
I want to know if it is possible that the data we wrote to fileB has flush to the disk, while the data we wrote to fileA haven't been flush to the disk?
回答1:
I believe that it is possible for fileB to be written to disk before fileA, as the writes will be bundled into block I/O requests and can be reordered at the block device layer by the I/O scheduler in an attempt to minimise disk seeking.
See the kernel documentation for more info about the I/O scheduler (elevator): http://lxr.free-electrons.com/source/Documentation/block/biodoc.txt#L885
回答2:
To answer you question in short you may want to consider calling sync()
or fsync()
system call in your application after the write()
to make sure that data is sync-ed to the disk immediately.
flush (or pdflush) kernel threads are responsible for syncing dirty pages to the disk. When the system is being shutdown
properly, all the dirty buffers get synced / written to disk. However, this is not the same in case of abrupt power failures as data which is not yet flushed / sync-ed to the disk is obviously lost.
If you don’t call sync()
in your application, then dirty buffers get written to the disk upon certain kernel tunables. You could control how the application data gets synced (inactive dirty pages) through sysctl
kernel tunable. You may want to consider reading more about the following:
vm.dirty_expire_centisecs - how old (in 1/100th of a sec) the dirty pages must be before writing them to the disk
vm.dirty_writeback_centisecs - how often the kernel will wake up the BDI-flush thread to sync the dirty pages onto the disk
vm.dirty_background_ratio - percentage of system memory which when dirty then system can start writing data to the disks
vm.dirty_ratio - percentage of system memory which when dirty the the process doing writes should block to write out dirty pages to the disks
来源:https://stackoverflow.com/questions/26707442/the-linux-page-cache-flush-order