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