使用setvbuf()禁用stdin和stdout的缓冲 [英] Disable buffering for stdin and stdout using setvbuf()

查看:434
本文介绍了使用setvbuf()禁用stdin和stdout的缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我阅读有关 setvbuf()的用法时,我遇到了 _IONBF (无缓冲)模式。因此,我很好奇如果尝试禁用缓冲将如何影响stdin和stdout。下面是示例代码:

When I was reading about the usage of setvbuf() , I came across the _IONBF(no buffering) mode. So I was curious how stdin and stdout will be affected if I try to disable the buffering. Below is an example code :

代码:

#include <stdio.h>

int main(void)
{

   int num;
   char a;
   setvbuf(stdin, NULL, _IONBF, 0); //turn off buffering
   scanf("%d", &num);
   a = getchar();
   printf("%d %c\n", num , a);

       return 0;
}

问题:

1。)从上面的代码中,我为程序提供了示例输入( 123a 和等等)即使我不包含 setvbuf(),也会产生相同的输出。

1.) From the above code, the sample input I've given to the program (123a and etc) yield the same output even if I didn't include setvbuf().

2。)我理解该缓冲区是一个中间存储,可以在其中存储大量数据,并且当缓冲区已满或给出换行符时,所有这些数据都将被发送到输入或输出流。

2.) I understand that buffer is an intermediate storage in which a chunk of data can be filled into it and all those data will be send to the input or output stream either when the buffer is full or a newline is given.

3。)那么禁用缓冲区的作用是什么?

3.)So what does the effect of disabling buffer? Is it in terms of performance?

推荐答案

部分是性能,部分是控制流库功能(fread,fgets,fprintf)等等。)与设备/文件的实际I / O有关。

It is partly performance and partly control over how stream library functions (fread, fgets, fprintf, etc.) relate to actual I/O to a device/file.

例如,默认情况下,输出到字符设备(例如您的终端)的流是行缓冲。这样做的效果是,以下代码

For example, stream output to a character device (e. g. your terminal) are, by default, line buffered. The effect of this is that the following code,

printf("start ");
sleep(10);
printf("stop\n");

将等待10秒钟,然后打印 start stop [NL]。第一次打印被缓冲,因为没有新行刷新缓冲区。要开始打印 ,然后休眠10秒钟,您可以在<$ c $之前添加 fflush 调用c> sleep 调用,或使用 setvbuf 关闭 stdout 的缓冲。

will wait 10 seconds and then print start stop[NL]. The first print was buffered because there was no new-line to flush the buffer. To get start to print, then sleep 10 seconds,you could either add a fflush call before the sleep call, or turn off buffering on stdout with setvbuf.

默认情况下,流输出到块设备或磁盘文件是完全缓冲的。这意味着在您溢出缓冲区或执行 fflush 之前,缓冲区不会刷新。例如,如果要使用 tail -f 实时监视输出,这可能是文件的问题。如果您知道可以进行此监视,则可以将流切换为行缓冲,这样,每次打印新行时,缓冲区就会刷新到文件中。这将以增加开销为代价,因为在打印换行符时会多次写入磁盘块。 (注意:此开销取决于文件系统的安装方式。固定驱动器(即安装的回写式缓存)与OS缓冲区写入磁盘的开销相比,与OS缓冲式装载的写式驱动器的开销要小。在这种情况下,操作系统将尝试执行部分写入操作,以提高在不卸下驱动器的情况下卸下驱动器的避免数据丢失的机会。)

Stream output to a block device or disk file is, by default, fully buffered. This means that the buffer won't flush until either you overflow the buffer or do an fflush. This could be a problem with files, for example, if you want to monitor the output in real-time with tail -f. If you know that this monitoring may be done, you could switch the stream to line-buffering so that every time a new-line is printed, the buffer is flushed to the file. This would be at the cost of increased overhead as disk blocks are written several times as new-lines are printed. (Note: this overhead depends on how the file system is mounted. A fixed drive, mounted write-back cache, will have less overhead as the OS buffers writes to the disk, vs. a removable drive mounted write-though. In the latter case, the OS will try to do the partial writes to improve the chances of avoiding data loss if the drive is removed without dismounting.)

这篇关于使用setvbuf()禁用stdin和stdout的缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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