缓冲区混乱
谁能澄清程序使用的缓冲区类型?
例如:
我有一个从标准输入读取到标准输出的 C 程序。
这里涉及到哪些缓冲区?我知道有2个。 一种由内核提供的用户没有任何控制权的。 一种提供标准流,即 stdout、stdin 和 stderr。每个都有一个单独的缓冲区。
我的理解正确吗?
谢谢, 约翰
Could anyone clarify on the types of buffers used by a program?
For eg:
I have a C program that reads from a stdin to stdout.
What are the buffers involved here? I'm aware that there are 2.
One provided by the kernel on which a user don't have any control.
One provided with standard streams namely stdout, stdin and stderr. Each having a separate buffer.
Is my understanding correct?
Thanks,
John
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是
linux/unix
,那么您可以更容易地理解存在三个流,即1.STDIN: FILE DESCRIPTOR VALUE
0
(IN unix
)2.STDOUT:文件描述符值
1
3.STDERR:文件描述符值
2默认
情况下,这些流对应于键盘和监视器。在 unix 中,我们可以更改这些流以从文件而不是键盘读取输入。要使用 close()、dup() 在文件而不是监视器上显示输出系统调用。是的,涉及3个缓冲区。要清除c中输入缓冲区的内容,我们使用fflush()函数。
如果您想了解有关在 UNIX 中处理这些流的更多信息,请告诉我。
If you are working on
linux/unix
then you could more easily understand that there are three streams namely1.STDIN: FILE DESCRIPTOR VALUE
0
(IN unix
)2.STDOUT :FILE DESCRIPTOR VALUE
1
3.STDERR :FILE DESCRIPTOR VALUE
2
By default these streams correspond to keyboard and monitor.In unix we can change these streams to read input from file instead of keyboard.To display output on a file rather than monitor using close(),dup() system calls.Yes there are 3 buffers involved.To clear the contents of input buffer in c we use fflush() function.
If you want to know more about handling these streams in UNIX then let me Know.
内核(或其他底层系统)可以有任意数量的缓冲层,具体取决于从哪个设备读取数据以及内核实现的细节;在某些系统中,该级别没有缓冲,数据直接读入用户空间缓冲区。
stdio库为stdin分配一个缓冲区;大小取决于实现,但您可以控制大小,甚至可以通过 setvbuf 使用您自己的缓冲区。它还允许您控制 I/O 是否完全缓冲(将尽可能多的数据读入缓冲区)、行缓冲(仅在遇到换行符之前读取数据)或不缓冲。如果系统可以确定输入是终端,则默认为行缓冲,否则为完全缓冲。
标准输出的情况类似。 stderr 默认情况下是无缓冲的。
The kernel (or other underlying system) could have any number of layers of buffering, depending on what device is being read from and the details of the kernel implementation; in some systems there is no buffering at that level, with the data being read directly into the userspace buffer.
The stdio library allocates a buffer for stdin; the size is implementation-dependent but you can control the size and even use your own buffer with setvbuf. It also allows you to control whether I/O is fully buffered (as much data is read into the buffer as is available), line buffered (data is is only read until a newline is encountered), or unbuffered. The default is line buffering if the system can determine that the input is a terminal, else fully buffered.
The story is similar for stdout. stderr is by default unbuffered.