分类:
2008-11-04 14:53:18
5.4 buffering
这里讲的是standard i/o 的buffering,不是kernel级别的buffering
干什么的,好处是什么:减少对底层的kernel的read和write的调用次数,提高效率。
有分类么:unbuffered, fully buffered, line buffered
怎么用?:我们可以打开,关闭buffer的使用,可以手动分配buffer然后指定用我们分配的这个buffer,还可以让他用默认的,即自动生成buffer。
注意:fflush的调用,只会将一个stream的buffer里的数据发送到kernel,如果kernel端也有buffer,那么就不一定会真正进行对数据的处理。不过这也就是standard i/o library能做到的了。
Setbuf可以打开,关闭 buffer的使用
Setvbuf可以详细设置使用何种buffer,可以设置使用用户分配的buffer。
默认的buffer模式:
By default, the stream that is opened is fully buffered, unless it refers to a terminal device, in which case it is line buffered. Once the stream is opened, but before we do any other operation on the stream, we can change the buffering if we want to, with the setbuf or setvbuf functions from the previous section.
#include void setbuf(FILE *restrict fp, char *restrict buf); int setvbuf(FILE *restrict fp, char *restrict buf, int mode,
size_t size); |
Returns: 0 if OK, nonzero on error |
Figure 5.1. Summary of the setbuf and setvbuf functions |
||||
Function |
mode |
buf |
Buffer and length |
Type of buffering |
setbuf |
|
non-null |
user buf of length BUFSIZ |
fully buffered or line buffered |
NULL |
(no buffer) |
unbuffered |
||
setvbuf |
_IOLBF |
non-null |
user buf of length size |
fully buffered |
NULL |
system buffer of appropriate length |
|||
_IOFBF |
non-null |
user buf of length size |
line buffered |
|
NULL |
system buffer of appropriate length |
|||
_IONBF |
(ignored) |
(no buffer) |
unbuffered |
#include int fflush(FILE *fp); |
Returns: 0 if OK, EOF on error |