CPU调度算法的模拟实现课程设计.docx
- 文档编号:12912672
- 上传时间:2023-04-22
- 格式:DOCX
- 页数:18
- 大小:19.61KB
CPU调度算法的模拟实现课程设计.docx
《CPU调度算法的模拟实现课程设计.docx》由会员分享,可在线阅读,更多相关《CPU调度算法的模拟实现课程设计.docx(18页珍藏版)》请在冰豆网上搜索。
CPU调度算法的模拟实现课程设计
设计1CPU调度算法的模拟实现
一、设计目的
1、深入理解CPU调度的四种算法:
先到先服务算法FCFS、非抢占最短作业优先算法SJF、可抢占优先权调度算法PRIOR、时间片轮转算法RR。
2、利用程序设计语言编写算法,模拟实现先到先服务算法FCFS、非抢占最短作业优先算法SJF、可抢占优先权调度算法PRIOR、时间片轮转算法RR。
3、针对模拟进程,利用CPU调度算法进行调度。
4、进行算法评价,计算平均周转时间和平均等待时间。
二、设计要求
1、调度所需的进程参数由输入产生:
采用手工输入参数或产生随机数的方法。
2、针对各个进程模拟算法,输出进程调度结果,包括执行算法的等待时间和周转时间。
3、根据调度结果对算法进行调试分析,纠正程序错误并对算法进行优化,总结并输出算法评价指标。
三、设计说明(含系统框图和(或)主要算法的流程图)
(1)优先权调度
#include"stdio.h"
#include"stdlib.h"
#defineN10
//#defineM10
typedefstruct
{
intm;
//intflag;
intpriority;//优先级顺序
floatzhixing;
floatzhouzhuan;
floatdq_zhouzhuan;
}process;
//intflag=0;//标志已经执行个进程的个数
//floattime=0;//当前共执行的时间
voidmain()
{
//intplag=0;
floattime=0;
intn;
inti,j,temp;
processfcfs[N],*p[N],*q;
floattime_zhouzhuan=0,wtime_zhouzhuan=0;
//输入
printf("请输入进程的个数:
");
scanf("%d",&n);
for(i=0;i { fcfs[i].m=i+1; //fcfs[i].linshi=0; //fcfs[i].flag=0; printf("请输入进程%d所需的执行时间和进程的优先级: ",fcfs[i].m); scanf("%f%d",&fcfs[i].zhixing,&fcfs[i].priority); } printf("haha"); for(i=0;i { p[i]=(process*)malloc(sizeof(process));//分配空间 p[i]=&fcfs[i]; } //按优先级别的排序 for(j=0;j { printf("ok");//有问题 temp=j; for(i=j+1;i { printf("ky"); if(p[i]->priority>p[temp]->priority) temp=i; } printf("#####"); q=p[j];//交换 p[j]=p[temp]; p[temp]=q; } printf("he"); //优先级算法 for(i=0;i { time+=p[i]->zhixing; p[i]->zhouzhuan=time; p[i]->dq_zhouzhuan=p[i]->zhouzhuan/p[i]->zhixing; } for(i=0;i { time_zhouzhuan+=p[i]->zhouzhuan; wtime_zhouzhuan+=p[i]->dq_zhouzhuan; } time_zhouzhuan/=n; wtime_zhouzhuan/=n; //输出结果 printf("静态优先级算法的进程的执行顺序为: \n"); for(i=0;i { printf("%d",p[i]->m); } printf("\n平均周转时间为%f",time_zhouzhuan); printf("\n带权平均周转时间为%f",wtime_zhouzhuan); } (2)FCFS和SJF综合 #include"stdio.h" #definegetjcb(type)(type*)malloc(sizeof(type)) #defineNULL0 intn=0,time=0;floateti,ewi; structjcb{charname[10];/*作业名*/ charstate;/*作业状态*/ intts;/*提交时间*/ floatsuper;/*优先权*/ inttb;/*开始运行时间*/ inttc;/*完成时间*/ floatti;/*周转时间*/ floatwi;/*带权周转时间*/ intntime;/*作业所需运行时间*/ charresource[10];/*所需资源*/ structjcb*link;/*结构体指针*/ }*p,*q,*head=NULL; typedefstructjcbJCB; inital(){ inti; printf("\nInputjcbnum\n"); scanf("%d",&n); printf("Input\nname\tts\tntime\tresource\n"); for(i=0;i p=getjcb(JCB); scanf("%s\t%d\t%d\t%s",&p->name,&p->ts,&p->ntime,&p->resource); p->state='W'; p->link=NULL; if(head==NULL)head=q=p; else{q->link=p;q=p;}}} fileinput(){ FILE*fp;inti; if((fp=fopen("os2.txt","r"))==NULL)printf("openerror! "); fscanf(fp,"%d\n",&n); for(i=0;i p=getjcb(JCB); scanf(fp,"%s%d%d%s",&p->name,&p->ts,&p->ntime,&p->resource); p->state='W'; p->link=NULL; if(head==NULL)head=q=p; else{q->link=p;q=p;}} fclose(fp);} voidprint(JCB*pr,intm){ JCB*p; printf("\ntime=%d",time); if(m==3){ printf("\nname\tstate\tts\tntime\tsuper\tsource\ttb\ttc\tti\twi\n"); printf("%s\t%c\t%d\t%d\t%4.2f\t%s\t%d\t%d\t%4.2f\t%4.2f\n", pr->name,pr->state,pr->ts,pr->ntime,pr->super,pr->resource,pr->tb,pr->tc,pr->ti,pr->wi);} else{ printf("\nname\tstate\tts\tntime\tsource\ttb\ttc\tti\twi\n"); printf("%s\t%c\t%d\t%d\t%s\t%d\t%d\t%4.2f\t%4.2f\n", pr->name,pr->state,pr->ts,pr->ntime,pr->resource,pr->tb,pr->tc,pr->ti,pr->wi);} p=head; do{if(p->state=='W') if(m==3){printf("%s\t%c\t%d\t%d\t%4.2f\t%s\n", p->name,p->state,p->ts,p->ntime,p->super,p->resource);} else{printf("%s\t%c\t%d\t%d\t%s\n", p->name,p->state,p->ts,p->ntime,p->resource);} p=p->link; }while(p! =NULL); p=head; do{if(p->state=='F') if(m==3){ printf("%s\t%c\t%d\t%d\t%4.2f\t%s\t%d\t%d\t%4.2f\t%4.2f\n", p->name,p->state,p->ts,p->ntime,p->super,p->resource,p->tb,p->tc,p->ti,p->wi);} else{printf("%s\t%c\t%d\t%d\t%s\t%d\t%d\t%4.2f\t%4.2f\n", p->name,p->state,p->ts,p->ntime,p->resource,p->tb,p->tc,p->ti,p->wi);} p=p->link; }while(p! =NULL);} voidlast(){eti/=n;ewi/=n; printf("\neti=%7.3f\tewi=%7.3f\n",eti,ewi);} super(){ JCB*padv; padv=head; do{if(padv->state=='W'&&padv->ts<=time) padv->super=(float)(time-padv->ts+padv->ntime)/padv->ntime; padv=padv->link; }while(padv! =NULL);} voidhrn(m){ JCB*min; inti,iden; for(i=0;i p=min=head;iden=1; super(); do{if(p->state=='W'&&p->ts<=time) if(iden){min=p;iden=0;} elseif(p->super>min->super)min=p; p=p->link; }while(p! =NULL); if(iden){ i--;time++;printf("\ntime=%d: \tnoJCBsubmib...wait...",time); if(time>1000){printf("\nruntimeistoolong...error...");getch();}} else{running(min,m);}}} voidsjf(intm){ JCB*min; inti,iden; for(i=0;i p=min=head;iden=1; do{if(p->state=='W'&&p->ts<=time) if(iden){min=p;iden=0;} elseif(p->ntime p=p->link; }while(p! =NULL); if(iden){i--;printf("\ntime=%d: \tnoJCBsubmib...wait...",time);time++; if(time>100){printf("\nruntimeistoolong...error");getch();}} else{running(min,m);}}} fcfs(intm){ inti,iden; printf("\n\nthejcbisruning..."); for(i=0;i p=head;iden=1; do{if(p->state=='W'&&p->ts<=time)iden=0; if(iden)p=p->link; }while(p! =NULL&&iden); if(iden){ i--;printf("\ntime=%d: \tnoJCBsubmib...wait...",time);time++; if(time>100){printf("\nruntimeistoolong...error");getch();}} else{running(p,m);}}} running(JCB*p,intm){ p->tb=time;p->state='R'; p->tc=p->tb+p->ntime; p->ti=(float)(p->tc-p->ts); p->wi=(float)(p->ti/p->ntime); eti+=p->ti; ewi+=p->wi; print(p,m); time+=p->ntime; p->state='F'; printf("\n%shasbeenfinished! \npressanykeytocontinue...\n",p->name); getch();} voidrunjcb(intm){ printf("\n\nstartrunningjcb..."); switch(m){case1: fcfs(m);break; case2: sjf(m);break; case3: hrn(m);break; default: printf("\nrunjcberror...\n");exit();}} fileway(){ printf("\nadoptsthefilewaytoinputthedatas...y/n: "); if(getch()=='y')fileinput(); elseinital();} start(){intm; charstr[100]="\nselectalgorithm\n1.FCFS\n2.SJF\n3.HRN\n"; printf("%s",str); m=getch()-48; fileway(); if(1<=m&&m<=3)runjcb(m); else{printf("\nselecterror! tryagain...\n"); start();}last();} voidmain(){ start(); printf("\nfinished! "); getch();} (3)RR算法 #include #include #include #include usingnamespacestd; #definegetch(type)(type*)malloc(sizeof(type)) #definenull0 #defineMAXN1000 intstack[MAXN]; inttop; structpcb{//调用进程模块 charname[10];//进程名 charstate;//进程状态: 就绪,运行,完成 intsuper;//进程优先级 intntime;//需要运行的时间 intrtime;//到达时间change structpcb*link;//链接指针 }*ready=null,*p; typedefstructpcbPCB; voiddestroy(); voidcheck(); voidsort()//建立对进程优先级排列函数 { PCB*first,*second; intinsert=0; if((ready==null)||((p->rtime)<(ready->rtime))){//优先级最大者,插入队首 p->link=ready; ready=p; } else{//进程比较优先级。 插入适当的位置中 first=ready; second=first->link; while(second! =null){ if((p->rtime)<(second->rtime)){//comeingtime //插入当前进程前面 p->link=second; first->link=p; second=null; insert=1; } else{ first=first->link; second=second->link; } } if(insert==0)first->link=p; } return; } PCB*q; PCB*front,*rear; voidRR() { inttime=0; q=ready; front=ready; rear=ready; while(rear->link! =null)rear=rear->link; while(front){ printf("RunningTime: %d\n",++time); if(front->rtime<=time){ front->state='R'; front->ntime--;//needtimeminus1 printf("\n***当前正在运行的进程: %s\n",front->name); if(front->ntime==0){//theonewhichisfinished p=front; if(front->link! =null) front=front->link; else{printf("\nfinished\n");break;} p->link=null; destroy() } else{//elseunfinished,connectlast rear->link=front; p=front; if(front->link! =null) front=front->link; else{printf("\nfinished\n");break;} p->link=null; } check();//displayallpcbsinqueue } } return; } voidinput() { inti,num; system("cls"); //clrscr(); printf("\n请输入进程数量"); scanf("%d",&num); for(i=0;i printf("进程号No.%d",i); p=getch(PCB); printf("\n输入进程名: "); scanf("%s",p->name); printf("\n到达时间: "); scanf("%d",&p->rtime); printf("\n输入进程运行时间: "); scanf("%d",&p->ntime); printf("\n"); p->super=0;p->state='W'; p->link=null; sort();//调用函数 } //check(); return; } intspace() { intl=0; PCB*pr=ready; while(pr! =null){ l++; pr=pr->link; } returnl; } voiddisp(PCB*pr) { printf("\nqname\tstate\tsuper\tntime\trtime\n"); printf("|%s\t",pr->name); printf("|%c\t",pr->state); printf("|%d\t",pr->super); printf("|%d\t",pr->ntime); printf("|%d\t",pr->rtime); printf("\n"); return; } voidcheck()//建立进程查看函数 { //PCB*pr; /**//*printf("\n***当前正在运行的进程: %s",p->name);显示当前运行进程 disp(p);*/ p=front; printf("\n****当前就绪队列状态: %c",p->state);/**//*显示队列状态*/ while(p! =null){ disp(p); p=p->link; } return; } voiddestroy()/**//*建立进程撤销函数(进程运行结束,撤销进程)*/ { printf("\n进程[%s]已完成。 \n",p->name); free(p); return; } /**//*voidrunning()//进程就绪函数 { (p->rtime)++; if(p->rtime==p->ntime)// destroy(); else{ (p->super)--; p->state='W'; sort(); } return; } intmain() { freopen("in.txt","r",stdin); intlen,h=0; //charch; input(); len=space(); RR(); /**//*while((len! =0)&&(ready! =null)){ ch=getchar(); h++; printf("\nTheexecutenumber: %d\n",h); p=ready; ready=p->link; p->link=null;//把最前的拿出来 p->state='R'; check(); running(); //system("pause"); printf("\n按任意键继续."); ch=getchar(); }*/ printf("\n\n进程已经完成。 \n"); system("pause"); return0; } 四、运行结果及分析 分析: FCFS算法,即先来先服务示,就是每次从就绪队列中选择一个最先进入队列的进程,把CPU分配给它,令它远行。 该进程一直运行下去,直到完成,或者由于某种原因而阻塞,对才放弃CPU。 FC
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CPU 调度 算法 模拟 实现 课程设计
![提示](https://static.bdocx.com/images/bang_tan.gif)