Direct I/O,Synchronous I/O的概念和区别

Direct I/O概念:

Direct I/O is a way to avoid entire caching layer in the kernel and send the I/O directly to the disk.

想要使用direct io的模式,可以这样做:

Opens files with O_DIRECT flag.


Synchronous I/O概念:

Synchronous I/O is any I/O (system I/O with or without O_DIRECT, or stream I/O) performed to a file descriptor that was opened using the O_SYNC or O_DSYNC flags.


几个Flag的区别:

O_DIRECT  I/O operations performed against files opened with O_DIRECT bypass the kernel's page cache, writing directly to the storage.
O_SYNC File data and all file metadata are written synchronously to disk.
O_DSYNC     Only file data and metadata needed to access the file data are written synchronously to disk. Metadata that is not required for retrieving the data of the file may not be written immediately.


2019年5月6日更新:

===============

上面的解释非常不清楚,我找到了这篇名为“UNIX高级环境编程(14)文件IO - O_DIRECT和O_SYNC详解 < 海棠花溪 >”的文章。看了之后,才终于懂了这两个flag的区别。

O_DIRECT:用于让IO从用户态直接跨过“stdio缓冲区的高速缓存”和“内核缓冲区的高速缓存”,直接写到存储上。

O_SYNC:用于控制“内核缓冲区的高速缓存”直接写到存储上,即强制刷新内核缓冲区到输出文件的存储。


I/O缓冲的过程是这样的:

用户数据 –> stdio缓冲区 –> 内核缓冲区高速缓存 –> 磁盘


可见,上面的两个flag的区别是O_DIRECT让IO从用户数据中直接到磁盘(跨过了两个缓冲区),而O_SYNC让IO从内核缓冲区直接到磁盘(仅跨过了内核缓冲区)。


参考资料

===============

What is direct I/O anyway?

http://www.alexonlinux.com/what-is-direct-io-anyway 

Ensuring data reaches disk

https://lwn.net/Articles/457667/

UNIX高级环境编程(14)文件IO - O_DIRECT和O_SYNC详解 < 海棠花溪 >

https://www.cnblogs.com/suzhou/p/5381738.html

原文地址:https://www.cnblogs.com/awpatp/p/8777796.html