I would like to have more clarification on the functionality of sync(8) and fsync functions in Linux (2.6.31). Does it make sure the files are written to the respective storage?
Asked
Active
Viewed 7,686 times
9

Shahbaz
- 46,337
- 19
- 116
- 182

spitfire88
- 1,596
- 3
- 19
- 31
-
since i face an issue after writing to sd card. here is the link http://stackoverflow.com/questions/12262044/sd-card-data-sometimes-not-seen-on-pc-when-connected-in-usb-mass-storage-mode-v – spitfire88 Sep 04 '12 at 11:47
1 Answers
8
http://linux.die.net/man/8/sync
It does not make sure that files are written to respective storage. It only makes sure that cached/buffered data is flushed to the disk device. It doesn't matter if this is an SD Card or whatever.

Michał Kosmulski
- 9,855
- 1
- 32
- 51

Oliver
- 928
- 9
- 25
-
4To be more precise, it flushes data to the device, but the device usually has a caching layer of its own, so it may still not be in persistent storage after the flush (i.e. it may be in the disk's cache but not yet on the platter / flash memory). This means if your power goes off, you may still lose data. – Michał Kosmulski Sep 04 '12 at 12:08
-
3To be even more precise, that depends. If you have a fs that supports barriers, and those are enabled, sync()/fsync() and some other operations will cause the appropriate CACHE FLUSH (ATA) or SYNCHRONIZE CACHE (SCSI) commands to be sent to the device; it's of course up to the device to implement those commands sensibly. – janneb Sep 04 '12 at 12:14
-
if the device in question (SD card/MMC driver) maintains a caching layer of its own where would it be? will i be able to debug this layer? – spitfire88 Sep 04 '12 at 12:15
-
1My first sentence should have clarified that, from experience I can tell that caching on disk drives of professional solutions also have batteries to be able to write the cache down to the medium. The normal disk drives will rarely do intelligent caching, they just use the buffer to have a constant write time, after a few seconds the data will be written, using fsync will do the job, that's why its used in reboot,shutdown ect. – Oliver Sep 04 '12 at 12:17
-
-
3fsync tells the OS to flush its buffers/cache to the physical media. So from API perspective, it make sure the files are written to the respective storage. However at storage media controller layer, there might be chaching as well. – Adil Sep 04 '12 at 13:36
-
thanks evryone. i think i have a clear understanding of sync operation now. – spitfire88 Sep 08 '12 at 08:26