I’ve been having difficulty in using the system command “sync”. As it known to all “sync” command is used for flushing the system buffer into the physical I/O device, but it’s not working in my case.
Let me tell the special way I use it in first. I have multiple file streams, at the same time, continuously writing into a storage device, SD card or udisk, at a pretty high bitrate, and when I finish the task I stop all the streams and execute “sync”. With a normal return from the command and my code exiting normally, I pull out the storage device and occasionally get a corrupted files, which misses some part of its data.
I have even tried calling the command twice, but there’s nothing changed.
I don’t think sync can be used this way, it doesn’t produce hot-plug hardware. For one thing this produces a command to tell the device itself to sync, but it is up to the device on completing this. Most file systems used in Linux also have metadata regarding mount counts and last mount time or journals…even if a particular part of the system managed to issue a sync to the device yanking a mounted file system may result in corrupting metadata (it is not just the data which is in the process of sync and successful return of sync may not imply the hardware itself finished…especially the metadata).
EDIT: Note that is is possible to mount a file system unbuffered (synchronous). This is quite slow, and perhaps even limits the life of a solid state device, but is probably the only way you can guarantee not corrupting a file system being yanked while mounted.