作业调度先来先服务实验分析.docx
《作业调度先来先服务实验分析.docx》由会员分享,可在线阅读,更多相关《作业调度先来先服务实验分析.docx(33页珍藏版)》请在冰豆网上搜索。
![作业调度先来先服务实验分析.docx](https://file1.bdocx.com/fileroot1/2023-1/5/4efe1672-ed83-4a4d-bcb4-e360147ee7fd/4efe1672-ed83-4a4d-bcb4-e360147ee7fd1.gif)
作业调度先来先服务实验分析
实验二作业调度实验
一.目的要求:
用高级语言编写和调试一个或多个作业调度的模拟程序,以加深对作业调度算法的理解。
二.例题:
为单道批处理系统设计一个作业调度程序。
由于在单道批处理系统中,作业一投入运行,它就占有计算机的一切资源直到作业完成为止,因此调度作业时不必考虑它所需要的资源是否得到满足,它所占用的CPU时限等因素。
作业调度算法:
采用先来先服务(FCFS)调度算法,即按作业提交的先后次序进行调度。
总是首先调度在系统中等待时间最长的作业。
每个作业由一个作业控制块JCB表示,JCB可以包含如下信息:
作业名、提交时间、所需的运行时间、所需的资源、作业状态、链指针等等。
作业的状态可以是等待W(Wait)、运行R(Run)和完成F(Finish)三种状态之一。
每个作业的最初状态总是等待W。
各个等待的作业按照提交时刻的先后次序排队,总是首先调度等待队列中队首的作业。
每个作业完成后要打印该作业的开始运行时刻、完成时刻、周转时间和带权周转时间,这一组作业完成后要计算并打印这组作业的平均周转时间、带权平均周转时间。
调度算法的流程图如下图所示。
三.实习题:
1、编写并调试一个单道处理系统的作业等待模拟程序。
作业等待算法:
分别采用先来先服务(FCFS),最短作业优先(SJF)、响应比高者优先(HRN)的调度算法。
对每种调度算法都要求打印每个作业开始运行时刻、完成时刻、周转时间、带权周转时间,以及这组作业的平均周转时间及带权平均周转时间,以比较各种算法的优缺点。
实验代码:
#include
#include
#include
#definegetpch(type)(type*)malloc(sizeof(type))
#defineNULL0
intn;
floatT1=0,T2=0;
inttimes=0;
structjcb//作业控制块
{
charname[10];//作业名
intreachtime;//作业到达时间
intstarttime;//作业开始时间
intneedtime;//作业需要运行的时间
floatsuper;//作业的响应比
intfinishtime;//作业完成时间
floatcycletime;//作业周转时间
floatcltime;//作业带权周转时间
charstate;//作业状态
structjcb*next;//结构体指针
}*ready=NULL,*p,*q;
typedefstructjcbJCB;
voidinize()//初始化界面
{
printf("\n\n\t\t*********************************************\t\t\n");
printf("\t\t\t\t单道批处理作业调度系统\n");
printf("\t\t*********************************************\t\t\n");
printf("\n\n\n\t\t\t\t软件143赵媛媛\n\n");
printf("\t\t\t\t\t\n");
printf("\n\n\n\t\t请输入任意键进入……\n");
getch();
}
voidinital()//建立作业控制块队列,先将其排成先来先服务的模式队列
{
inti;
printf("\n输入作业数:
");
scanf("%d",&n);
for(i=0;i{
p=getpch(JCB);
printf("\n输入作业名:
");
scanf("%s",p->name);
getch();
p->reachtime=i;
printf("作业默认到达时间:
%d",i);
printf("\n输入作业要运行的时间:
");
scanf("%d",&p->needtime);
p->state='W';
p->next=NULL;
if(ready==NULL)ready=q=p;
else{
q->next=p;
q=p;
}
}
}
voiddisp(JCB*q,intm)//显示作业运行后的周转时间及带权周转时间等
{
if(m==3)//显示高响应比算法调度作业后的运行情况
{
printf("\n作业%s正在运行,估计其运行情况:
\n",q->name);
printf("开始运行时刻:
%d\n",q->starttime);
printf("完成时刻:
%d\n",q->finishtime);
printf("周转时间:
%f\n",q->cycletime);
printf("带权周转时间:
%f\n",q->cltime);
printf("相应比:
%f\n",q->super);
getch();
}
else//显示先来先服务,最短作业优先算法调度后作业的运行情况
{
printf("\n作业%s正在运行,估计其运行情况:
\n",q->name);
printf("开始运行时刻:
%d\n",q->starttime);
printf("完成时刻:
%d\n",q->finishtime);
printf("周转时间:
%f\n",q->cycletime);
printf("带权周转时间:
%f\n",q->cltime);
getch();
}
}
voidrunning(JCB*p,intm)//运行作业
{
if(p==ready)//先将要运行的作业从队列中分离出来
{
ready=p->next;
p->next=NULL;
}
else
{
q=ready;
while(q->next!
=p)q=q->next;
q->next=p->next;
}
p->starttime=times;//计算作业运行后的完成时间,周转时间等等
p->state='R';
p->finishtime=p->starttime+p->needtime;
p->cycletime=(float)(p->finishtime-p->reachtime);
p->cltime=(float)(p->cycletime/p->needtime);
T1+=p->cycletime;
T2+=p->cltime;
disp(p,m);//调用disp()函数,显示作业运行情况
times+=p->needtime;
p->state='F';
printf("\n%shasbeenfinished!
\npressanykeytocontinue...\n",p->name);
free(p);//释放运行后的作业
getch();
}
voidsuper()//计算队列中作业的高响应比
{
JCB*padv;
padv=ready;
do{
if(padv->state=='W'&&padv->reachtime<=times)
padv->super=(float)(times-padv->reachtime+padv->needtime)/padv->needtime;
padv=padv->next;
}while(padv!
=NULL);
}
voidfinal()//最后打印作业的平均周转时间,平均带权周转时间
{
floats,t;
t=T1/n;
s=T2/n;
getch();
printf("\n\n作业已经全部完成!
");
printf("\n%d个作业的平均周转时间是:
%f",n,t);
printf("\n%d个作业的平均带权周转时间是%f:
\n\n\n",n,s);
}
voidhrn(intm)//高响应比算法
{
JCB*min;
inti,iden;
system("cls");
inital();
for(i=0;i{
p=min=ready;iden=1;
super();
do{
if(p->state=='W'&&p->reachtime<=times)
if(iden)
{
min=p;iden=0;
}
elseif(p->super>min->super)min=p;
p=p->next;
}while(p!
=NULL);
if(iden)
{
i--;times++;
//printf("\ntime=%d:
\tnoJCBsubmib...wait...",time);
if(times>1000)
{printf("\nruntimeistoolong...error...");getch();}
}
else
{
running(min,m);//调用running()函数
}
}//for
final();//调用running()函数
}
voidsjf(intm)//最短作业优先算法
{
JCB*min;
inti,iden;
system("cls");
inital();
for(i=0;i{
p=min=ready;iden=1;
do{
if(p->state=='W'&&p->reachtime<=times)
if(iden){
min=p;iden=0;
}
elseif(p->needtimeneedtime)min=p;
p=p->next;
}while(p!
=NULL);
if(iden){
i--;//printf("\ntime=%d:
\tnoJCBsubmib...wait...",time);
times++;
if(times>100){printf("\nruntimeistoolong...error");getch();}
}
else{
running(min,m);//调用running()函数
}
}//for
final();//调用running()函数
}
voidfcfs(intm)//先来先服务算法
{
inti,iden;
system("cls");
inital();
for(i=0;i{
p=ready;iden=1;
do{
if(p->state=='W'&&p->reachtime<=times)iden=0;
if(iden)p=p->next;
}while(p!
=NULL&&iden);
if(iden)
{
i--;
printf("\n没有满足要求的进程,需等待");
times++;
if(times>100){printf("\n时间过长");getch();}
}
else{
running(p,m);//调用running()函数
}
}
final();//调用running()函数
}
voidmune()
{
intm;
system("cls");
printf("\n\n\t\t*********************************************\t\t\n");
printf("\t\t\t\t作业调度演示\n");
printf("\t\t*********************************************\t\t\n");
printf("\n\n\n\t\t\t1.先来先服务算法.");
printf("\n\t\t\t2.最短作业优先算法.");
printf("\n\t\t\t3.响应比高者优先算法");
printf("\n\t\t\t0.退出程序.");
printf("\n\n\t\t\t\t选择所要操作:
");
scanf("%d",&m);
switch(m)
{case1:
fcfs(m);
getch();
system("cls");
mune();
break;
case2:
sjf(m);
getch();
system("cls");
mune();
break;
case3:
hrn(m);
getch();
system("cls");
mune();
break;
case0:
system("cls");
break;
default:
printf("选择错误,重新选择.");
getch();
system("cls");
mune();
}
}
main()//主函数
{
inize();
mune();
}
实验截图
2、编写并调度一个多道程序系统的作业调度模拟程序。
作业调度算法:
采用基于先来先服务的调度算法。
可以参考课本中的方法进行设计。
对于多道程序系统,要假定系统中具有的各种资源及数量、调度作业时必须考虑到每个作业的资源要求。
实验代码:
#include
#include
#definegetjch(type)(type*)malloc(sizeof(type))
#defineN10
structjcb{/*定义作业控制块PCB*/
charname[10];
floatneedtime;/*运行时间*/
floatarrivetime;/*提交时刻*/
floatstorage[N];/*系统资源*/
structjcb*link;
}*ready=NULL,*pb=NULL,*p;
typedefstructjcbJCB;
floatTc,Ti,Wi,T=0;/*完成时刻,周转时间,带权周转时间,时间量*/
floatTiSum=0,WiSum=0;/*平均周转时间,带权a平均周转时间*/
floatsource[N];
intn;
voidinput();/*输入作业信息*/
intspace();/*返回就绪队列中作业的数目*/
voidfcfs();/*先来先服务算法*/
voiddisp(JCB*pr);/*显示相应的作业*/
voidrunning();/*运行作业组*/
voiddestroy();/*撤销作业*/
voidinput()/*建立作业控制块函数*/
{
inti,k,num;
printf("请输入所拥有的资源种类:
");
scanf("%d",&n);
printf("输入系统所拥有资源数:
\n");
for(i=0;i{
printf("资源[%d]:
",i);
scanf("%f",&source[i]);
}
printf("\n输入作业数量:
");
scanf("%d",&num);
for(i=0;i{
printf("\n作业号[%d]:
\n",i);
p=getjch(JCB);
printf("输入作业名:
");
scanf("%s",p->name);
printf("输入提交时间:
");
scanf("%f",&p->arrivetime);
printf("输入运行时间:
");
scanf("%f",&p->needtime);
printf("输入所需资源数:
\n");
for(k=0;k{
printf("资源[%d]:
",i);
scanf("%f",&p->storage[k]);
}
printf("\n");
p->link=NULL;
fcfs();
}
}
intspace()
{
intl=0;
JCB*pr=ready;
while(pr!
=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
voiddisp(JCB*pr)/*建立作业显示函数,用于显示当前作业*/
{
inti;
printf("\n%6s\t%6s\t%6s\t","作业名","运行时间","提交时刻");
for(i=0;iprintf("资源[%d]\t",i);
printf("\n%6s\t%6.2f\t\t%6.2f\t",pr->name,pr->needtime,pr->arrivetime);
for(i=0;iprintf("\t%6.2f",pr->storage[i]);
printf("\n");
}
voiddestroy()/*建立作业撤消函数(作业运行结束,撤消作业)*/
{
free(p);
}
voidcheck()
{
JCB*first,*fir,*p;
intflag=0,i,test=0;
first=pb;
while(first&&(T>=first->arrivetime)&&(flag==0))
{
for(i=0;i{
if(source[i]>=first->storage[i])
source[i]=source[i]-first->storage[i];
else
test=1;
}
if(test==0)
{
p=first;
first=first->link;
p->link=NULL;
if(ready==NULL)
ready=p;
else
{
fir=ready;
while(fir->link!
=NULL)
{
fir=fir->link;
}
fir->link=p;
}
}
else
flag=1;
}
pb=first;
}
voidfcfs()
{
JCB*first,*second;
intins=0;
if((pb==NULL)||(p->arrivetimearrivetime))
{
p->link=pb;
pb=p;
}
else
{
first=pb;
second=first->link;
while(second!
=NULL)
{
if(p->arrivetimearrivetime)
{
p->link=second;
second=NULL;
first->link=p;
ins=1;
}
else
{
first=first->link;
second=second->link;
}
}
if(ins==0)
first->link=p;
}
}
voidrunning()
{
JCB*pr;
inti;
printf("正在运行的作业是:
%s\n",p->name);
disp(p);
if(ready!
=NULL)
{
printf("就绪队列如下:
\n");
pr=ready;
while(pr!
=NULL)
{
disp(pr);
pr=pr->link;
}
}
else
printf("就绪队列为空队列!
\n");
if(pb!
=NULL)
{
printf("后备队列如下:
\n");
pr=pb;
while(pr!
=NULL)
{
disp(pr);
pr=pr->link;
}
}
else
printf("后备队列为空队列!
\n");
printf("作业%s的开始运行时刻T:
%4.2f\n",p->name,T);
Tc=T+p->needtime;
T=Tc;
Ti=Tc-p->arrivetime;
Wi=Ti/(p->needtime);
for(i=0;isource[i]=source[i]+p->storage[i];
printf("完成时刻Tc:
%4.2f\n",Tc);
printf("周转时间Ti:
%4.2f\n",Ti);
printf("带权周转时间Wi:
%4.2f\n",Wi);
TiSum+=Ti;
WiSum+=Wi;
destroy();
}
main()//主函数
{
intlen;
charch;
input();
T=pb->arrivetime;
check();
len=space();
while((len!
=0)&&(ready!
=NULL))
{
system("pause");
p=ready;
ready=p->link;
p->link=NULL;
running();
if(pb!
=NULL)
{
if(ready==NULL)
{
if(Tarrivetime)
T=pb->arrivetime;
}
check();
len=space();
}
}
printf("\n该作业组的平均周转时间:
%4.2f\n",TiSum/len);
printf("该作业组的带权平均周转时间:
%4.2f\n",WiSum/len);
ch=getchar();
}
实验截图:
3、编写并调试一个多道程序系统的作业调度模拟程序。
作业调度算法:
采用基于优先级的作业调度。
可以参考课本中的例子自行设计。
实验代码:
#include
#include
#definegetjch(type)(type*)malloc(sizeof(type))
#defineN10
structjcb
{
/*定义作业控制块PCB*/