进程调度.docx
- 文档编号:12034219
- 上传时间:2023-04-16
- 格式:DOCX
- 页数:16
- 大小:16.07KB
进程调度.docx
《进程调度.docx》由会员分享,可在线阅读,更多相关《进程调度.docx(16页珍藏版)》请在冰豆网上搜索。
进程调度
#include"stdio.h"
#include"stdlib.h"
#definetrue1
#definefalse0
/*ґґЅЁPCB*/
typedefstructPCB
{
intname;
intpre;
intneedTime;
intreachTime;
intrunTime;
structPCB*next;
}PCB,*Top1;
/*Х»іхКј»Ї*/
intinitStack(Top1*t)
{
*t=NULL;
returntrue;
}
/*МнјУФЄЛШ*/
intpush(Top1*t,intname,intpre,intneedTime,intreachTime,intrunTime)
{
Top1p=(Top1)malloc(sizeof(PCB));/*їЄДЪґж*/
if(p==NULL)
returnfalse;
p->name=name;
p->pre=pre;
p->needTime=needTime;
p->reachTime=reachTime;
p->runTime=runTime;
p->next=*t;
*t=p;
returntrue;
}
/*ЙиЦГёчІОБї*/
voidsetname(Top1*t,intName)
{
(*t)->name=Name;
}
voidsetpre(Top1*t,intPre)
{
(*t)->pre=Pre;
}
voidsetneedTime(Top1*t,intNeedTime)
{
(*t)->needTime=NeedTime;
}
voidsetreachTime(Top1*t,intReachTime)
{
(*t)->reachTime=ReachTime;
}
voidsetrunTime(Top1*t,intRunTime)
{
(*t)->runTime=RunTime;
}
/*ЙѕіэХ»¶ҐФЄЛШ*/
intpop(Top1*t)
{
Top1p=*t;
*t=p->next;
free(p);/*КН·ЕЛщХјДЪґж*/
returntrue;
}
/*ИЎµГёчІОБї*/
intgetname(Top1*t)
{
return(*t)->name;
}
intgetpre(Top1*t)
{
return(*t)->pre;
}
intgetneedTime(Top1*t)
{
return(*t)->needTime;
}
intgetreachTime(Top1*t)
{
return(*t)->reachTime;
}
intgetrunTime(Top1*t)
{
return(*t)->runTime;
}
/*ПИАґПИ·юОс*/
voidFCFS(Top1*Top,intnum)
{
inti,j;
intName,Pre,NeedTime,ReachTime,RunTime;
Top1pcb;
initStack(&pcb);
/*Ѕ«Х»ёцФЄЛШ·ЧЄ*/
for(i=0;i {/*ИҐХ»¶ҐФЄЛШ*/ Name=getname(Top); Pre=getpre(Top); NeedTime=getneedTime(Top); ReachTime=getreachTime(Top); RunTime=getrunTime(Top); push(&pcb,Name,Pre,NeedTime,ReachTime,RunTime); pop(Top); } printf("namepreneedTimereachTimerunTime\n"); for(i=0;i { /*ИЎµГХ»¶ҐФЄЛШµДёчІОБї*/ Name=getname(&pcb); Pre=getpre(&pcb); NeedTime=getneedTime(&pcb); ReachTime=getreachTime(&pcb); RunTime=getrunTime(&pcb); for(j=0;j { if(NeedTime<0||NeedTime==0) { printf("process%disover.\n",Name); break; } else { printf("%d%d%d%d%d\n", Name,Pre,NeedTime,ReachTime,RunTime++); } NeedTime--; } pop(&pcb); } } /*¶ОЧчТµУЕПИ*/ voidSJF(Top1*Top,intnum) { inti,j,temp,sum=0; intName,Pre,NeedTime,ReachTime,RunTime; intName1,Pre1,NeedTime1,ReachTime1,RunTime1; Top1pcb; Top1pcb1; Top1pcb2; initStack(&pcb); initStack(&pcb1); /*·ЧЄ·ЕИлБнТ»Х»ЦР*/ for(i=0;i { Name=getname(Top); Pre=getneedTime(Top); NeedTime=getneedTime(Top); ReachTime=getreachTime(Top); RunTime=getrunTime(Top); sum+=getneedTime(Top); push(&pcb,Name,Pre,NeedTime,ReachTime,RunTime); pop(Top); } printf("namepreneedTimereachTimerunTime\n"); for(i=0;i { /*ИЎФЄЛШЈ¬К№µГХ»¶ҐФЄЛШУЕПИј¶ЧоРЎ*/ if(i==0) { Name=getname(&pcb); Pre=getpre(&pcb); NeedTime=getneedTime(&pcb); ReachTime=getreachTime(&pcb); RunTime=getrunTime(&pcb); push(&pcb1,Name,Pre,NeedTime,ReachTime,RunTime); pop(&pcb); } elseif(i { temp=getneedTime(&pcb1); Name=getname(&pcb); Pre=getpre(&pcb); NeedTime=getneedTime(&pcb); ReachTime=getreachTime(&pcb); RunTime=getrunTime(&pcb); if(NeedTime>temp) { Name1=getname(&pcb1); Pre1=getpre(&pcb1); NeedTime1=getneedTime(&pcb1); ReachTime1=getreachTime(&pcb1); RunTime1=getrunTime(&pcb1); pop(&pcb1); push(&pcb1,Name,Pre,NeedTime,ReachTime,RunTime); push(&pcb1,Name1,Pre1,NeedTime1,ReachTime1,RunTime1); } elsepush(&pcb1,Name,Pre,NeedTime,ReachTime,RunTime); pop(&pcb); } Name=getname(&pcb1); Pre=getpre(&pcb1)-1; NeedTime=getneedTime(&pcb1)-1; ReachTime=getreachTime(&pcb1); RunTime=getrunTime(&pcb1)+1; if(NeedTime<0||NeedTime==0) { printf("%d%d%d%d%d\n", Name,Pre,NeedTime,ReachTime,RunTime); printf("process%disover.\n",Name); pop(&pcb1); } else { printf("%d%d%d%d%d\n", Name,Pre,NeedTime,ReachTime,RunTime); setneedTime(&pcb1,NeedTime); setrunTime(&pcb1,RunTime); setpre(&pcb1,Pre); } } } /*К±јдЖ¬ВЦЧЄ*/ voidRR(Top1*Top,intnum) { inti,j,temp,sum=0; intName,Pre,NeedTime,ReachTime,RunTime; intName1,Pre1,NeedTime1,ReachTime1,RunTime1; Top1pcb; Top1pcb1; Top1pcb2; initStack(&pcb); initStack(&pcb1); for(i=0;i { Name=getname(Top); Pre=getneedTime(Top); NeedTime=getneedTime(Top); ReachTime=getreachTime(Top); RunTime=getrunTime(Top); sum+=getneedTime(Top); push(&pcb,Name,Pre,NeedTime,ReachTime,RunTime); pop(Top); } printf("namepreneedTimereachTimerunTime\n"); temp=num; for(i=0;i { for(j=0;j { Name=getname(&pcb); Pre=getpre(&pcb); NeedTime=getneedTime(&pcb); ReachTime=getreachTime(&pcb); RunTime=getrunTime(&pcb); push(&pcb1,Name,Pre,NeedTime,ReachTime,RunTime); pop(&pcb); Name=getname(&pcb1); Pre=getpre(&pcb1)-1; NeedTime=getneedTime(&pcb1)-1; ReachTime=getreachTime(&pcb1); RunTime=getrunTime(&pcb1)+1; if(NeedTime<0||NeedTime==0) { printf("%d%d%d%d\n", Name,NeedTime,ReachTime,RunTime); printf("process%disover.\n",Name); pop(&pcb1); num--; } else { printf("%d%d%d%d\n", Name,NeedTime,ReachTime,RunTime); setneedTime(&pcb1,NeedTime); setrunTime(&pcb1,RunTime); } i++; } temp=num; for(i=0;i { Name=getname(&pcb1); Pre=getpre(&pcb1); NeedTime=getneedTime(&pcb1); ReachTime=getreachTime(&pcb1); RunTime=getrunTime(&pcb1); push(&pcb,Name,Pre,NeedTime,ReachTime,RunTime); pop(&pcb1); } } } /*ёЯПмУ¦±ИУЕПИ*/ voidHRRN(Top1*Top,intnum) { inti,j,temp,sum=0; intName,Pre,NeedTime,ReachTime,RunTime; intName1,Pre1,NeedTime1,ReachTime1,RunTime1; Top1pcb; Top1pcb1; Top1pcb2; initStack(&pcb); initStack(&pcb1); for(i=0;i { Name=getname(Top); Pre=0;NeedTime=getneedTime(Top); ReachTime=getreachTime(Top); RunTime=0; sum+=getneedTime(Top); push(&pcb,Name,Pre,NeedTime,ReachTime,RunTime); pop(Top); } printf("namepreneedTimereachTimerunTime\n"); for(i=0;i { if(i==0) { Name=getname(&pcb); NeedTime=getneedTime(&pcb); ReachTime=getreachTime(&pcb); RunTime=i; Pre=(NeedTime+RunTime-ReachTime)/NeedTime; push(&pcb1,Name,Pre,NeedTime,ReachTime,RunTime); pop(&pcb); } elseif(i { Name=getname(&pcb); NeedTime=getneedTime(&pcb); ReachTime=getreachTime(&pcb); RunTime=i; Pre=(NeedTime+RunTime-ReachTime)/NeedTime; temp=(getneedTime(&pcb1)+RunTime-getreachTime(&pcb1))/(getneedTime(&pcb1)); if(Pre { Name1=getname(&pcb1); Pre1=temp; NeedTime1=getneedTime(&pcb1); ReachTime1=getreachTime(&pcb1); RunTime=i; pop(&pcb1); push(&pcb1,Name,Pre,NeedTime,ReachTime,RunTime); push(&pcb1,Name1,Pre1,NeedTime1,ReachTime1,RunTime); } else { push(&pcb1,Name,Pre,NeedTime,ReachTime,RunTime); } pop(&pcb); } Name=getname(&pcb1); NeedTime=getneedTime(&pcb1)-1; ReachTime=getreachTime(&pcb1); RunTime=i; /*ЈїЈїЈїґЛґ¦Pre=(NeedTime+RunTime-ReachTime)/NeedTime;РРІ»НЁЈїЈїЈїЈї*/ if(NeedTime<0||NeedTime==0) { printf("%d%d%d%d%d\n", Name,Pre,NeedTime,ReachTime,RunTime); printf("process%disover.\n",Name); pop(&pcb1); } else { printf("%d%d%d%d%d\n", Name,Pre,NeedTime,ReachTime,RunTime); setneedTime(&pcb1,NeedTime); setrunTime(&pcb1,RunTime); setpre(&pcb1,Pre); } } } voidmain() { loop: /*С»·*/ { charch; intnum; intneedTime; inti; Top1dig; initStack(&dig); num=rand()%10+1; printf("TheNo.ofprocessis%d.\n",num); for(i=0;i { needTime=rand()%4+1; printf("process: %dneedTime: %dreachTime: %d\n",i,needTime,i); push(&dig,i,i,needTime,i,0); } printf("----------------------------------\n"); printf("1---FCFS;2---SJF;3---HRRN;4---RR\n"); printf("----------------------------------\n"); printf("intputtheNO.: "); i=0; scanf("%d",&i); switch(i) { case1: FCFS(&dig,num);break; case2: SJF(&dig,num);break; case3: HRRN(&dig,num);break; case4: RR(&dig,num);break; default: printf("inputwrongNo.! ! ! "); } printf("again? (yorn)\ninput: "); loop_1: { ch=getch(); if(ch=='y')/*yФЩґОКдИл*/ gotoloop; elseif(ch=='n')/*nНЛіц*/ exit(0); else/*¶јІ»КЗМбКѕКдИлґнОу*/ { printf("\ninputwrong! againplease! ! ! \ninput: "); gotoloop_1; } } } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 进程 调度