简体   繁体   English

使用setvbuf()禁用stdin和stdout的缓冲

[英]Disable buffering for stdin and stdout using setvbuf()

When I was reading about the usage of setvbuf() , I came across the _IONBF (no buffering) mode. 当我读到setvbuf()的用法时,我遇到了_IONBF (无缓冲)模式。 So I was curious how stdin and stdout will be affected if I try to disable the buffering. 所以我很好奇如果我试图禁用缓冲,stdin和stdout会受到什么影响。 Below is an example code : 下面是一个示例代码:

The 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;
}

The Question : 问题:

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() . 1.)从上面的代码中,即使我没有包含setvbuf() ,我给程序( 123a等)的样本输入也会产生相同的输出。

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. 2.)我理解缓冲区是一个中间存储,其中可以填充一大块数据,并且当缓冲区已满或者给出换行符时,所有这些数据将被发送到输入或输出流。

3.)So what does the effect of disabling buffer? 3.)那么禁用缓冲区的效果是什么? Is it in terms of performance? 是在性能方面吗?

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. 部分性能部分控制流库函数(fread,fgets,fprintf等)与设备/文件的实际I / O之间的关系。

For example, stream output to a character device (eg your terminal) are, by default, line buffered. 例如,默认情况下,流输出到字符设备(例如您的终端)是行缓冲的。 The effect of this is that the following code, 这样的效果是以下代码,

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

will wait 10 seconds and then print start stop [NL]. 将等待10秒然后打印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 . start打印,然后睡10秒,您可以在sleep呼叫之前添加fflush呼叫,或者使用setvbuf关闭stdout上的缓冲。

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 . 这意味着缓冲区在溢出缓冲区或执行fflush之前不会刷新。 This could be a problem with files, for example, if you want to monitor the output in real-time with tail -f . 这可能是文件的问题,例如,如果要使用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.) (注意:此开销取决于文件系统的安装方式。固定驱动器,安装的回写缓存,操作系统缓冲区写入磁盘时的开销较小,而后者写入可移动驱动器。如果在不卸载的情况下移除驱动器,操作系统将尝试执行部分写入以提高避免数据丢失的可能性。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM