Chinaunix首页 | 论坛 | 博客
  • 博客访问: 988450
  • 博文数量: 200
  • 博客积分: 5011
  • 博客等级: 大校
  • 技术积分: 2479
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-27 15:07
文章分类

全部博文(200)

文章存档

2009年(12)

2008年(190)

我的朋友

分类:

2008-11-04 14:53:18

5.4 buffering

这里讲的是standard i/o buffering,不是kernel级别的buffering

干什么的,好处是什么:减少对底层的kernelreadwrite的调用次数,提高效率。

有分类么:unbuffered, fully buffered, line buffered

怎么用?:我们可以打开,关闭buffer的使用,可以手动分配buffer然后指定用我们分配的这个buffer,还可以让他用默认的,即自动生成buffer

注意:fflush的调用,只会将一个streambuffer里的数据发送到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

 

阅读(452) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~