简体   繁体   中英

Should fsync() be called before or after write() call?

I understand the working of write() call wherein it writes the data only to kernel's buffers which is later written to disk by kernel after sorting the data optimally.

Calling fsync() on file descriptor makes sure that data is written to disk as soon as it's posted in the kernel's buffer. My question is, whether fsync() should be called before write() or after write() call. I've read couple of books on the topic, looked on the internal as well but couldn't find a satisfactory answer.

you understanding of write is kinda wrong. after calling the write system call, it queues the data into kernel's buffer then it flushes out the data into the opened file descriptor whether it is an opened file or a socket.

  1. fsync should be called after the write system call. It flushes the file data buffers and the metadata to the physical device.

  2. Alternatively, you can use the O_SYNC flag in the open system call for the file and get the same result for each subsequent write call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM