高响应比算法.docx

上传人:wj 文档编号:1216443 上传时间:2023-04-30 格式:DOCX 页数:9 大小:10.67KB
下载 相关 举报
高响应比算法.docx_第1页
第1页 / 共9页
高响应比算法.docx_第2页
第2页 / 共9页
高响应比算法.docx_第3页
第3页 / 共9页
高响应比算法.docx_第4页
第4页 / 共9页
高响应比算法.docx_第5页
第5页 / 共9页
高响应比算法.docx_第6页
第6页 / 共9页
高响应比算法.docx_第7页
第7页 / 共9页
高响应比算法.docx_第8页
第8页 / 共9页
高响应比算法.docx_第9页
第9页 / 共9页
亲,该文档总共9页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

高响应比算法.docx

《高响应比算法.docx》由会员分享,可在线阅读,更多相关《高响应比算法.docx(9页珍藏版)》请在冰点文库上搜索。

高响应比算法.docx

#include

#include

#definebuffersize5//5个缓冲区

intprocessnum=0;//初始化产品数量

structpcb//定义进程控制块PCB

{

intflag;

intnumlabel;

charproduct;

charstate;

structpcb*processlink;

}*exe=NULL,*over=NULL;

typedefstructpcbPCB;

PCB*readyhead=NULL,*readytail=NULL;

PCB*consumerhead=NULL,*consumertail=NULL;

PCB*producerhead=NULL,*producertail=NULL;

intproductnum=0;//产品数量

intfull=0,empty=buffersize;//信号量

charbuffer[buffersize];//缓冲区

intbufferpoint=0;//缓冲区指针

voidlinklist(PCB*p,PCB*listhead){//创建就绪队列

PCB*cursor=listhead;

while(cursor->processlink!

=NULL){

cursor=cursor->processlink;

}

cursor->processlink=p;

}

voidfreelink(PCB*linkhead)

{

PCB*p;

while(linkhead!

=NULL){

p=linkhead;

linkhead=linkhead->processlink;

free(p);

}

}

voidlinkqueue(PCB*process,PCB**tail)//初始化队列

{

if((*tail)!

=NULL)

{

(*tail)->processlink=process;

(*tail)=process;

}

else{printf("队列序列化!

");}

}

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;

}

elsereturnNULL;

returnp;

}

boolprocessproc()//初始化进程

{

inti,f,num;

charch;

PCB*p=NULL;

PCB**p1=NULL;

printf("\n请输入希望产生的进程个数:

");

scanf("%d",&num);

getchar();

for(i=0;i

{

printf("\n请输入您要产生的进程:

输入1为生产者进程;输入2为消费者进程\n");

scanf("%d",&f);

getchar();

p=(PCB*)malloc(sizeof(PCB));

if(!

p){printf("内存分配失败");returnfalse;}

p->flag=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("您要该进程产生的字符是%c\n",p->product);

}

else{

printf("您要生产的进程是消费者,它是第%d个进程。

\n",p->numlabel);}

linkqueue(p,&readytail);

}

returntrue;

}

boolhasElement(PCB*pro)//判断队列中是否有进程存在

{

if(pro->processlink==NULL)returnfalse;

elsereturntrue;

}

boolwaitempty()//判断生产者等待队列是否为空

{

if(empty<=0)

{

printf("进程%d:

缓存区存数,缓存区满,该进程进入生产者等待序列\n",exe->numlabel);

linkqueue(exe,&producertail);

returnfalse;

}

else{empty--;returntrue;}

}

voidsignalempty()//唤醒生产者进程

{

PCB*p;

if(hasElement(producerhead))

{

p=getq(producerhead,&producertail);

linkqueue(p,&readytail);

printf("等待中的生产者进程进入就绪队列,它的进程号是%d\n",p->numlabel);

}

empty++;

}

boolwaitfull()//判断消费者等待进程队列是否为满

{

if(full<=0)

{

printf("进程%d:

缓存区取数,缓存区空,该进程进入消费者等待队列\n",exe->numlabel);

linkqueue(exe,&consumertail);

returnfalse;

}

else{full--;returntrue;}

}

voidsignalfull()//唤醒消费者进程

{

PCB*p;

if(hasElement(consumerhead)){

p=getq(consumerhead,&consumertail);

linkqueue(p,&readytail);

printf("等待中的消费者进程进入就绪队列,它的进程号是%d\n",p->numlabel);

}

full++;

}

voidproducerrun()//生产者进程

{

if(!

waitempty())

return;

printf("进程%d开始向缓冲区存数%c\n",exe->numlabel,exe->product);

buffer[bufferpoint]=exe->product;

bufferpoint++;

printf("进程%d向缓冲区存数操作结束\n",exe->numlabel);

signalfull();

linklist(exe,over);

}

voidcomsuerrun()//消费者进程

{

if(!

waitfull())

return;

printf("进程%d向缓冲区取数\n",exe->numlabel);

exe->product=buffer[bufferpoint-1];

bufferpoint--;

printf("进程%d向缓冲区取数操作结束,取数是%c\n",exe->numlabel,exe->product);

signalempty();

linklist(exe,over);

}

voiddisplay(PCB*p)//显示进程

{

p=p->processlink;

while(p!

=NULL){

printf("进程%d,它是一个",p->numlabel);

p->flag==1?

printf("生产者\n"):

printf("消费者\n");

p=p->processlink;

}

}

voidmain()

{

charterminate;

boolelement;

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();

elsecomsuerrun();

element=hasElement(readyhead);

}

printf("就绪队列没有进程\n");

if(hasElement(consumerhead))

{

printf("消费者等待队列中有进程:

\n");

display(consumerhead);

}

else{printf("消费者等待队列中没有进程\n");}

if(hasElement(producerhead))

{

printf("生产者等待队列中有进程:

\n");

display(producerhead);

}

else{

printf("生产者等待队列中没有进程\n");

}

printf("你想继续吗?

(press'Y'foron)");

scanf("%c",&terminate);

getchar();

}

printf("\n\n进程模拟完成.\n");

freelink(over);//释放空间

over=NULL;

freelink(readyhead);

readyhead=NULL;

readytail=NULL;

freelink(consumerhead);

consumerhead=NULL;

consumertail=NULL;

freelink(producerhead);

producerhead=NULL;

producertail=NULL;

getchar();

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 商务科技

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2