I have a compact-flash memory card detected as a block device /dev/sdb. It has a single FAT16 partition. Now if I mount the FAT16 file-system, write something to the file system and umount the file system, then actual writing of the file takes place during the umount:

# mount | grep sdb
/dev/sdb1 on /media type vfat (rw)
# time cp image.bin /media/

real    0m0.179s
user    0m0.000s
sys 0m0.104s
# time umount /media 

real    0m18.185s
user    0m0.004s
sys 0m0.032s

I have seen similar behavior with USB flash-memory. What determines if the file is written to the file system right away or during the file system detaching?


The kernel keeps data cached in memory, reading data is critical (somebody is waiting for it), writing can be done lazily (nobody hurries you; the data just might be used again/modified anew). As devices can either be reading or writing at a time, it pays off to defer writing as much as possible. So data is written "voluntarily" by the kernel only if there is no way out (need the space now). You can force the kernel's hand by opening files synchronously (write as soon as anything changes), by calling fsync(2) on the file to force data out. When you unmount a device, obviously all data cached for it has to be written out beforehand.

The above should give you shivers, as the machine might crash at any time and leave lots of unwritten data. That is why there is a program called sync(1), which makes the kernel write out all cached file data. It is normally set up so it runs periodically (typically each 5 seconds or so) to avoid data loss as described. You'd need to check your distribution's documentation on how exactly this is set up or tweaked.

| improve this answer | |

Writes are done right away if the file is open synchronously, or the file system mounted with the sync option.

This is generally bad for the overall system performance so is only done when critical data must be committed to the storage device when the write call returns. Copying a file is not considered as critical so the kernel bufferize the write internally and only flush the data to the media periodically.

You can ask the OS to flush all of these caches with the sync command

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.