Andre
Andre

Reputation: 186

file system operation really "flushed"

We are working on an iMX6Sx Freescale board, building the Linux kernel distro with Yocto. I would like to know if there is a way to check if it is possible to check if file system operations (in particular, write) are really terminated, avoiding to close/kill a process while operations are still running.

To be more clear: we have to do some actions (copy of files, writes, ..) when our application has to switch-off and we have to know (since they are asynchronus I think) when they're are really completed.

Thanks in advance Andrea

Upvotes: 1

Views: 267

Answers (1)

nos
nos

Reputation: 229314

If you want to ensure all the writes are commited to storage and the filesystem is updated:

  • call fsync() on the file descriptor,
  • open the parent directory and call fsync() on that file descriptor

When both of these are done, the kernel has flushed everything from memory and ensured the filesystem is updated regarding the file you operate on.

Another approach is to call sync(), which ensures all kernel data are written to storage for all files and filesystem metadata.

Note:

  • if your application are working with FILE* instead of file descriptors, you need to first ensure written data are flushed from your application to the kernel, either by calling fflush() or fclose the FILE*

  • If you kill an application, any write operation it has performed will not be cancelled or interrupted, and you can make sure it's committed to storage by calling sync() or open the same file and call fsync() on it.

  • If you kill an application arbitrarily you can't expect everything to be consistent, perhaps the application was doing 2 important writes to a database, config file, etc. and you terminated it after the 1 write, the file might be damaged according to its format.

Upvotes: 2

Related Questions