Fully buffered 和 Line buffered
来自apue的5.4和8.3章节
1.例子
在linux中运行如下代码:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
int main() {
printf("hello\n");
if (fork() == 0)
// child
printf("hello from child\n");
else
// parent
printf("hello from parent\n");
exit(0);
}
tianlan@tianlan-virtual-machine:~/c/test$ ./a.out
hello
hello from parent
hello from child
将输出重定向到res.txt文件中
tianlan@tianlan-virtual-machine:~/c/test$ ./a.out > res.txt
tianlan@tianlan-virtual-machine:~/c/test$ more res.txt
hello
hello from parent
hello
hello from child
此时可以看到hello输出了两次
2.全缓存(fully buffered)和行缓存(line buffered)以及不缓存(no buffered)
在标准c中的缓存有全缓存、行缓存、不缓存之分。
全缓存:缓存满或者调用fflush时,冲刷缓存内容(意味着把缓存中的内容都写出去)。程序从main中返回(return)和调用 exit(0) 是一样的,调用 exit 会自动关闭流并且冲刷(fflush)缓存中的内容。
行缓存:标准输出流遇到换行符\n时冲刷缓存。
子进程是父进程的一个副本,拥有和父进程一样的数据、堆、栈。父进程中打开的流,子进程也打开。所以当输出到终端时,第一个printf执行完就立马冲刷缓存,不会输出第二次。而将输出重定向到一个文件,程序一直到结束没有主动调用 fflush(),所以最后调用exit()时负责调用 fclose() 关闭流和fflush() 冲刷缓存内容,所以输出了两次。
Unix中read和write都是不带缓存的。
> man 2 read
> man 2 write
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
ssize_t read(int fd, void *buf, size_t count);
ISO中的标准输入流(stdin)输出流(stdout)在指向终端(terminal)时是行缓存,其他情况一般都是全缓存。
使用fflush
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
int main() {
printf("hello\n");
// use fflush to flush stdout
fflush(stdout);
if (fork() == 0)
// child
printf("hello from child\n");
else
// parent
printf("hello from parent\n");
exit(0);
}
tianlan@tianlan-virtual-machine:~/c/test$ ./a.out > res.txt
tianlan@tianlan-virtual-machine:~/c/test$ more res.txt
hello
hello from parent
hello from child