[英]c setvbuf() function call not working as expected
I'm learning system programming and when I run the following code我正在学习系统编程,当我运行以下代码时
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char buff[5];
setvbuf(stderr, buff, _IOFBF, 5);
for (int i = 1; i < 10; i++) {
fprintf(stderr, "%d", i);
}
_exit(0);
}
setvbuf
is imposing fully buffered policy on stderr
with a 5-byte sized buffer, so I expected to get 12345
but instead it prints 1234567
, why is that? setvbuf
正在使用 5 字节大小的缓冲区对stderr
强加完全缓冲的策略,所以我希望得到12345
但它打印1234567
,这是为什么?
strace-ing the program shows that when compiled against glibc it first does a write of size 1, then a write of size 5, and finally a write of size 1: strace-ing 程序显示,当针对 glibc 编译时,它首先写入大小为 1,然后写入大小为 5,最后写入大小为 1:
$ strace -o /dev/stdout -e write,writev ./a.out 2>/dev/null
write(2, "1", 1) = 1
write(2, "23456", 5) = 5
write(2, "7", 1) = 1
With musl it does 7 writes of size 1:使用 musl,它执行 7 次大小为 1 的写入:
$ strace -o /dev/stdout -e write,writev ./a.out 2>/dev/null
writev(2, [{iov_base="1", iov_len=1}, {iov_base=NULL, iov_len=0}], 2) = 1
writev(2, [{iov_base="2", iov_len=1}, {iov_base=NULL, iov_len=0}], 2) = 1
writev(2, [{iov_base="3", iov_len=1}, {iov_base=NULL, iov_len=0}], 2) = 1
writev(2, [{iov_base="4", iov_len=1}, {iov_base=NULL, iov_len=0}], 2) = 1
writev(2, [{iov_base="5", iov_len=1}, {iov_base=NULL, iov_len=0}], 2) = 1
writev(2, [{iov_base="6", iov_len=1}, {iov_base=NULL, iov_len=0}], 2) = 1
writev(2, [{iov_base="7", iov_len=1}, {iov_base=NULL, iov_len=0}], 2) = 1
writev(2, [{iov_base="8", iov_len=1}, {iov_base=NULL, iov_len=0}], 2) = 1
writev(2, [{iov_base="9", iov_len=1}, {iov_base=NULL, iov_len=0}], 2) = 1
+++ exited with 0 +++
So it's obvious that both C library implementations treat the size
argument of setvbuf(stream, buf, _IOFBF, size)
as a maximum, and they feel free to flush the buffer even before it's full.因此,很明显,两个 C 库实现都将setvbuf(stream, buf, _IOFBF, size)
的size
参数视为最大值,并且即使在缓冲区已满之前,它们也可以随意刷新缓冲区。
AFAICS there doesn't seem to be anything in the standard against this interpretation. AFAICS 标准中似乎没有任何内容反对这种解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.