“术语不带缓冲指的是每个read和write都调用嗯内核中的一个系统调用。所有的磁盘I/O都要经过内核的块缓冲(也称内核的缓冲区高速缓存),唯一例外的是对原始磁盘设备的I/O。既然read或write的数据都要被内核缓冲,那么术语“不带缓冲的I/O“指的是在用户的进程中对这两个函数不会自动缓冲,每次read或write就要进行一次系统调用。”--------摘自<unix环境编程>
相关的源码示例如下:
#i nclude <unistd.h>
#i nclude <iostream>
#i nclude <fcntl.h>
#i nclude <string>
#i nclude <sys/types.h>
#i nclude <sys/stat.h>
#i nclude <iostream>
#i nclude <fcntl.h>
#i nclude <string>
#i nclude <sys/types.h>
#i nclude <sys/stat.h>
using namespace std;
int main(){
int fd;
FILE *file;
char *s="hello,world\n";
if((fd=open("test.txt",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1){
}
if((file=fopen("test2.txt","w"))==NULL){
cout<<"Error Open File."<<endl;
}
cout<<"File has been Opened."<<endl;
sleep(15);
if(write(fd,s,strlen(s))<strlen(s)){
}
if(fwrite(s,sizeof(char),strlen(s),file)<strlen(s)){
}
cout<<"After write"<<endl;
sleep(15);
cout<<"After sleep."<<endl;
cout<<"After sleep."<<endl;
close(fd);
return 0;
}
return 0;
}