1、知识点
实验1
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
printf("hello world!\n");
write (1, "hahaha\n", 7);
if(fork() == -1){
perror("fork");
exit(1);
}
}
执行结果:
结果分析:(输出到终端和log文件结果不同)
1、因为用户空间有I/O缓存,内核空间有页缓存
I/O缓存分三种模式:
块缓存:固定字节的缓冲区大小,比如和文件相关的流都是块缓存;标准I/O称块缓存为完全缓存;
行缓存:遇到换行符,缓冲区的数据会拷贝到内核缓冲区
无缓存:数据直接拷贝到内核缓冲区;如标准错误stderr采用无缓冲模式
2、直接执行a.out输出到终端,是因为库函数printf输出到终端采用行缓存,遇到换行符直接输出到终端;write系统调用没有采用缓存,直接写到内核缓冲区;
3、输出到log文件,文件采用块缓存,printf的数据先缓存在用户空间的I/O缓冲区,write系统调用没有采用缓存,直接写到内核缓冲区;所以write的内容先显示,hello world显示两次是因为fork了I/O缓冲区;然后等到退出时,刷新缓冲区到内核;
实验2
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
printf ("hello world!\n");
// fflush (stdout);
write (1, "hahaha\n", 7);
int ret_from_fork;
ret_from_fork = fork ();
if (ret_from_fork == -1)
{
perror ("fork");
exit (1);
}
else if (ret_from_fork == 0)
{
_exit (0);
}
return 0;
}
执行结果: