Here is an example of working code which shows how this works on a Linux system. You can see from all of the read
and write
calls that there's no buffer. It reads and writes one at a time.
$ cat c-read-buffer-test.c
#include <stdio.h>
int main() {
char input[8] = {0};
const size_t input_len = sizeof input;
size_t i;
int inC;
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
for (i = 0; i < input_len; ++i) {
inC = getchar();
input[i] = inC;
}
for (i = 0; i < input_len; ++i) {
if (i > 0)
printf(" ");
printf("%c", input[i]);
}
printf("\n");
return 0;
}
$ echo abcdefgh | strace ./c-read-buffer-test
execve("./c-read-buffer-test", ["./c-read-buffer-test"], 0x7fffd97152e0 /* 61 vars */) = 0
brk(NULL) = 0x48a20000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=197658, ...}) = 0
mmap(NULL, 197658, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fffab5b0000
close(3) = 0
openat(AT_FDCWD, "/lib64/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0\25\0\1\0\0\0\0n\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=882496, ...}) = 0
mmap(NULL, 279840, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fffab560000
mmap(0x7fffab590000, 131072, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x20000) = 0x7fffab590000
close(3) = 0
openat(AT_FDCWD, "/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0\25\0\1\0\0\0\220P\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=6723976, ...}) = 0
mmap(NULL, 2118520, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fffab350000
mmap(0x7fffab540000, 131072, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e0000) = 0x7fffab540000
close(3) = 0
mprotect(0x7fffab540000, 65536, PROT_READ) = 0
mprotect(0x7fffab590000, 65536, PROT_READ) = 0
mprotect(0x10010000, 65536, PROT_READ) = 0
mprotect(0x7fffab640000, 65536, PROT_READ) = 0
munmap(0x7fffab5b0000, 197658) = 0
set_tid_address(0x7fffab653110) = 58832
set_robust_list(0x7fffab653120, 24) = 0
rt_sigaction(SIGRTMIN, {sa_handler=0x7fffab566630, sa_mask=[], sa_flags=SA_SIGINFO}, NULL, 8) = 0
rt_sigaction(SIGRT_1, {sa_handler=0x7fffab566740, sa_mask=[], sa_flags=SA_RESTART|SA_SIGINFO}, NULL, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0
prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0
read(0, "a", 1) = 1
read(0, "b", 1) = 1
read(0, "c", 1) = 1
read(0, "d", 1) = 1
read(0, "e", 1) = 1
read(0, "f", 1) = 1
read(0, "g", 1) = 1
read(0, "h", 1) = 1
write(1, "a", 1a) = 1
write(1, " ", 1 ) = 1
write(1, "b", 1b) = 1
write(1, " ", 1 ) = 1
write(1, "c", 1c) = 1
write(1, " ", 1 ) = 1
write(1, "d", 1d) = 1
write(1, " ", 1 ) = 1
write(1, "e", 1e) = 1
write(1, " ", 1 ) = 1
write(1, "f", 1f) = 1
write(1, " ", 1 ) = 1
write(1, "g", 1g) = 1
write(1, " ", 1 ) = 1
write(1, "h", 1h) = 1
write(1, "\n", 1
) = 1
exit_group(0) = ?
+++ exited with 0 +++