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

查看:32
本文介绍了使用 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
", 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
");

将等待 10 秒,然后打印 start stop[NL].第一次打印被缓冲,因为没有换行来刷新缓冲区.要让 start 打印,然后休眠 10 秒,您可以在 sleep 调用之前添加一个 fflush 调用,或者关闭 sleep 上的缓冲code>stdout 和 setvbuf.

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 实时监控输出.如果您知道可以完成此监视,则可以将流切换到行缓冲,以便每次打印新行时,缓冲区都会刷新到文件中.这将以增加开销为代价,因为在打印换行符时会多次写入磁盘块.(注意:此开销取决于文件系统的挂载方式.固定驱动器,挂载的回写缓存,在操作系统缓冲区写入磁盘时将具有较少的开销,而可移动驱动器则以写入方式挂载.在后者中在这种情况下,操作系统将尝试进行部分写入,以提高在不卸载的情况下移除驱动器时避免数据丢失的机会.)

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天全站免登陆