The Linux Programming Interface 13 File I/O Buffering 文件接口缓存

本文详细介绍了Linux编程接口中关于文件I/O缓冲的内容,包括内核缓冲和stdio库的缓冲对应用性能的影响。阐述了缓冲设计的目的,即提高read()和write()的效率,减少磁盘传输次数。同时提到了如何控制内核和stdio缓冲,以及使用posix_fadvise()优化I/O性能。还讨论了O_DIRECT标志用于绕过缓冲缓存的情况,并介绍了fileno()和fdopen()在混合系统调用与标准C库函数进行文件I/O中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Linux Programming Interface

File I/O Buffering

(01) 内容

In this chapter, we describe both types of buffering and consider how the affect application performance.

(02)cache技术

write(fd, "abc", 3)

内核先把abc写入到buffer中,然后flush,之后再写到disk中。

(03)目的

The aim of this design is to allow read() and write() to be fast, since they don't need to wait on a (slow) disk operation.

This design is also efficient, since it reduces the number of disk transfers that the kernel must perform.

(04)整个读写过程图

(05)直接文件操作,不需要buffer

(06)总结

Buffering of input and output data is performed by the kernel, and also by the stdio library. In some cases, we may wish to prevent buffering, but we need to be aware of the impact this has on application performance. Various system calls and library functions can be used to control kernel and stdio buffering and to perform one-off buffer flushes.

    A process can use posix_fadvise() to advise the kernel of its likely pattern for accessing data from a specified file. The kernel may use this information to optimize the use of the buffer cache, thus improving I/O performance.

    The Linux-specific open() O_DIRECT flag allows specialized application to bypass the buffer cache.

    The fileno() and fdopen() functions assist us with the task of mixing system calls and standard C library functions to perform I/O on the same file. Give a stream, fileno() returns the corresponding file descriptor; fdopen() performs the converse operation, creating a new stream that employs a specified open file descriptor.

(07)习题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值