From Operating System Concepts

Another issue that can affect the performance of I/O is whether writes to the file system occur synchronously or asynchronously.

Synchronous writes occur in the order in which the disk subsystem receives them, and the writes are not buffered. Thus, the calling routine must wait for the data to reach the disk drive before it can proceed.

In an asynchronous write, the data are stored in the cache, and control returns to the caller.

Most writes are asynchronous. However, metadata writes, among others, can be synchronous. Operating systems frequently include a flag in the open system call to allow a process to request that writes be performed synchronously. For example, databases use this feature for atomic transactions, to assure that data reach stable storage in the required order.

  1. In synchronous write, "the writes are not buffered". Are synchronous write and direct I/O the same concept?

  2. "Operating systems frequently include a flag in the open system call to allow a process to request that writes be performed synchronously." By calling what functions can you achieve synchronous write, and how do you call them? Is it open() with O_DIRECT?

  3. Is it correct that write() by default is blocking, and returns when finish writing to the buffer cache, not necessarily to the file? Is write() synchronous write by the definition in the quote?

  4. aio_write() is also called asynchronous I/O. Is aio_write() asynchronous write or not?



Re O_SYNC and O_DIRECT, they are not the same:

  • O_SYNC causes disk syncs but things go in the caches, so you have read cache
  • O_DIRECT bypasses the caches but doesn't sync
  • In the case of (e.g.) raid, AFAIK O_DIRECT could possibly result in things being sent to raid but not lower than that
  • O_SYNC will probably result in full disk syncs, which will cause data from other partitions and other files to be synced, and will also flush the hardware caches

Re blocking, yes and no:

  • If the filesystem is not mounted with sync then it will return as long as data have been copied to the kernel. As long as there's free buffer space in the kernel it will be instant. Once there's no more buffer space it will block until there is.
  • If the filesystem is mounted with sync then it will block

Re open, from man 2 open:

   O_SYNC Write operations on the file  will  complete  according  to  the
          requirements  of  synchronized I/O file integrity completion (by
          contrast with the synchronized  I/O  data  integrity  completion
          provided by O_DSYNC.)

See the manpage for more information on that.

  • @tim Where is that quote from? – V13 Oct 14 '18 at 0:03
  • @Tim do you mean synchronous? (the post says synchronous) – V13 Oct 14 '18 at 0:21
  • 1
    Sorry. My questions are: How do you understand the quote says "synchronous writes are not buffered"? Do you mean synchronous writes by O_SYNC? – Tim Oct 14 '18 at 0:25
  • @Tim, I think it's an artifact of the overloaded term "buffered". In this case it means that there isn't an intermediate buffer that may collate multiple write()s before actually writing the data, so two write()s of 10 bytes will be twice as slow as one of 20 byes. There's still full buffering in the kernel, although since the data won't be dirty for long, they may get discarded soon if there's memory pressure. – V13 Oct 14 '18 at 0:41
  • 2
    @Tim O_SYNC. See also the man page – V13 Oct 14 '18 at 0:56

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.