您的位置:首页 > 运维架构 > Linux

[Unix/Linux Programming] Buffered I/O vs Unbuffered I/O

2011-08-18 22:49 666 查看
SEE HERE: Stackoverflow: buffered I/O vs unbuffered IO

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




Buffered I/O and non-buffered I/O

http://xiaomeng.yo2.cn/articles/tag/direct-io


实验需要对Flash Disk做无系统缓冲的I/O操作,顺便了解了一下Linux下的I/O.

Linux上的块设备的操作可以分为两类:

第一类是使用C标准库中的fopen/fread/fwrite 系列的函数,我们可以称其为 buffered I/O。

具体的I/O path如下

Application<->Library Buffer<->Operation System Cache<->File System/Volume Manager<->Device



library buffer是标准库提供的用户空间的buffer,可以通过setvbuf改变其大小。

第二类是使用Linux的系统调用的open/read/write 系列的函数,我们可以称其为 non-buffered I/O。

I/O Path

Application<-> Operation System Cache <->File System/Volume Manager<->Device

此外,我们可以通过设置open的O_DIRECT标志来实现Direct I/O(或者叫Raw I/O),即绕过OS Cache,直接读取Device ( that's
what we want^o^ ), 等于将OS cache换成自己管理的cache。不过,Linus在邮件列表中建议不这么做,而是使用posix_fadvice, madvice。[2]中表明Direct
I/O比buffered I/O的性能高很多。

在使用O_DIRECT的注意buffer的address必须是block alignment的(i.e. 初始地址必须是boundary), 可以用posix_memalign()函数分配内存以得到这样的buffer。至于为什么要这样,与实现的mmap有关,参见[5].

参考:

Linux: Accessing Files With O_DIRECT http://kerneltrap.org/node/7563
Andrea Arcangeli , O_DIRECT Whitepaperhttp://www.ukuug.org/events/linux2001/papers/html/AArcangeli-o_direct.html
A Trip Down the Data Path: I/O and Performancehttp://articles.directorym.net/_A_Trip_Down_the_Data_Path_IO_and_Performance-a894569.html
Operating Systems System Calls and I/Ohttp://articles.directorym.net/Operating_Systems_System_Calls_and_IO-a894576.html
Linux Device Drivers, 2nd Edition, Chapter 13 mmap and DMAhttp://www.xml.com/ldd/chapter/book/ch13.html
http://topic.csdn.net/u/20080806/10/cdb1faa1-0146-4e96-8b12-26ba60acdbb5.html
http://lists.alioth.debian.org/pipermail/parted-devel/2007-July/thread.html#1855
Read系统调用剖析, http://www.ibm.com/developerworks/cn/linux/l-cn-

另外推荐一篇不错的文章:http://www.ibm.com/developerworks/cn/linux/l-async/

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

http://zishen.blogspot.com/2011/03/buffered-io-unbuffered-io.html

簡單來說,

Buffered I/O 讀寫的資料都會經過 Cache Manager,會暫存一份在 Memory 當中,然後會根據作業系統決定寫回硬碟的時機(如果用 Write Through,還是可以自行控制)。此模式適合常讀寫檔案,因為可以快速的從 Memory 讀寫。

(想想 L1, L2 cache 之於 RAM 的關係。)

Unbuffered I/O 讀寫的資料不會經過 Cache Manager,而是直接對硬碟讀寫。因此也不會因為大量讀寫資料造成 Cache Trashing。

以下是我的推測,不代表真實測試結果。XD

Installer 的檔案複製可能就適合 Unbuffered I/O,因為 Buffered I/O 可能會造成大量的 Cache Trashing。

當有大量的 Cache Trashing 發生時,可能也會影響到 Unbuffered I/O 的效能。因為 Cache Manager 會對硬碟做大量讀寫,這個行為可能會和 Unbuffered I/O 產生 Resource Contention。

參考資料:

"Slow Large File Copy Issues" by Ask the Performance Team

Asynchronous Disk I/O Appears as Synchronous on Windows NT, Windows 2000, and Windows XP

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

Quote:
Originally Posted by netrak


What is the difference between buffered and unbuffered I/O and can anybody show me a sample C code using both buffered and unbuffered I/0 and pointing out the difference. I have looked through many resources and I don't know where
to start.Thanks.
Buffered I/O reads blocks of data at once. Unbuffered I/O reads a byte at a time. Buffered I/O is faster for large transactions, unbuffered I/O is more appropriate to interactive transactions like telnet etc..

Try Google for more information:

Google: "BUffered I/O"
__________________

If this answer helps you, please click the thanks button.

-- Paul at http://arachnoid.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐