2008-04-07

缓冲区管理

//假设系统中有两个分别名为P1、P2的进程
//两台均可进行输入、输出操作的设备d1、d2
//两个进程可以从两台设备中的任何一台输入数据,并可以向其中的任何一台设备输出数据。

#include
#include
#include
#include
#include


#define NUMOFBUFFER 10
#define SIZEOFBUFFER 100
#define NO -1
#define MAX 200


struct buffer{ //系统中的缓冲区
int count; //缓冲区中现在的字符个数
char databuffer[SIZEOFBUFFER]; //数据区
struct buffer *p; //用作队列指针
}buf[NUMOFBUFFER];


typedef struct queue{ //队列
int buf[NUMOFBUFFER]; //保存队列中的元素
int front; //头指针
int rear; //尾指针
int currentSize; //当前长度
int maxSize; //最大长度
}SqQueue;


SqQueue emptyBuffer;
SqQueue outputOfDevice1;
SqQueue outputOfDevice2;
SqQueue inputOfDevice1;
SqQueue inputOfDevice2;


int mutex = 1;
int od1,od2,id1,id2;

int mistake = 0;


//初始化空队列
void InitEmptyQueue(SqQueue &L){
int i;
for(i = 0;i <>
L.buf[i] = i;
L.front = 0;
L.rear = 9;
L.currentSize = 10;
L.maxSize = NUMOFBUFFER;
}


//初始化队列
void InitQueue(SqQueue &L){
int i;
for(i = 0;i <>
L.buf[i] = NO;
L.front = 0;
L.rear = L.front - 1;
L.currentSize = 0;
L.maxSize = NUMOFBUFFER;
}


//P操作
void Pprocess(int source){
source --;
if(source <>
printf("Sorry.There is no empty buffer.Please wait.:-(\n");
mistake = 1;
}
}


//V操作
void Vprocess(int source){
source ++;
if(source > 0)
mistake = 0;
}


//入队
void EnQueue(SqQueue &L,int buf){
Pprocess(mutex);

if(L.currentSize == 0){
L.front = 0;
L.rear = -1;
}

if(L.rear == 9) //队满的情况已经在主函数中做过处理
L.rear = fmod(L.rear + 1,10);
else
L.rear ++;
L.buf[L.rear] = buf;
L.currentSize ++;

Vprocess(mutex);
}


//出队
int DeQueue(SqQueue &L){ //队空的情况已经在主函数中做过处理
int e;

Pprocess(mutex);

e = L.buf[L.front];
L.buf[L.front] = NO;
L.front ++;
L.currentSize --;

Vprocess(mutex);

return e;
}


//完成缓冲区和各个队列的初始化操作
void init(){

//缓冲区初始化操作
int i,j;
for(i = 0;i <>
buf[i].count = 0;
for(j = 0;j <>
buf[i].databuffer[j] = ' ';
buf[i].p = &buf[i + 1];
}

//各个队列的初始化操作
InitEmptyQueue(emptyBuffer);
InitQueue(outputOfDevice1);
InitQueue(outputOfDevice2);
InitQueue(inputOfDevice1);
InitQueue(inputOfDevice2);
}


//从空队列中取出一个缓冲区
int getBuffer(){
int index;
int size = emptyBuffer.currentSize;

Pprocess(size);

if(mistake != 1){
Pprocess(mutex); //对空缓冲区队列进行互斥操作
index = DeQueue(emptyBuffer);
Vprocess(mutex);
}

return index;
}


//空队列中放入一个缓冲区
void putBuffer(int index){
int size = emptyBuffer.currentSize;

Pprocess(mutex); //对空缓冲区队列进行互斥操作
EnQueue(emptyBuffer,index);
Vprocess(mutex);

Vprocess(size);
}


//输出内容,格式控制函数
void print(){
int i = 0,k = 0,index;

printf("...........................................\n");
printf("...........................................\n");

//显示空缓冲区队列
printf("emptyBuffer : ");
if(emptyBuffer.currentSize == 0 || mistake == 1)
printf("NULL");
else{
i = emptyBuffer.front;
do{
index = fmod((i+k),10);
printf("%d ",emptyBuffer.buf[index]);
k ++;
}while(k <>
}
putchar('\n');

//显示设备一的输出队列
printf("outputOfDevice1 : ");
if(outputOfDevice1.currentSize == 0)
printf("NULL");
else{
i = outputOfDevice1.front;
k = 0;
do{
index = fmod((i+k),10);
printf("%d ",outputOfDevice1.buf[index]);
k ++;
}while(k <>
}
putchar('\n');

//显示设备二的输出队列
printf("outputOfDevice2 : ");
if(outputOfDevice2.currentSize == 0)
printf("NULL");
else{
i = outputOfDevice2.front;
k = 0;
do{
index = fmod((i+k),10);
printf("%d ",outputOfDevice2.buf[index]);
k ++;
}while(k <>
}
putchar('\n');

//显示设备一的输入队列
printf("inputOfDevice1 : ");
if(inputOfDevice1.currentSize == 0)
printf("NULL");
else{
i = inputOfDevice1.front;
k = 0;
do{
index = fmod((i+k),10);
printf("%d ",inputOfDevice1.buf[index]);
k ++;
}while(k <>
}
putchar('\n');

//显示设备二的输入队列
printf("inputOfDevice2 : ");
if(inputOfDevice2.currentSize == 0)
printf("NULL");
else{
i = inputOfDevice2.front;
k = 0;
do{
index = fmod((i+k),10);
printf("%d ",inputOfDevice2.buf[index]);
k ++;
}while(k <>
}
putchar('\n');

printf("...........................................\n");
}


//设备选择显示函数
int withTools(){
char dc = getch();
if(dc == '1')
printf("device1.");
else{
if(dc == '2')
printf("device2.");
else
printf("error.");
}
putchar('\n');
printf("...........................................\n");
int tool = (int)dc - 48;
return tool;
}


//线程选择显示函数
int withType(){
int dc = getch();
if(dc == '1')
printf("thread1.");
else{
if(dc == '2')
printf("thread2.");
else
printf("error.");
}
putchar('\n');
printf("...........................................\n");
int type = (int)dc - 48;
return type;
}


//完成进程的数据处理.
//按照inputdev指定的设备号从该设备的输入队列中取出一个缓冲区;
//按照procnum的序号对缓冲区中的数据进行指定的数据处理操作;
//按照outputdev指定的设备号将缓冲区放入该设备的输出队列。
void process(int procnum,int inputdev,int outputdev){
int index;

switch (inputdev){
case 1:
id1 = inputOfDevice1.currentSize;
Pprocess(id1);
index = DeQueue(inputOfDevice1);
break;
case 2:
id2 = inputOfDevice2.currentSize;
Pprocess(id2);
index = DeQueue(inputOfDevice2);
break;
}

strupr(buf[index].databuffer); //小写字母转换成大写字母

switch (outputdev){
case 1:
od1 = outputOfDevice1.currentSize;
EnQueue(outputOfDevice1,index);
Vprocess(od1);
break;
case 2:
od2 = outputOfDevice2.currentSize;
EnQueue(outputOfDevice2,index);
Vprocess(od2);
break;
}

printf("thread: %d---inputdevice: %d---outputdevice: %d\n",procnum,inputdev,outputdev);
print();
}


//完成设备的读写操作.其中参数devnum指出需要进行I/O操作的设备,flag为操作类型
void deviceReadAndWrite(int devnum,int flag){
int index;
char input[SIZEOFBUFFER];

if(flag == 1){ //读操作
index = getBuffer(); //从空队列中取出一个缓冲区
if(mistake == 1){
printf("You must make a full buffer empty.:-(\n");
goto lab;
}
else{
printf("Please input some letters : ");
scanf("%s",input);
strcpy(buf[index].databuffer,input); //从终端读入一行字符串,放入缓冲区的数据区
buf[index].count = strlen(input);
if(devnum == 1){ //按照指定的设备号将缓冲区放入相应的设备输入队列
id1 = inputOfDevice1.currentSize;
EnQueue(inputOfDevice1,index);
Vprocess(id1);
}
else{
id2 = inputOfDevice2.currentSize;
EnQueue(inputOfDevice2,index);
Vprocess(id2);
}
}
}
else{ //写操作
if(devnum == 1){
od1 = outputOfDevice1.currentSize;
Pprocess(od1); //p(od1)
index = DeQueue(outputOfDevice1); //按照指定的设备号,从设备的输出队列中取出一个装满数据的缓冲区
printf("The contents of this buffer is : ");
printf("%s\n",buf[index].databuffer); //将缓冲区中的数据在终端上显示
putBuffer(index); //将空缓冲区放入空缓冲区队列
}
if(devnum == 2){
od2 = outputOfDevice2.currentSize;
Pprocess(od2); //p(od2)
index = DeQueue(outputOfDevice2); //按照指定的设备号,从设备的输出队列中取出一个装满数据的缓冲区
printf("The contents of this buffer is : ");
printf("%s\n",buf[index].databuffer); //将缓冲区中的数据在终端上显示
putBuffer(index); //将空缓冲区放入空缓冲区队列
}
}

lab:
print();
}


//代码使用控制函数
void getIn(){
char password[7] = {'0','5','2','2','2','6'};
char input[10];
int i = 0;

printf("-------------------------------------------\n");
printf("-------------------------------------------\n");


printf("Password : ");
char c = getch();

while(c != '*'){
input[i] = c;
i ++;
putchar('*');
c = getch();
}
input[i] = '\0';
putchar('\n');

if(strcmp(password,input) != 0){
printf("You do not have the right to use this code. :-(\n");
exit(0);
}

printf("-------------------------------------------\n");
printf("-------------------------------------------\n");
}


//模式选择函数
int printTip(){
printf("This code support three work models : \n");
printf("A.input B.output C.deal D.exit \n");
printf("Please choose : ");
char c = getch();
int model;

switch(c){
case 'A':
printf("input.\n");
model = 1;
break;
case 'B':
printf("output.\n");
model = 2;
break;
case 'C':
printf("deal.\n");
model = 3;
break;
case 'D':
printf("exit.\n");
model = 4;
break;
}
printf("...........................................\n");
return model;
}


//判断输出队列是否为空
int judgeOutput(int tool){
int emp; //0非空1空

switch (tool){
case 1:
if(outputOfDevice1.currentSize == 0)
emp = 1;
else
emp = 0;
break;
case 2:
if(outputOfDevice2.currentSize == 0)
emp = 1;
else
emp = 0;
break;
}
return emp;
}


//判断输入队列是否为空
int judgeInput(int tool){
int emp; //0非空1空
switch (tool){
case 1:
if(inputOfDevice1.currentSize == 0)
emp = 1;
else
emp = 0;
break;
case 2:
if(inputOfDevice2.currentSize == 0)
emp = 1;
else
emp = 0;
break;
}
return emp;
}


//判断输入队列是否为满
int judgeInputFull(int tool){
int full; //0非满1满
switch (tool){
case 1:
if(inputOfDevice1.currentSize == 10)
full = 1;
else
full = 0;
break;
case 2:
if(inputOfDevice2.currentSize == 10)
full = 1;
else
full = 0;
break;
}
return full;
}


//判断输出队列是否为满
int judgeOutputFull(int tool){
int full; //0非满1满
switch (tool){
case 1:
if(outputOfDevice1.currentSize == 10)
full = 1;
else
full = 0;
break;
case 2:
if(outputOfDevice2.currentSize == 10)
full = 1;
else
full = 0;
break;
}
return full;
}


void main(){
void InitEmptyQueue(SqQueue &L);
void InitQueue(SqQueue &L);
void EnQueue(SqQueue &L,int buf);
int DeQueue(SqQueue &L);
void init();
int getBuffer();
void putBuffer(int index);
void print();
int withTools();
int withType();
void process(int procnum,int inputdev,int outputdev);
void Pprocess(int source);
void Vprocess(int source);
void deviceReadAndWrite(int devnum,int flag);
void getIn();
int printTip();
int judgeOutput(int tool);
int judgeInput(int tool);
int judgeInputFull(int tool);
int judgeOutputFull(int tool);

int tool,thrd,model,tools;

getIn();
printf("Initiate : \n");
init();
print();

do{
model = printTip();
switch(model){
case 1: //输入
do{
printf("1:device1 2:device2\nPlease choose : ");
tool = withTools();
if(judgeInputFull(tool) == 1){ //判断输入队列是否为满
printf("The list of inputDevice%d is full.:-(\n",tool);
printf("Please input again.\n");
}
}while(judgeInputFull(tool) == 1);
deviceReadAndWrite(tool,model);
break;
case 2: //输出
do{
printf("1:device1 2:device2\nPlease choose : ");
tool = withTools();
if(judgeOutput(tool) == 1){ //判断输出队列是否为空
printf("The list of outputOfDevice%d is empty.:-(\n",tool);
printf("Please input again.\n");
}
}while(judgeOutput(tool) == 1);
deviceReadAndWrite(tool,model);
break;
case 3: //数据处理
printf("1:thread1 2:thread2\nPlease choose : ");
thrd = withType();
do{
printf("1:device1 2:device2\nThe device of input : ");
tool = withTools();
if(judgeInput(tool) == 1){ //判断输入队列是否为空
printf("The list of input of device%d is empty.:-(\n",tool);
printf("Please input again.\n");
}
}while(judgeInput(tool) == 1);
do{
printf("1:device1 2:device2\nThe device of output : ");
tools = withTools();
if(judgeOutputFull(tool) == 1){ //判断输出队列是否为满
printf("The list of output of device%d is full.:-(\n",tool);
printf("Please input again.\n");
}
}while(judgeOutputFull(tool) == 1);
process(thrd,tool,tools);
break;
case 4: //退出
break;
}
}while(model != 4);

printf("...........................................\n");
printf("...........................................\n");
printf("Thank you for using by 052226.:-)\n");
}

没有评论: