Search code examples
filewinapiposixdevice

POSIX O_DIRECT vs Windows FILE_FLAG_WRITE_THROUGH & FILE_FLAG_NO_BUFFERING


From what I can gather, then using POSIX O_DIRECT with open() on blocking device files works just the same way as combining both FILE_FLAG_WRITE_THROUGH and FILE_FLAG_NO_BUFFERING with CreateFile() on Windows - is this correctly assumed by me?

Open:

O_DIRECT Try to minimize cache effects of the I/O to and from this file. In general this will degrade performance, but it is useful in special situations, such as when applications do their own caching. File I/O is done directly to/from user space buffers.

Createfile:

If FILE_FLAG_WRITE_THROUGH is used but FILE_FLAG_NO_BUFFERING is not also specified, so that system caching is in effect, then the data is written to the system cache but is flushed to disk without delay. If FILE_FLAG_WRITE_THROUGH and FILE_FLAG_NO_BUFFERING are both specified, so that system caching is not in effect, then the data is immediately flushed to disk without going through the Windows system cache. The operating system also requests a write-through of the hard disk's local hardware cache to persistent media.


Solution

  • Yes, it will have a very similar effect to use those two flags in a Windows system as O_DIRECT on a POSIX system. Since these are two different OS's, you can expect that there is no EXACT match.

    However, unless you are trying to understand exactly how the filesystem does things, you get "don't buffer the data, and write it straight to disk as soon as possible" from both of those options.