模拟缓冲池(Buffer Pool)
◼ 基本思想: 对于既可用于输入又可用于输出的公用缓冲池, 其中至少应含有以下三种类型的缓冲区:
① 空(闲)缓冲区;
②装满输入数据的缓冲区;
③装满输出数据的缓冲区。
为了管理上的方便,可将相同类型的缓冲区链成一个队列,于是可形成以下三个队列:
(1)空缓冲队列 emq:由空缓冲区所链成的队列。
(2)输入队列 inq:由装满输入数据的缓冲区所链成的队列。
(3)输出队列 outq:由装满输出数据的缓冲区所链成的队列。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Buff_MAX 10 //初始空闲缓冲区的个数
typedef struct buffernode
{
int buf_type;
//"0"空闲缓冲区,"-1"装输入数据的缓冲区,"1"装输出数据的缓冲区
int buf_data; //循环区数据
struct buffernode *next; //指向下一个指针
} node;
node *tempnode; //全局变量
/********************************************************************
create 函数说明:建立空闲缓冲队列,每个空闲缓冲区内的数据存放“-32768”。
返回值: 返回队列的头指针
********************************************************************/
node *creat( ) /*建立空闲缓冲队列*/
{
int n=Buff_MAX;
node *head=(node*)malloc(sizeof(node));
node *p;
node *pre=(node*)malloc(sizeof(node));
head=pre;
while(n--){
p=(node*)malloc(sizeof(node));
p->buf_type=0;
p->buf_data=-32768;
pre->next=p;
pre=pre->next;
}
pre->next=NULL;
return head;
}
/********************************************************************
print_buf 函数说明:该函数打印三个缓冲队列的数据情况
返回值: 无
********************************************************************/
void print_buf(node *emq, node *inq, node *outq)
{
node *p=(node*)malloc(sizeof(node));
printf("空闲缓冲区队列emq内容:\n");
p=emq->next;
while(p){
printf("%7d",p->buf_data);
p=p->next;
}
printf("\n\n输入缓冲区队列inq;\n");
if(!inq->next){
printf("Input buffer is empty.");
}
else{
p=inq->next;
while(p){
printf("%5d",p->buf_data);
p=p->next;
}
}
printf("\n\n输出缓冲区队列inq;\n");
if(!outq->next){
printf("Output buffer is empty.");
}
else{
p=outq->next;
while(p){
printf("%5d",p->buf_data);
p=p->next;
}
}
printf("\n\n");
}
/********************************************************************
Getbuf 函数说明: 该函数从队首读一个数据出来,使用 tempnode 指向队首第一个结点
返回值:队列指针 head
********************************************************************/
node *Getbuf(node *head)
{
tempnode = head->next;
if(!head->next){
return head;
}
if(!head->next->next){
head->next=NULL;
return head;
}
head->next = head->next->next;
return head;
}
/********************************************************************
Putbuf 函数说明: 该函数从插入一个结点到队未。
返回值:队列指针 head
********************************************************************/
node *Putbuf(node *head, node *newnode)
{
node *p=(node*)malloc(sizeof(node));
if(!head->next){
head->next=newnode;
newnode->next=NULL;
return head;
}
p=head->next;
while(p->next){
p=p->next;
}
p->next=newnode;
newnode->next=NULL;
return head;
}
/**************MAIN 程序**********************/
int main(int argc, char *argv[])
{
int flag;
node *emq;
node *inq=(node*)malloc(sizeof(node));
node *outq=(node*)malloc(sizeof(node));
node *hin,*sin,*sout,*hout;
emq=creat();
inq->next=NULL;
outq->next=NULL;
print_buf(emq,inq,outq);
printf("\n######################\n");
printf("what do you want to do?\n");
printf("1. 收容输入\n");
printf("2. 提出输入\n");
printf("3. 收容输出\n");
printf("4. 提出输出\n");
printf("5. 退出\n");
printf("\nInput your choice:\n");
scanf("%d",&flag);
while(flag !=5) //"0",空闲缓冲区;"-1",输入数据的缓冲区;"1",输出数据的缓冲区
{
switch(flag){
case 1:
printf("收容输入--请输入\"输入数据\":");
emq=Getbuf(emq);
hin=tempnode;
hin->buf_type=-1;
scanf("%d",&hin->buf_data);
inq=Putbuf(inq, hin);
print_buf(emq,inq,outq);
break;
case 2:
inq=Getbuf(inq);
sin=tempnode;
if(!sin)
printf("Input buffer is empty!Error!\n");
else{
sin->buf_type=0;
sin->buf_data=-32768;
emq=Putbuf(emq,sin);
}
print_buf(emq,inq,outq);
break;
case 3:
printf("收容输出--请输入\"输出数据\":");
emq=Getbuf(emq);
hout=tempnode;
hout->buf_type=1;
scanf("%d",&hout->buf_data);
outq=Putbuf(outq, hout);
print_buf(emq,inq,outq);
break;
case 4:
outq=Getbuf(outq);
sout=tempnode;
if(!sout)
printf("Output buffer is empty!Error!\n");
else{
sout->buf_type=0;
sout->buf_data=-32768;
emq=Putbuf(emq,sout);
}
print_buf(emq,inq,outq);
break;
case 5:
return 0;
default:
break;
}
printf("\n######################\n");
printf("what do you want to do?\n");
printf("1. 收容输入\n");
printf("2. 提出输入\n");
printf("3. 收容输出\n");
printf("4. 提出输出\n");
printf("5. 退出\n");
printf("\nInput your choice:\n");
scanf("%d",&flag);
}
return 0;
}