Unbuffered I/O

来源:互联网 发布:手机测试网络稳定性 编辑:程序博客网 时间:2024/05/06 11:28

Unbuffered I/O在概念上是相对于Buffered I/O(标准I/O),他的读写函数read,write都会触发系统的内核调用。Unbuffered I/O并不是说不使用Cache机制,一般来说Unix系统都会在内核中使用Buffer Cache 或者 Page Cache。当我们使用write来写数据到磁盘文件时,这些数据一般会被copy到内核的cache中排列起来,在之后某个时刻写到磁盘,这被称作延迟写。

APUE 2 中是这样解释的:Traditional implementations of the UNIX System have a buffer cache or page cache in the kernel through which most disk I/O passes. When we write data to a file, the data is normally copied by the kernel into one of its buffers and queued for writing to disk at some later time. This is called delayed write.

 

内核最终需要将cache中的数据写入磁盘,这样保证数据文件一致性,并使得cache可以重用。sync,fsync和fdatasync可用来进行数据同步。

sync:触发cache数据的写入磁盘操作,但是它会直接返回不会等操作结束。

fsync:针对单个文件,等待cache数据写入磁盘完成后返回。

fdatasync:与fsync相似,但是它只针对文件的数据部分。