文章目录
前言
我们接下来要谈论的是我们语言层面的缓冲区(C,C++之类的),不是我们操作系统内核里面自带的缓冲区,我们每次在打开一个文件的时候,以C语言为例子,C语言会为我们所打开的这个文件分配一块缓冲区,用来缓存我们读写的数据`,这个缓冲区会被放在我们创建的FILE的结构体里面,里面存放着缓冲区的字段和维护信息
一、缓冲区刷新方法分类
a.无缓冲–直接刷新
b.行缓冲–不刷新,直到碰到\n才刷新
显示器写入一般采用的是行缓冲
c.全缓冲–缓冲区满了才刷新
文件写入一般采用的是全缓冲,缓冲区满了或者程序结束的时候刷新
二、 缓冲区的常见刷新问题
1.问题
我们将可执行文件内容重定向到log1里面
最后我们发现与C有关的接口被打印了两次,这是什么原因呢?
2.刷新本质
这些C接口最后写入内核缓冲区,本质都是调用write的系统接口
三、模拟实现
1.Mystdio.h
#include <string.h>
#define SIZE 1024
#define FLUSH_NOW 1//无缓冲
#define FLUSH_LINE 2//行缓冲
#define FLUSH_ALL 4//全缓冲
typedef struct IO_FILE{
int fileno;//文件描述符
int flag; //刷新方式
char outbuffer[SIZE]; // 简单模拟语言层缓冲区
int out_pos;//缓冲区当前大小
}_FILE;
_FILE * _fopen(const char*filename, const char *flag);
int _fwrite(_FILE *fp, const char *s, int len);
void _fclose(_FILE *fp);
2.Mystdio.c
#include "Mystdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#define FILE_MODE 0666//文件默认权限
_FILE * _fopen(const char*filename, const char *flag)
{
assert(filename);
assert(flag);
int f = 0;//文件的写入方式
int fd = -1;//文件描述符
if(strcmp(flag, "w") == 0) {
f = (O_CREAT|O_WRONLY|O_TRUNC);
fd = open(filename, f, FILE_MODE);
//获取文件描述符
}
else if(strcmp(flag, "a") == 0) {
f = (O_CREAT|O_WRONLY|O_APPEND);
fd = open(filename, f, FILE_MODE);
}
else if(strcmp(flag, "r") == 0) {
f = O_RDONLY;
fd = open(filename, f);
}
else
return NULL;
if(fd == -1) return NULL;
_FILE *fp = (_FILE*)malloc(sizeof(_FILE));
//创建文件指针结构体
if(fp == NULL) return NULL;
fp->fileno = fd;
//fp->flag = FLUSH_LINE;
fp->flag = FLUSH_ALL;
fp->out_pos = 0;
return fp;
}
int _fwrite(_FILE *fp, const char *s, int len)
{
// "abcd\n"
memcpy(&fp->outbuffer[fp->out_pos], s, len); // 没有做异常处理, 也不考虑局部问题
fp->out_pos += len;
if(fp->flag&FLUSH_NOW)//无缓冲
{
write(fp->fileno, fp->outbuffer, fp->out_pos);
fp->out_pos = 0;
}
else if(fp->flag&FLUSH_LINE)//行缓冲
{
if(fp->outbuffer[fp->out_pos-1] == '\n'){ // 不考虑其他情况
write(fp->fileno, fp->outbuffer, fp->out_pos);
fp->out_pos = 0;
}
}
else if(fp->flag & FLUSH_ALL)//全缓冲
{
if(fp->out_pos == SIZE){
write(fp->fileno, fp->outbuffer, fp->out_pos);
fp->out_pos = 0;
}
}
return len;
}
void _fflush(_FILE *fp)//手动刷新缓冲区
{
if(fp->out_pos > 0){
write(fp->fileno, fp->outbuffer, fp->out_pos);
fp->out_pos = 0;
}
}
void _fclose(_FILE *fp)
{
if(fp == NULL) return;
_fflush(fp);
close(fp->fileno);
free(fp);
}
3.main.c
#include "Mystdio.h"
#include <unistd.h>
#define myfile "test.txt"
int main()
{
_FILE *fp = _fopen(myfile, "a");
if(fp == NULL) return 1;
const char *msg = "hello world\n";
int cnt = 10;
while(cnt){
_fwrite(fp, msg, strlen(msg));
// fflush(fp);
sleep(1);
cnt--;
}
_fclose(fp);
return 0;
}