#include<stdio.h>
#include<stdlib.h>
typedef struct {
char * buf;
int head;
int tail;
int size;
} fifo_t;
//This initializes the FIFO structure with the given buffer and size
void fifo_init(fifo_t * f, char * buf, int size){
f->head = 0;
f->tail = 0;
f->size = size;
f->buf = buf;
}
//This reads nbytes bytes from the FIFO
//The number of bytes read is returned
int fifo_read(fifo_t * f, void * buf, int nbytes){
int i;
char * p;
p = buf;
for(i=0; i < nbytes; i++){
if( f->tail != f->head ){ //see if any data is available
*p++ = f->buf[f->tail]; //grab a byte from the buffer
f->tail++; //increment the tail
if( f->tail == f->size ){ //check for wrap-around
f->tail = 0;
}
} else {
return i; //number of bytes read
}
}
return nbytes;
}
//This writes up to nbytes bytes to the FIFO
//If the head runs in to the tail, not all bytes are written
//The number of bytes written is returned
int fifo_write(fifo_t * f, const void * buf, int nbytes){
int i;
const char * p;
p = buf;
for(i=0; i < nbytes; i++){
//first check to see if there is space in the buffer
if( (f->head + 1 == f->tail) || ( (f->head+1 == f->size) && (f->tail == 0) )){
return i; //no more room
} else {
f->buf[f->head] = *p++;
f->head++; //increment the head
if( (f->head == f->size)){ //check for wrap-around
f->head = 0;
}
}
}
return nbytes;
}
int main(){
fifo_t f;
char wr_buffer[10]={'a','n','v','s','d'};
char rd_buffer[10];
char * fifo_buffer;
int size, nb_wr, nb_rd, i;
printf("donner la taille du buffer :\n");
scanf("%d",&size);
fifo_buffer = malloc(size * sizeof(char));
fifo_init(&f, fifo_buffer, size);
nb_wr = fifo_write(&f, wr_buffer, 4);
nb_rd = fifo_read(&f, rd_buffer, 6);
printf("\n nombre de trames ecrites : \t %d \n",nb_wr);
for(i=0;i<nb_wr;i++){
printf("\t - trame %d : %c \n", i, f.buf[i]);
}
printf("\n nombre de trames lues : \t %d \n",nb_rd);
for(i=0;i<nb_rd;i++){
printf("\t - trame %d : %c \n", i, rd_buffer[i]);
}
}

小波思基
- 粉丝: 90
- 资源: 1万+
最新资源
- Ollama安装包Mac版
- 【人工智能比赛获奖源码】+【PyQt5混元大模型】+【桌面聊天应用】+【效率辅助工具】
- 三相VIENNA整流器Simulink仿真详解:输入电压与输出电压规格化设定,高效率与精准控制特性的系统分析展示,三相VIENNA整流技术详解:Simulink仿真分析与电路设计特点 输入220V
- 是德Keysight Infiniium MXR/EXR-Series Oscilloscopes使用说明书下载
- 按年龄和国家划分的全球平均人体身高.zip
- 《基于多时段动态电价策略优化电动汽车有序充电,实现电网负荷平衡与用户充电成本节约》,《基于多时段动态电价策略与粒子群算法的电动汽车有序充电优化》,《基于多时段动态电价的电动汽车有序充电策略优化》 平台
- LabVIEW与YOLOv5融合:多模型并行推理的ONNX Runtime封装DLL,支持视频、图片双模式CPU/GPU切换式识别,实现灵活选择高性价比推演模式,可迅速标注及高效训练,LabVIEW与
- Chatbox MAC安装包
- 质心侧偏角与横摆角速度相平面法在车辆动力学控制中的协调应用与程序实现,车辆动力学控制的质心侧偏角与横摆角速度相平面法研究及程序实现,相平面法,车辆动力学控制,协调控制使用,质心侧偏角-横摆角速度相平面
- IIS假死监视工具,发现假死就重启iis和释放程序池
- FPGA采集CameraLink相机Full模式解码输出方案:从输入到HDMI高清视频输出的实现流程,FPGA采集CameraLink相机Full模式解码输出实现方案:从相机输入到HDMI视频输出精细
- c++-继承与派生-例题源代码
- 【毕业设计参考】AVR寻迹小车.rar
- springcloud
- 波特兰建筑许可数据.zip
- Vuforia package-10-28-4
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


