高响应比算法_第1页
高响应比算法_第2页
高响应比算法_第3页
高响应比算法_第4页
高响应比算法_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

#include#include#define buffersize 5 /5个缓冲区int processnum=0; /初始化产品数量struct pcb /定义进程控制块PCBint flag;int numlabel;char product;char state;struct pcb* processlink;*exe=NULL,*over=NULL;typedef struct pcb PCB;PCB* readyhead=NULL,* readytail=NULL;PCB* consumerhead=NULL,* consumertail=NULL;PCB* producerhead=NULL,* producertail=NULL;int productnum=0; /产品数量int full=0,empty=buffersize; /信号量char bufferbuffersize; /缓冲区int bufferpoint=0; /缓冲区指针void linklist(PCB* p,PCB* listhead) /创建就绪队列PCB*cursor=listhead;while(cursor-processlink!=NULL)cursor=cursor-processlink;cursor-processlink=p;void freelink(PCB* linkhead)PCB* p;while(linkhead!=NULL)p=linkhead;linkhead=linkhead-processlink;free(p);void linkqueue(PCB* process,PCB* tail) /初始化队列if(* tail)!=NULL)(*tail)-processlink=process; (*tail)=process;elseprintf(队列序列化!);PCB* getq(PCB * head,PCB * tail)PCB* p;p=head-processlink;if(p!=NULL)head-processlink=p-processlink;p-processlink=NULL;if(head-processlink=NULL)(*tail)=head;else return NULL;return p;bool processproc() /初始化进程int i,f,num;char ch;PCB* p=NULL;PCB* p1=NULL;printf(n请输入希望产生的进程个数:);scanf(%d,&num);getchar();for(i=0;iflag=f;processnum+;p-numlabel=processnum;p-state=w;p-processlink=NULL;if(p-flag=1)printf(您要产生的进程是生产者,它是第%d个进程。请您输入您要该进程产生的字符:n,processnum);scanf(%c,&ch);getchar();p-product=ch;productnum+;printf(您要该进程产生的字符是%cn,p-product);elseprintf(您要生产的进程是消费者,它是第%d个进程。n,p-numlabel);linkqueue(p,&readytail);return true;bool hasElement(PCB *pro) /判断队列中是否有进程存在if(pro-processlink=NULL) return false;else return true;bool waitempty() /判断生产者等待队列是否为空if(emptynumlabel);linkqueue(exe,&producertail);return false;else empty-; return true;void signalempty() /唤醒生产者进程PCB* p;if(hasElement(producerhead)p=getq(producerhead,&producertail);linkqueue(p,&readytail);printf(等待中的生产者进程进入就绪队列,它的进程号是%dn,p-numlabel);empty+;bool waitfull() /判断消费者等待进程队列是否为满if(fullnumlabel);linkqueue(exe,&consumertail);return false;else full-; return true;void signalfull() /唤醒消费者进程PCB* p;if(hasElement(consumerhead) p=getq(consumerhead,&consumertail);linkqueue(p,&readytail);printf(等待中的消费者进程进入就绪队列,它的进程号是%dn,p-numlabel);full+;void producerrun() /生产者进程if(!waitempty()return;printf(进程%d开始向缓冲区存数%cn,exe-numlabel,exe-product);bufferbufferpoint=exe-product;bufferpoint+;printf(进程%d向缓冲区存数操作结束n,exe-numlabel);signalfull();linklist(exe,over);void comsuerrun() /消费者进程if(!waitfull()return;printf(进程%d向缓冲区取数n,exe-numlabel);exe-product=bufferbufferpoint-1;bufferpoint-;printf(进程%d向缓冲区取数操作结束,取数是%cn,exe-numlabel,exe-product);signalempty();linklist(exe,over);void display(PCB* p) /显示进程p=p-processlink;while(p!=NULL)printf(进程%d,它是一个,p-numlabel);p-flag=1?printf(生产者n):printf(消费者n);p=p-processlink;void main()char terminate;bool element;printf(你想开始程序吗?(y/n);scanf(%c,&terminate); getchar();readyhead=(PCB*)malloc(sizeof(PCB); /初始化队列if(readyhead=NULL)return;readytail=readyhead;readyhead-flag=3;readyhead-numlabel=processnum;readyhead-state=w;readyhead-processlink=NULL;consumerhead=(PCB*)malloc(sizeof(PCB);if(consumerhead=NULL)return;consumertail=consumerhead;consumerhead-processlink=NULL;consumerhead-flag=4;consumerhead-numlabel=processnum;consumerhead-state=w;consumerhead-processlink=NULL;producerhead=(PCB*)malloc(sizeof(PCB);if(producerhead=NULL)return;producertail=producerhead;producerhead-processlink=NULL;producerhead-flag=5;producerhead-numlabel=processnum;producerhead-state=w;producerhead-processlink=NULL;over=(PCB*)malloc(sizeof(PCB);if(over=NULL)return;over-processlink=NULL;while(terminate=y)if(!processproc() break;element=hasElement(readyhead);while(element)exe=getq(readyhead,&readytail);printf(进程%d申请运行,它是一个,exe-numlabel);exe-flag=1? printf(生产者n):printf(消费者n);if(exe-flag=1) producerrun();else comsuerrun();element=hasElement(readyhead);printf(就绪队列没有进程n);if(hasElement(consumerhead)printf(消费者等待队列中有进程:n);display(consumerhead);elseprintf(消费者等待队列中没有进程n);if(hasElement(producerhead)printf(生产者等待队列中有进程:n);display(producerhead);elseprintf(生产者等待队列中没有进程n);printf(你想继续吗?(press Y for on);scanf(%c,&terminate);getchar();printf(nn进程模拟完成.n)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论