实验五 作业调度算法模拟
一、 实验目的
1、用高级语言编写和调试单道环境下的作业调度的模拟程序,以加深对作业调度的理解。单道环境的特点是被调度的作业占有所有资源。
2、在完成了单道环境的作业调度后,有余力的同学可以完成多道环境下的作业调度,多道的特点是:内存中可以同时存在一道以上的进程,所有进程共享系统资源,这样作业调度过程中还要考虑资源分配情况。
3、通过两种环境下作业调度的模拟,比较两种环境下作业调度的异同,从而达到理解作业调度的功能。
二、实验要求
1、复习作业调度的各种算法的思路; 2、认真完成实验内容,并对代码进行测试;
3、完成实验报告,并于17周周三之前由学习委员按学号整理好提交给任课教师; 4、实验报告要求: (1)实验目的
(2)程序所用的数据结构及其说明 (3)打印一份源程序且附上注释。 (4)对测试结果进行截图。
三、实验内容
为单道批处理系统设计一个作业调度程序,由于在单道批处理系统中,作业一投入运行,它就占有计算机的一切资源直到作业完成为止,因此调度作业时不必考虑它所需要的资源是否得到满足,它所占用的CPU时限等因素。作业调度算法:(1)先来先服务调度算法(FCFS);(2)短作业优先算法;(3)最高响应比调度算法;(4)最高优先级调度算法。
输入:每个作业包括数据项,作业调度所需要的数据项:作业号、提交时间、运行时间等。
输出:要求打印每个作业完成后该作业的开始运行时间、完成时间、周转时间和平均周转时间。 选做:
实现多道环境下,并结合资源分配情况,相应的几个作业调度算法,比较两者之间的异同。 四、提示信息
假定第一道作业提交之后就开始调度,另外作业所有的资源都已经获得。
(1)FCFS先来先服务算法:先提交的作业先调度;
(2)SJF短作业优先:当有多道作业在输入井时,运行时间短的作业先调度。
(3)HRN最高响应比优先:在作业调度之前,计算每道作业的响应比,响应比高的作业优先调度。
(4)几个重要的计算公式:
a:周转时间=运行时间+等待时间;或者周转时间=完成时间-提交时间 b:带权周转时间=周转时间/运行时间;或者 带权周转时间=1+等待时间/运行时间;
c:响应比的计算公式:响应比=1+等待时间/运行时间;
单道环境下的参考程序1: #include \#include \#define SIZE 4 struct job_type { int no;//作业号 int tb;//到达时间 int tr;//运行时间 }x;
struct job_type job[]={1,2,20,2,35,50,3,0,40,4,30,13};
void load() { int i; printf(\输入作业顺序:\\n\ printf(\ 作业号 到达时间 运行时间\\n\ }
for(i=0;i void sjf() { int i,j,time=0,k=0; int t=0;//总时间 int tw=0;//等待时间 //float tavg=(job[i].tr+tw)/SIZE; for(i=0;i if(job[i].tr>job[j].tr) { x=job[i]; job[i]=job[j]; job[j]=x; } for(i=k;i { if(job[i].tb>time) { } x=job[i]; job[i]=job[j]; job[j]=x; } time=time+job[i].tr; } printf(\短作业优先调度结果:\\n\ printf(\开始时间 作业号 到达时间 运行时间 总时间 等待时间 平均周转时for(i=0;i printf(\ t=t+job[i].tr; tavg=(t-job[i].tb)/SIZE;//平均周转时间(只保留整数部分) } tw=t-job[i].tb-job[i].tr; printf(\ 间\\n\ int tavg; } void main() { } load(); sjf(); 参考程序2:(有问题) #include\#include\#include\void FCFS(); void SJF(); void HRN(); void input(); void operation(); int n; double avturn; double avdqzz; double t=0.0,k=0.0; struct HomeWork { int num; double subtime; double runtime; double begintime; double endtime; double turntime; double dqzztime; double zgxy; }HW[10]; void input() { cout<<\请输入要执行的作业的数量\ cin>>n; for(int i=1;i<=n;i++) { cout<<\请输入第\个作业的数据:\ HW[i].num=i; cout<<\提交时间:\ cin>>HW[i].subtime; cout<<\执行时间:\ cin>>HW[i].runtime; } } void operation() { for(int i=1;i<=n;i++) { if(i==1) HW[i].begintime=HW[i].subtime; else HW[i].begintime=HW[i-1].endtime; HW[i].endtime=HW[i].begintime+HW[i].runtime; HW[i].turntime=HW[i].endtime-HW[i].subtime; HW[i].dqzztime=HW[i].turntime/HW[i].runtime; } for(i=1;i<=n;i++) { t=t+HW[i].turntime; k=k+HW[i].dqzztime; } avturn=t/n; avdqzz=k/n; cout<<\作业序号 提交时间 执行时间 开始时间 完成时间 周转时间 带权周转时间\for(i=1;i<=n;i++) { cout<<\ \\\t\HW[i].subtime<<\\\t\HW[i].runtime<<\\\t\HW[i].begintime<<\ \\t\ HW[i].endtime<<\ \\t\ HW[i].dqzztime< cout<<\平均周转时间:\cout<<\平均带权周转时间:\} } void FCFS() { Cout<<\先来先服务(FCFS)**********\\n\operatine(); t=0.0; k=0.0; } void SJF() { cout<<\短作业优先(SJF)***********\\n \int no; double tjtime; double yxtime; for(int i=2;i<=n;i++) for(int j=i+1;j<=n;j++) { if(HW[i].runtime>HW[j].runtime) { no=HW[i].num; HW[i].num=HW[j].num; HW[j].num=no; tjtime=HW[i].subtime; HW[i].subtime=HW[j].subtime; HW[j].subtime=tjtime; yxtime=HW[i].runtime; HW[i].runtime=HW[j].runtime; HW[j].runtime=yxtime; } HW[i].turntime<<\ \\t\ } operation(); t=0.0; k=0.0; } void HRN() { cout<<\最高响应比优先**********\\n \ double tjtime,yxtime; int no ; for(int i=2;i<=n;i++) { if(HW[i].subtime HW[i].zgxy=(HW[i-1].endtime-HW[i].subtime)/HW[i].runtime; } for(int j=i+1;j<=n;j++) { if(HW[j].subtime HW[j].zgxy=(HW[j].begintime-HW[j].subtime)/HW[j].runtime; if(HW[i].zgxy no=HW[i].num; HW[i].num=HW[j].num; HW[j].num=no; tjtime=HW[i].subtime; HW[i].subtime=HW[j].subtime; HW[j].subtime=tjtime; yxtime=HW[i].runtime; HW[i].runtime=HW[j].runtime; HW[j].runtime=yxtime; } } operation(); t=0.0; k=0.0; } void main() { input(); FCFS(); SJF(); HRN(): } 多道环境下的参考程序: #include char *UserID; char *JobID; char status; //状态 double RunTime; //运行时间 int ApplyMem; //需要内存 int MemoryNum; int ApplyRecorder; //需要磁带 struct JCB *next; }JCB; JCB jt; int memory=MEMLENGTH; int recorder=RECORDERNUM; int num=5; JCB *JTfind(JCB *jcb,char status) { JCB *p; if(jcb==NULL) return NULL; for(p=jcb->next;p!=NULL&&p->status!=status;p=p->next); return p; }//查找jcp指针之后status值与形参status相等的指针并返回其地址 void JTinit(JCB *p) { } void JTinsert(JCB *jt,JCB *p) { JCB *q; for(q=jt;q->next!=NULL;q=q->next); q->next=p; p->next=(JCB *)malloc(sizeof(JCB)); p->next->next=NULL; p->UserID=p->JobID=NULL; p->next=NULL; } int JTdel(JCB *jt,JCB *ep) { } JCB *p; if(ep==NULL) return 0; p=jt->next; if(p==NULL) return 0; for(;p->next!=NULL&&p->next!=ep;p=p->next); if(p->next==NULL) return 0; p->next=ep->next; return 1; void JTrelease(JCB * jt) { JCB * p; JCB * q; p=jt->next; if(p==NULL) return; for(q=p->next;q!=NULL;p=q,q=q->next) free(p); } void large()//将作业调入内存(就绪->执行) { JCB *ep,*pd; //int i; ep=JTfind(&jt,'s'); if(ep!=NULL) { pd=ep; //选择运行时间最短的作业 while((pd=JTfind(pd,'s'))!=NULL) if(ep->RunTime>pd->RunTime) ep=pd; if(memory>=ep->ApplyMem) { if(recorder>=ep->ApplyRecorder) { ep->status='e'; //执行状态 JTdel(&jt,ep); //删除作业ep JTinsert(&jt,ep); //将ep插入到链表jt尾部 memory-=ep->ApplyMem;//memory-=ep->ApplyRecorder; } } recorder-=ep->ApplyRecorder; printf(\ printf(\} else printf(\ else printf(\ } else printf(\ JCB *JTfindUserID(JCB *jcb,char *UserID) { JCB *p; if(jcb==NULL) return NULL; p=jcb->next->next; while(strcmp(p->UserID,UserID)!=0&&p!=NULL) p=p->next; return p; } void small()//内存不足,换出最先进入内存的作业(等待->完成)回收资源 { JCB *ep,*p; char *UID=(char*)malloc(10*sizeof(char)); //ep=JTfind(&jt,'e');/*先来先服务*/ printf(\内存中有如下作业,UserID为:\for(p=jt.next->next;p!=NULL;p=p->next) if(p->status=='e') printf(\printf(\ printf(\请输入需要回收资源的UserID:\scanf(\ ep=JTfindUserID(&jt,UID); if(ep==NULL) { printf(\主存中无作业!\\n\ return; } else if(ep->status=='s') printf(\该作业处于收容状态,未调入内存\\n\else if(ep->status=='e') //释放资源 { printf(\ printf(\num--; memory+=ep->ApplyMem; recorder+=ep->ApplyRecorder; JTdel(&jt,ep); } } void init() { JCB *p; JTinit(&jt); printf(\请输入5道作业信息,每个数据之间用空格隔开。例如:A(用户名) ZYA(作业号) s(作业状态) 0.3(作业运行时间) 15(申请内存大小) 2(申请打印机数量)\\n\ for(int i=0;i<5;i++) { p=(JCB*)malloc(sizeof(JCB)); p->UserID=(char*)malloc(10*sizeof(char)); p->JobID=(char*)malloc(10*sizeof(char)); scanf(\ em,&p->ApplyRecorder); JTinsert(&jt,p); } } int main() { int operation; } init(); for(;num!=0;) { } printf(\scanf(\ if((operation>9)||(operation<0))continue; if(operation>5) large(); //将作业调入内存 最短作业优先法 else small(); //将作业从内存释放 JTrelease(&jt); printf(\return 0; JTdel(&jt,ep); } } void init() { JCB *p; JTinit(&jt); printf(\请输入5道作业信息,每个数据之间用空格隔开。例如:A(用户名) ZYA(作业号) s(作业状态) 0.3(作业运行时间) 15(申请内存大小) 2(申请打印机数量)\\n\ for(int i=0;i<5;i++) { p=(JCB*)malloc(sizeof(JCB)); p->UserID=(char*)malloc(10*sizeof(char)); p->JobID=(char*)malloc(10*sizeof(char)); scanf(\ em,&p->ApplyRecorder); JTinsert(&jt,p); } } int main() { int operation; } init(); for(;num!=0;) { } printf(\scanf(\ if((operation>9)||(operation<0))continue; if(operation>5) large(); //将作业调入内存 最短作业优先法 else small(); //将作业从内存释放 JTrelease(&jt); printf(\return 0; 百度搜索“70edu”或“70教育网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,70教育网,提供经典综合文库作业调度算法在线全文阅读。
相关推荐: