流缓冲是什么意思? [英] What is meant by stream buffering?

查看:170
本文介绍了流缓冲是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始学习C编程,所以我是一个初学者,在学习标准文本流时,我想到了缓冲 stdout流而没有缓冲 stderr流的行,但我不是

I had started learning C programming, so I'm a beginner, while learning about standard streams of text, I came up with the lines "stdout" stream is buffered while "stderr" stream is not buffered, but I am not able to make sense with this lines.

我已经在该论坛上阅读过有关缓冲区的信息,我喜欢糖果类比,但我无法弄清楚是什么意思是当一个人说: 该流被缓冲而另一个没有缓冲。时,有什么作用?

I already have read about "buffer" on this forum and I like candy analogy, but I am not able to figure out what is meant when one says: "This stream is buffered and the other one is not." What is the effect?

有什么区别?

更新:它会影响处理速度吗?

Update: Does it affect the speed of processing?

推荐答案

缓冲区是属于流的一块内存,用于临时保存流数据。当对文件执行第一次I / O操作时,将调用 malloc 并获得一个缓冲区。写入流的字符通常会累积在缓冲区中(在以块形式传输到文件之前),而不是在应用程序输出后立即出现。同样,流从主机环境中以块为单位而不是逐个字符地检索输入。这样做是为了提高效率,因为文件和控制台的I / O与内存操作相比要慢。

Buffer is a block of memory which belongs to a stream and is used to hold stream data temporarily. When the first I/O operation occurs on a file, malloc is called and a buffer is obtained. Characters that are written to a stream are normally accumulated in the buffer (before being transmitted to the file in chunks), instead of appearing as soon as they are output by the application program. Similarly, streams retrieve input from the host environment in blocks rather than on a character-by-character basis. This is done to increase efficiency, as file and console I/O is slow in comparison to memory operations.

GCC提供了三种类型的缓冲-非缓冲,块缓冲和缓冲。行缓冲。无缓冲意味着字符在写入后立即出现在目标文件上(对于输出流),或者从输入文件中逐字符读取输入,而不是逐块读取(对于输入流)。块缓冲意味着将字符保存在缓冲区中并作为块写入或读取。行缓冲表示仅在将新行写入缓冲区或从缓冲区读取换行之前保存字符。
stdin stdout 在且仅当可以确定不引用交互式内容时才被块缓冲设备,否则它们是行缓冲的(任何流都是如此)。 stderr 在默认情况下始终未缓冲。

GCC provides three types of buffering - unbuffered, block buffered, and line buffered. Unbuffered means that characters appear on the destination file as soon as written (for an output stream), or input is read from a file on a character-by-character basis instead of reading in blocks (for input streams). Block buffered means that characters are saved up in the buffer and written or read as a block. Line buffered means that characters are saved up only till a newline is written into or read from the buffer. stdin and stdout are block buffered if and only if they can be determined not to refer to an interactive device else they are line buffered (this is true of any stream). stderr is always unbuffered by default.

标准库提供用于更改流默认行为的函数。您可以使用 fflush 强制将数据移出输出流缓冲区(对于输入流,未定义 fflush )。您可以使用 setbuf 函数使流无缓冲。

The standard library provides functions to alter the default behaviour of streams. You can use fflush to force the data out of the output stream buffer (fflush is undefined for input streams). You can make the stream unbuffered using the setbuf function.

这篇关于流缓冲是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆