资源描述
计算机工程学院实验报告
课程名称: 网络操作系统
班级:
实验成绩:
指导教师:
姓名:
实验项目名称: 短作业优先调度算法
学号
上机实践日期:
实验项目编号: 2
组号:
上机实践时间: 学时
一、 目的(本次实验所涉及并要求掌握的知识点)
1实现短作业优先算法。
2了解作业控制块的作用,以及作业控制块的内容和组织方式。
二、实验内容与设计思想(设计思路、主要数据结构、主要代码结构、主要代码段分析、电路图)
实验内容:
1为每个进入系统的作业建立档案以记录和作业相关的信息,例如作业名(以自己的姓名拼音缩写编号,例如,张三同学编写程序所用的作业名是zs1,zs2…..),作业服务时间,作业进入系统的时间,作业开始执行时间,作业完成时间,作业周转时间,平均周转时间,作业信息在存储器中的位置,指向下一个作业控制块的指针等信息。
2将系统中等待作业调度的JCB组织成一个队列(后备队列)。当进行作业调度时,从后备队列中查找选择一个执行时间最短的作业将它调入内存运行。
3进行作业调度时,计算出每个作业的开始执行时间,完成时间,周转时间和平均周转时间,利用短作业优先算法进行作业调度,并按照完成时间由小到大的顺序显示出来。
三、实验使用环境(本次实验所使用的平台和相关软件)
Linux虚拟机 VC++
四、实验步骤和调试过程(实验步骤、测试数据设计、测试结果分析)
实验代码:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct task{
char name[4];
float servicetime; //作业服务时间
float gosystemtime; //作业进入系统的时间
float starttime; //作业开始执行时间
float finishtime; //作业完成时间
float arounttime; //作业周转时间
struct task* next; //指向下一个作业控制块的指针
};
int r; //作业数
struct task *head,*p1,*p2;
struct task* createtask();
void taskschedule(struct task * s);
void main(){
struct task *t;
t=createtask();
taskschedule(t);
}
struct task* createtask(){ //创建作业链
int n=0;
p1=p2=(struct task *)malloc(sizeof(struct task)); /*开辟一个新单元*/
head=NULL;
printf("Please input the nuber of task:\n");
scanf("%d",&r); //输入作业数
for(int i=0;i<r;i++){
printf(" Please input the name of task %d :\n",i+1 );
scanf("%s", p1->name); //输入作业名
printf(" Please input the service time :\n");
scanf("%f",&p1-> servicetime); //输入作业服务时间
printf(" Please input the time of task into system :\n");
scanf("%f",&p1->gosystemtime); //输入作业进入系统时间
n=n+1; /*结点个数加1*/
if(n==1) head=p1; /*是第一个结点, 置头指针*/
else p2->next=p1; /*非首结点, 将新结点插入到链表尾*/
p2=p1; /*设置新的尾结点*/
p1=(struct task *)malloc(sizeof(struct task));
}
p2->next=NULL;
return(head);
}
void taskschedule(struct task * s){ //作业调度
float servicetim; //用于保存最小服务时间
float gosystemtim; //用于保存最小进入系统时间
float endtime=0; //用语标记作业开始的时间点
int flage; //标记时间点内是否有任务
float avaragetime=0;
struct task * head,* percur,* pres,*l,*t;
head=s;
struct task *p;
p=head;
float e=p->gosystemtime; //用于暂时保存最小进入系统时间
for(;p->next!=NULL;) //找出最小进入系统时间
{
if(p->gosystemtime<=e)
e=p->gosystemtime;
p=p->next;
}
endtime=e;
printf("===============================================================================\n");
printf(" 作业名 运行时间 进入系统时间 开始时间 完成时间 周转时间\n");
int i=0;
for(i;i<r;i++)
{ t=l=head;
pres=percur=l;
servicetim=100000000;
flage=0;
int j=i;
int k=i;
for(j;j<r;j++)
{
if(l->gosystemtime<=endtime)
{ flage=1; //标记为该时间点内有作业任务
if(l->servicetime<servicetim)
{
servicetim=l->servicetime;
pres=l;
percur=t;
}
}
if(l!=head) //l是t的next
t=t->next;
l=l->next;
}
if(flage==0) //该时间点没有作业任务
{ t=l=head;
pres=percur=l;
gosystemtim=l->gosystemtime;
servicetim=l->servicetime;
for(k;k<r-1;k++) //找出最早进入系统的作业,若进入时间相同,则选则服务时间最短的
{ l=l->next;
if(l->gosystemtime<gosystemtim||(l->gosystemtime==gosystemtim&&l->servicetime<servicetim))
{
gosystemtim=l->gosystemtime;
servicetim=l->servicetime;
pres=l;
percur=t;
}
t=t->next;
}
endtime=gosystemtim;
}
pres->starttime=endtime;
pres->finishtime= pres->starttime+pres->servicetime;
pres->arounttime=pres->finishtime-pres->gosystemtime;
avaragetime=avaragetime+pres->arounttime;
printf(" %s %f %f %f %f %f\n",pres->name,pres->servicetime, pres->gosystemtime, pres->starttime, pres->finishtime, pres->arounttime);
endtime=endtime+pres->servicetime;
if(pres==head)
{
head=pres->next;
}
if(pres->next==NULL)
{
percur->next=NULL;
}
else
{
percur->next=pres->next;
}
}
avaragetime=avaragetime/r;
printf("The avaragetime of aroundtime: %f \n", avaragetime);
}
五、实验小结(实验中遇到的问题及解决过程、实验中产生的错误及原因分析、实验体会和收获)
这个实验涉及的算法比较复杂,觉得最头痛的是作业链表中的指针的运用,要很小心,一个小的差错就会全盘错。
在实验的开始,就困恼算法,静下来大概整理下整体思路,将根据特定时间点内是否有任务在等待,将调度分成两大部分:一,有任务等待,则选出服务时间最短的任务执行;二;没有任务,则选出最早到的任务,若多个任务一起到,则选出服务时间最短的作业执行。如此,就需要2个循环,外循环循环执行一个任务,内循环则,找出短作业优先调度算法下的作业。
在实验的过程中出了许多问题如:循环次数没摸准,输入的作业,有些跳过没有出现在结果中;开始时间很混乱,没有达到实际的结果;当某时间点没有作业的时候,程序就停止了。 通过分析和调试程序,逐渐找出问题,最后到达能完成短作业优先调度算法的程序。
其中发现,主要是指针的问题,指针对于程序来说有至关重要的关系,指针让你程序变的灵活,然而指针也是很难处理的,若指针没有运用好,则是麻烦大,程序检查起来就恨吃力,而且很困难,有时你根本不知道,指针是不是真的完成了你想要的效果,是否就是指向了你想要的变量,是否指针没有指向任何变量。
六、附录(参考文献和相关资料)
Linux系统与网络服务管理
展开阅读全文