setvbuf()的使用 [英] Uses of setvbuf()

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

问题描述

哪种情况要求使用setvbuf(),特别是当你自己提供缓冲区时?
?用户指定

缓冲区比自动分配的缓冲区有什么优势?我们可以在

上调用setvbuf()预定义的流,立即启动main()吗?在典型的C程序中使用这个函数的频率是多少?


谢谢。

解决方案

santosh写于01/05/07 11:46,:


哪种情况要求使用setvbuf(),特别是当你自己提供缓冲区吗?b $ b用户指定

缓冲区比自动分配的缓冲区有什么优势?我们可以在

上调用setvbuf()预定义的流,立即启动main()吗?

这个函数在典型的C程序中使用的频率是多少?



可能最常用的是使用setvbuf()或setbuf()来使输出流行缓冲或无缓冲。提供的缓冲区(如果有的话)

大小在这种情况下不是问题




对于通过大量通过的输入或输出流。数据

并且可以完全缓冲运行,指定大数据。缓冲区

可以提高I / O吞吐量。你获得多少改进取决于系统,C实现,程序'/ b $ b $ I / O行为,以及许多其他特定情况的细节;

很多的含义和大也难以确定。

更大并不总是更好; 太大在某些情况下,缓冲区实际上可能会损害性能。


您可以将setvbuf()或setbuf()应用于任何流,如果

你很快就做到了。在这方面,

预定义流没有什么特别之处。


最后,这些功能实际上只是建议

到I / O库,库可以随意修改它们甚至忽略它们。实现方式不同,并且实现者(出于某些奇怪的原因)经常认为他对b

的实施基础有更多的了解。程序员...


-
Er *** ****** @ sun.com


Eric Sosman写道:


santosh写于01/05/07 11:46,:


哪种情况需要使用setvbuf(),特别是当你

时自己提供缓冲?用户指定

缓冲区比自动分配的缓冲区有什么优势?我们可以在

上调用setvbuf()预定义的流,立即启动main()吗?

这个函数在典型的C程序中使用的频率是多少?



< snip explanation>


最后,这些函数实际上只是对I / O库的建议

,并且库可以自由修改

他们甚至忽略他们。



我可以理解通过返回一个非零值来忽略一个调用但

不会默默地修改一个调用误导的语义调用

代码?这似乎更符合C的精神,无论是要么兑现

请求还是失败。


Anway感谢您的解释。
< blockquote class =post_quotes>
实现方式不同,

实现者(出于某种奇怪的原因)经常认为他对b
的基础有了更多的了解。实施

比程序员...



在大多数情况下他们可能会这样做。


santosh写道:


哪种情况要求使用setvbuf(),特别是当你提供
时你自己的缓冲?用户指定

缓冲区比自动分配的缓冲区有什么优势?我们可以在

上调用setvbuf()预定义的流,立即启动main()吗?

这个函数在典型的C程序中使用的频率是多少?



通常,当我调用setvbuf()时,它会关闭stdin

和/或stdout的缓冲。需要这个的一个例子是Winboard

协议国际象棋引擎,它们用国际象棋
协议服务器交换文本命令流。如果I / O被缓冲,命令可能无法刷新

并且引擎可以无故拖延。


有时,我想增加缓冲区的大小。顺序

扫描文件就是一个方便的例子。但是,允许的

缓冲区大小非常小,我通常会使用特定于操作系统的
例程,因为setvbuf()允许的微小缓冲区不会

可以带来任何巨大的推动。


Which situations call for the use of setvbuf(), specifically when you
supply the buffer yourself? What is the advantage of an user specified
buffer over an automatically allocated one? Can we call setvbuf() on
the predefined streams, immediatly upon start of main()? How often is
this function used in typical C programs?

Thanks.

解决方案

santosh wrote On 01/05/07 11:46,:

Which situations call for the use of setvbuf(), specifically when you
supply the buffer yourself? What is the advantage of an user specified
buffer over an automatically allocated one? Can we call setvbuf() on
the predefined streams, immediatly upon start of main()? How often is
this function used in typical C programs?

Probably the commonest use of setvbuf() or setbuf() is
to make an output stream line-buffered or unbuffered. The
size of the supplied buffer (if any) isn''t much of an issue
in this scenario.

For input or output streams that pass "a lot" of data
and can operate fully-buffered, specifying a "large" buffer
may improve the I/O throughput. How much improvement you
get depends on the system, the C implementation, the program''s
I/O behavior, and a lot of other situation-specific details;
the meanings of "a lot" and "large" are also hard to pin down.
Bigger is not always better; "too large" a buffer may actually
hurt performance in some circumstances.

You can apply setvbuf() or setbuf() to any stream, if
you do it soon enough. There''s nothing special about the
predefined streams in this regard.

Finally, these functions are really only "suggestions"
to the I/O library, and the library is at liberty to modify
them or even to ignore them. Implementations differ, and
the implementor (for some strange reason) often believes he
has more knowledge of the underpinnings of the implementation
than does the programmer ...

--
Er*********@sun.com


Eric Sosman wrote:

santosh wrote On 01/05/07 11:46,:

Which situations call for the use of setvbuf(), specifically when you
supply the buffer yourself? What is the advantage of an user specified
buffer over an automatically allocated one? Can we call setvbuf() on
the predefined streams, immediatly upon start of main()? How often is
this function used in typical C programs?

<snip explanation>

Finally, these functions are really only "suggestions"
to the I/O library, and the library is at liberty to modify
them or even to ignore them.

I can understand ignoring a call by returning a non-zero value but
won''t modifying silently the semantics of a call mislead the calling
code? It seems more in keeping with the spirit of C to either honour a
request or to fail.

Anway thanks for your explanation.

Implementations differ, and
the implementor (for some strange reason) often believes he
has more knowledge of the underpinnings of the implementation
than does the programmer ...

In most cases they probably do.


santosh wrote:

Which situations call for the use of setvbuf(), specifically when you
supply the buffer yourself? What is the advantage of an user specified
buffer over an automatically allocated one? Can we call setvbuf() on
the predefined streams, immediatly upon start of main()? How often is
this function used in typical C programs?

Usually, when I call setvbuf() it is to turn off buffering for stdin
and/or stdout. An example of when this is needed is for Winboard
protocol chess engines which exchange text command streams with a chess
protocol server. If the I/O is buffered, a command may not get flushed
and the engine can stall for no apparent reason.

Sometimes, I want to increase the size of the buffer. Sequentially
scanning a file is an example where this can be handy. However, the
buffer size allowed is so tiny that I usually resort to OS specific
routines instead, because the tiny buffers allowed by setvbuf() do not
afford any big boost.


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

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