CPU调度算法的模拟实现课程设计文档格式.docx
《CPU调度算法的模拟实现课程设计文档格式.docx》由会员分享,可在线阅读,更多相关《CPU调度算法的模拟实现课程设计文档格式.docx(18页珍藏版)》请在冰点文库上搜索。
![CPU调度算法的模拟实现课程设计文档格式.docx](https://file1.bingdoc.com/fileroot1/2023-5/1/274c9db7-966e-4f85-88b9-378683bf45f2/274c9db7-966e-4f85-88b9-378683bf45f21.gif)
fcfs[i].m=i+1;
//fcfs[i].linshi=0;
//fcfs[i].flag=0;
请输入进程%d所需的执行时间和进程的优先级:
fcfs[i].m);
%f%d"
fcfs[i].zhixing,&
fcfs[i].priority);
}
haha"
p[i]=(process*)malloc(sizeof(process));
//分配空间
p[i]=&
fcfs[i];
//按优先级别的排序
for(j=0;
j<
n-1;
j++)
ok"
//有问题
temp=j;
for(i=j+1;
ky"
if(p[i]->
priority>
p[temp]->
priority)
temp=i;
#####"
q=p[j];
//交换
p[j]=p[temp];
p[temp]=q;
he"
//优先级算法
time+=p[i]->
zhixing;
p[i]->
zhouzhuan=time;
dq_zhouzhuan=p[i]->
zhouzhuan/p[i]->
time_zhouzhuan+=p[i]->
zhouzhuan;
wtime_zhouzhuan+=p[i]->
dq_zhouzhuan;
time_zhouzhuan/=n;
wtime_zhouzhuan/=n;
//输出结果
静态优先级算法的进程的执行顺序为:
\n"
%d"
p[i]->
m);
\n平均周转时间为%f"
time_zhouzhuan);
\n带权平均周转时间为%f"
wtime_zhouzhuan);
}
(2)FCFS和SJF综合
#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;
\nInputjcbnum\n"
Input\nname\tts\tntime\tresource\n"
i++){
p=getjcb(JCB);
scanf("
%s\t%d\t%d\t%s"
p->
name,&
ts,&
ntime,&
resource);
p->
state='
W'
;
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"
scanf(fp,"
%s%d%d%s"
q=p;
}}
fclose(fp);
voidprint(JCB*pr,intm){
JCB*p;
printf("
\ntime=%d"
time);
if(m==3){
\nname\tstate\tts\tntime\tsuper\tsource\ttb\ttc\tti\twi\n"
%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{
\nname\tstate\tts\tntime\tsource\ttb\ttc\tti\twi\n"
%s\t%c\t%d\t%d\t%s\t%d\t%d\t%4.2f\t%4.2f\n"
pr->
p=head;
do{if(p->
state=='
)
if(m==3){printf("
%s\t%c\t%d\t%d\t%4.2f\t%s\n"
name,p->
state,p->
ts,p->
ntime,p->
super,p->
else{printf("
%s\t%c\t%d\t%d\t%s\n"
p=p->
link;
}while(p!
=NULL);
do{if(p->
F'
resource,p->
tb,p->
tc,p->
ti,p->
voidlast(){eti/=n;
ewi/=n;
\neti=%7.3f\tewi=%7.3f\n"
eti,ewi);
super(){
JCB*padv;
padv=head;
do{if(padv->
&
padv->
ts<
=time)
padv->
super=(float)(time-padv->
ts+padv->
ntime)/padv->
ntime;
padv=padv->
}while(padv!
voidhrn(m){
JCB*min;
inti,iden;
p=min=head;
iden=1;
super();
if(iden){min=p;
iden=0;
elseif(p->
super>
min->
super)min=p;
if(iden){
i--;
time++;
\ntime=%d:
\tnoJCBsubmib...wait..."
if(time>
1000){printf("
\nruntimeistoolong...error..."
getch();
else{running(min,m);
}}}
voidsjf(intm){
JCB*min;
inti,iden;
for(i=0;
ntime<
ntime)min=p;
=NULL);
if(iden){i--;
100){printf("
\nruntimeistoolong...error"
fcfs(intm){
\n\nthejcbisruning..."
=time)iden=0;
if(iden)p=p->
=NULL&
iden);
else{running(p,m);
running(JCB*p,intm){
tb=time;
R'
tc=p->
tb+p->
ti=(float)(p->
tc-p->
ts);
wi=(float)(p->
ti/p->
ntime);
eti+=p->
ti;
ewi+=p->
wi;
print(p,m);
time+=p->
\n%shasbeenfinished!
\npressanykeytocontinue...\n"
p->
name);
getch();
voidrunjcb(intm){
\n\nstartrunningjcb..."
switch(m){case1:
fcfs(m);
break;
case2:
sjf(m);
case3:
hrn(m);
default:
\nrunjcberror...\n"
exit();
fileway(){
\nadoptsthefilewaytoinputthedatas...y/n:
if(getch()=='
y'
)fileinput();
elseinital();
start(){intm;
charstr[100]="
\nselectalgorithm\n1.FCFS\n2.SJF\n3.HRN\n"
;
%s"
str);
m=getch()-48;
fileway();
if(1<
=m&
m<
=3)runjcb(m);
else{printf("
\nselecterror!
tryagain...\n"
start();
}last();
voidmain(){
start();
\nfinished!
(3)RR算法
#include<
stdio.h>
stdlib.h>
conio.h>
iostream>
usingnamespacestd;
#definegetch(type)(type*)malloc(sizeof(type))
#definenull0
#defineMAXN1000
intstack[MAXN];
inttop;
structpcb{//调用进程模块
charname[10];
//进程名
//进程状态:
就绪,运行,完成
intsuper;
//进程优先级
//需要运行的时间
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->
(second->
rtime)){//comeingtime
//插入当前进程前面
link=second;
first->
link=p;
second=null;
insert=1;
}
else{
first=first->
second=second->
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->
while(front){
RunningTime:
%d\n"
++time);
if(front->
rtime<
=time){
front->
state='
front->
ntime--;
//needtimeminus1
\n***当前正在运行的进程:
%s\n"
front->
if(front->
ntime==0){//theonewhichisfinished
p=front;
link!
=null)
front=front->
\nfinished\n"
p->
link=null;
destroy()
else{//elseunfinished,connectlast
rear->
link=front;
p=front;
check();
//displayallpcbsinqueue
voidinput()
inti,num;
system("
cls"
//clrscr();
\n请输入进程数量"
num);
for(i=0;
i<
num;
i++){
进程号No.%d"
i);
p=getch(PCB);
\n输入进程名:
\n到达时间:
rtime);
\n输入进程运行时间:
super=0;
state='
sort();
//调用函数
//check();
return;
intspace()
intl=0;
PCB*pr=ready;
while(pr!
l++;
pr=pr->
returnl;
voiddisp(PCB*pr)
\nqname\tstate\tsuper\tntime\trtime\n"
|%s\t"
pr->
|%c\t"
state);
|%d\t"
super);
return;
voidcheck()//建立进程查看函数
//PCB*pr;
/**//*printf("
显示当前运行进程
disp(p);
*/
\n****当前就绪队列状态:
%c"
/**//*显示队列状态*/
while(p!
voiddestroy()/**//*建立进程撤销函数(进程运行结束,撤销进程)*/
\n进程[%s]已完成。
free(p);
/**//*voidrunning()//进程就绪函数
(p->
rtime)++;
if(p->
rtime==p->
ntime)//
destroy();
super)--;
state='
intmain()
freopen("
in.txt"
stdin);
intlen,h=0;
//charch;
input();
len=space();
RR();
/**//*while((len!
=0)&
(ready!
=null)){
ch=getchar();
h++;
\nTheexecutenumber:
h);
p=ready;
ready=p->
link=null;
//把最前的拿出来
state='
running();
//system("
pause"
\n按任意键继续."
}*/
\n\n进程已经完成。
system("
return0;
}
四、运行结果及分析
分析:
FCFS算法,即先来先服务示,就是每次从就绪队列中选择一个最先进入队列的进程,把CPU分配给它,令它远行。
该进程一直运行下去,直到完成,或者由于某种原因而阻塞,对才放弃CPU。
FC