实验报告材料一进程调度算法.docx
《实验报告材料一进程调度算法.docx》由会员分享,可在线阅读,更多相关《实验报告材料一进程调度算法.docx(15页珍藏版)》请在冰豆网上搜索。
![实验报告材料一进程调度算法.docx](https://file1.bdocx.com/fileroot1/2023-3/29/8bc37393-bc4f-4fed-b995-bdc3fd2b9ed4/8bc37393-bc4f-4fed-b995-bdc3fd2b9ed41.gif)
实验报告材料一进程调度算法
江西师范大学计算机信息工程学院学生实验报告
专业12级物联网班姓名严超学号1208067042日期2014/05/8
课程名称
操作系统教程
实验室名称
W4313
实验名称
进程调度算法
指导教师
张练兴
成绩
1、实验目的
用代码实现模拟操作系统的进程调度,以加深对进程的概念及进程调度算法的理解
2、实验原理和内容
(1)先来先服务(FCFS)调度算法:
从“就绪队列”中选择一个最先进入队列的进程,为它分配处理器,使之开始运行。
(2)优先数调度算法:
根据进程的情况或要求赋予进程一个优先级,进程运行过程中优先级不再改变。
每次调度时,就绪队列中优先级最高的进程被率先调度,同级的采用先来先服务(FCFS)。
3、实验步骤
进程调度算法:
(1)编写进程控制块数据结构
(2)统一按照FCFS调度算法创建队列
(3)在FCFS调度算法中,将就绪队列队首进程调入执行,如果在队列中存在到达时间小于等于当前时间的结点,将该结点的状态设为就绪状态。
如果当前进程执行完了,就将其状态改为完成状态,并将其插入到队尾。
(4)在优先级调度算法中,将就绪队列队首进程调入执行,如果在队列中存在到达时间小于等于当前时间的结点,将该结点的状态设为就绪状态,并对队列中的结点按优先级数的大小进行排序(队首除外)。
如果当前进程执行完了,就将其状态改为完成状态,并将其插入到队尾。
(5)输出运行后的结果,如周转时间和带权周转时间。
4、程序及运行结果(或实验数据记录及分析)
进程调度算法:
本次实验让我更加明白进程调度的概念,更加了解进程调度的工作原理。
在前期,我是直接将结果显示出来,后来,我又在原有的基础上加了显示每一时刻队列的信息。
在编写此代码过程中遇到很多问题,例如指针问题,指针指来指去,总是指错地址。
具体代码:
#include
#include
#defineMAX1000
typedefstructprogress{
intID;//进程名
charstate;//进程状态
intsuper;//优先数
intarrive_time;//到达时间
intserve_time;//服务时间
structprogress*next;
}node;
node*sortFCFS(node*head,node*q)
{
node*p,*pre;
intdone=0;
if((head==NULL)||((q->arrive_time)<(head->arrive_time)))/*到达时间最先者,插入队首*/
{
q->next=head;
head=q;
}
else/*进程比较到达时间,插入适当的位置中*/
{
p=head;
pre=p->next;
while(pre!
=NULL)
{
if((q->arrive_time)<(pre->arrive_time))/*若插入进程比当前进程到达时间小,*/
{/*插入到当前进程前面*/
q->next=pre;
p->next=q;
pre=NULL;
done=1;
}
else
{
p=p->next;
pre=pre->next;
}
}
if(done==0)p->next=q;
}
returnhead;
}
/*
函数功能:
创建单链表
参数:
空
返回值:
指向节点的指针head
*/
node*create()
{
node*head;
node*p,*q;
intx,count=0;
head=NULL;
printf("\n\t\t请输入进程名【输入-1结束】:
");
while(scanf("%d",&x)!
=EOF&&x!
=-1)
{
p=(node*)malloc(sizeof(node));
printf("\t\t请输入优先级数【优先数高者优先】:
");
scanf("%d",&p->super);
printf("\t\t请输入到达时间【到达时间不得小于0】:
");
scanf("%d",&p->arrive_time);
printf("\t\t请输入服务时间【服务时间必须大于0】:
");
scanf("%d",&p->serve_time);
p->ID=x;
p->state='w';
p->next=NULL;
head=sortFCFS(head,p);
printf("\n\t\t请输入进程名(输入-1结束):
");
}
returnhead;
}
/*
函数功能:
输出单链表
参数:
指向节点的指针head
返回值:
空
*/
voidprint(node*head)
{
node*p;
printf("\n\t|--------------------------结点信息情况--------------------------|");
printf("\n\t\t|进程名|优先级数|到达时间|服务时间|状态|");
p=head;
while(p)
{
printf("\n\t\t|%8d|%8d|%8d|%8d|%8c|",p->ID,p->super,p->arrive_time,p->serve_time,p->state);
p=p->next;
}
printf("\n\t|--------------------------结点信息情况--------------------------|\n");
}
/*
函数功能:
利用先来先服务调度算法
参数:
指向节点的指针head
返回值:
空
存在问题:
*/
voidFCFS(node*head)
{
intstart_time,finish_time=0,round_time,all_time=0;
intdone=1;
intclock=0;
floatright_round_time;
node*p,*q,*flag;
flag=p=head;
clock=p->arrive_time;
all_time=start_time=head->arrive_time;
while(p)
{
all_time+=p->serve_time;
p=p->next;
}
p=head;
while(done)
{
done=0;
printf("\n\n\t|---------------------------第%2d时刻---------------------------|",clock);
while(p)
{
if(p->arrive_time<=clock&&p->state=='w'||p->state=='r')
{
p->state='r';done=1;
}
p=p->next;
}
while(flag->next)flag=flag->next;
print(head);
printf("\t|---------------------------第%2d时刻---------------------------|\n\n",clock);
if(clock==all_time)break;
clock++;
finish_time=start_time+head->serve_time;
if(finish_time==clock)
{
head->state='f';
flag->next=head;
head=head->next;
flag=flag->next;
flag->next=NULL;
start_time=finish_time;
}
p=head;
}p=head;
finish_time=p->arrive_time;
printf("\t|--------------------------FCFS调度算法--------------------------|");
printf("\n\t|进程名|到达时间|服务时间|开始时间|完成时间|周转时间|带权周转|");
while(p)
{
if(p->arrive_time<=finish_time)
start_time=finish_time;
else
start_time=p->arrive_time;
finish_time=start_time+p->serve_time;
round_time=finish_time-p->arrive_time;
right_round_time=(float)round_time/p->serve_time;
printf("\n\t|%8d|%8d|%8d|%8d|%8d|%8d|%8.2f|",p->ID,
p->arrive_time,p->serve_time,start_time,finish_time,round_time,right_round_time);
p=p->next;
}
printf("\n\t|--------------------------FCFS调度算法--------------------------|");
printf("\n");
}
voidSortBySuper(node*head)
{
node*p1,*q1,*temp;
temp=(node*)malloc(sizeof(node));
for(p1=head->next;p1!
=NULL&&p1->state=='r';p1=p1->next)
{
for(q1=p1->next;q1!
=NULL&&q1->state=='r';q1=q1->next)
{
if(p1->supersuper)
{
temp->ID=p1->ID;
p1->ID=q1->ID;
q1->ID=temp->ID;
temp->super=p1->super;
p1->super=q1->super;
q1->super=temp->super;
temp->arrive_time=p1->arrive_time;
p1->arrive_time=q1->arrive_time;
q1->arrive_time=temp->arrive_time;
temp->serve_time=p1->serve_time;
p1->serve_time=q1->serve_time;
q1->serve_time=temp->serve_time;
temp->state=p1->state;
p1->state=q1->state;
q1->state=temp->state;
}
}
}
}
/*
函数功能:
利用优先数调度算法
参数:
指向节点的指针head
返回值:
空
存在问题:
*/
voidpriority(node*head)
{
intstart_time,finish_time=0,round_time,all_time=0;
intdone=1;
intclock=0;
floatright_round_time;
node*p,*q,*flag;
flag=p=head;
clock=p->arrive_time;
all_time=start_time=head->arrive_time;
while(p)
{
all_time+=p->serve_time;
p=p->next;
}
p=head;
while(done)
{
done=0;
printf("\n\n\t|---------------------------第%2d时刻---------------------------|",clock);
while(p)
{
if(p->arrive_time<=clock&&p->state=='w'||p->state=='r')
{
p->state='r';done=1;
}
p=p->next;
}
SortBySuper(head);while(flag->next)flag=flag->next;
print(head);
printf("\t|---------------------------第%2d时刻---------------------------|\n\n",clock);
if(clock==all_time)break;
clock++;
finish_time=start_time+head->serve_time;
if(finish_time==clock)
{
head->state='f';
flag->next=head;
head=head->next;
flag=flag->next;
flag->next=NULL;
start_time=finish_time;
}
p=head;
}p=head;
finish_time=p->arrive_time;
printf("\n\t|--------------------------优先数调度算法--------------------------|");
printf("\n\t|进程名|到达时间|服务时间|开始时间|完成时间|周转时间|带权周转|");
while(p)
{
if(p->arrive_time<=finish_time)
start_time=finish_time;
else
start_time=p->arrive_time;
finish_time=start_time+p->serve_time;
round_time=finish_time-p->arrive_time;
right_round_time=(float)round_time/p->serve_time;
printf("\n\t|%8d|%8d|%8d|%8d|%8d|%8d|%8.2f|",p->ID,
p->arrive_time,p->serve_time,start_time,finish_time,round_time,right_round_time);
p=p->next;
}
printf("\n\t|--------------------------优先数调度算法--------------------------|");
printf("\n");
}
intmain()
{
charchoice;
node*head;
do{
printf("\n\t\t|----------------MUNE---------------|\n");
printf("\t\t|----------1.FCFS调度算法-----------|\n");
printf("\t\t|----------2.优先数调度算法---------|\n");
printf("\t\t\t你的选择是:
<>\b\b");
}while(scanf("%c",&choice)!
=EOF&&choice!
='1'&&choice!
='2');
head=create();
switch(choice)
{
case'1':
print(head);
FCFS(head);break;
case'2':
print(head);
priority(head);break;
}
}
运行结果截图:
先来先服务调度算法运行结果
优先级调度算法运行结果