进程调度 实验报告.docx
《进程调度 实验报告.docx》由会员分享,可在线阅读,更多相关《进程调度 实验报告.docx(19页珍藏版)》请在冰点文库上搜索。
进程调度实验报告
实验一进程调度实验报告
一、实验目的
多道程序设计中,经常是若干个进程同时处于就绪状态,必须依照某种策略来决定那个进程优先占有处理机。
因而引起进程调度。
本实验模拟在单处理机情况下的处理机调度问题,加深对进程调度的理解。
二、实验内容
1.优先权法、轮转法
简化假设
1)进程为计算型的(无I/O)
2)进程状态:
ready、running、finish
3)进程需要的CPU时间以时间片为单位确定
2.算法描述
1)优先权法——动态优先权
当前运行进程用完时间片后,其优先权减去一个常数。
2)轮转法
三、流程图
四、实验要求
1.
产生的各种随机数的取值范围加以限制,如所需的CPU时间限制在1~20之间。
2.进程数n不要太大通常取4~8个
3.使用动态数据结构
4.独立编程
5.至少三种调度算法
6.若有可能请在图形方式下,将PCB的调度用图形成动画显示。
五实验源程序
主函数控制台Main.c
#include
#include
#include"fcfs.h"
#include"privilege.h"
#include"round.h"
voidmain()
{
intn;
cout<<"输入随机产生的进程数目"<cin>>n;
intchoice=1;
while(choice!
=5)
{
cout<<"请输入你的选择"<cout<<"1:
FCFS调度算法\n"<<"2:
动态优先权调度算法\n"<<"3:
时间片轮转法\n"<<"4:
清屏\n"<<"5:
退出"<cin>>choice;
switch(choice)
{
case1:
{structprocess*first=creatlist(n);print(first);fcfs(first);break;}
case2:
{structprocess1*first1=creatlist1(n);print1(first1);privilege(first1);break;}
case3:
{structprocess2*first=creatlist2(n);print2(first);round(first);break;}
case4:
{system("cls");break;}
case5:
break;
default:
break;
}
}
//*/
}
先来先服务调度算法:
fcfs.h
#ifndefFCFS_H
#defineFCFS_H
#include
#include
#include
#include
#include
#definerandom(x)(rand()%x)
structprocess
{
intnum;//进程号
intattime,servtime;//进程到达时间,服务时间
process*next;
};
structprocess*creatlist(int);//新建一个链表
voidinsert(structprocess*first,structprocess*s);//插入一个节点(尾插法)
voidprint(structprocess*first);//打印函数
structprocess*creatlist(intn)
{
srand((int)time(0));
structprocess*first=newprocess;
first->next=NULL;
for(inti=0;i{
structprocess*s;
s=newprocess;
s->num=i;
s->attime=i;
s->servtime=random(20);
insert(first,s);
}
returnfirst;
}
voidinsert(structprocess*first,structprocess*s)
{
structprocess*r=first;
structprocess*p;
while(r){p=r;r=r->next;}
p->next=s;p=s;
p->next=NULL;
}
voidprint(structprocess*first)//打印函数
{
structprocess*p;
p=first->next;
cout<<"随机产生的进程的信息如下"<cout<<"进程名"<<"进程到达时间"<<"进程服务时间"<while(p)
{
cout<num<<'\t'<attime<servtime<p=p->next;
}
}
voidfcfs(structprocess*first)
{
intstartime=0;//开始执行时间
intfinishtime=0;//完成时间
intwasttime=0;//周转时间
intweighttime=0;//带权周转时间
structprocess*p=first->next;
structprocess*r;
cout<<"/**************************************************/"<cout<<"FCFS调度算法基本信息\n"<cout<<"进程名"<<"到达时间"<<"服务时间"<<"开始执行时间"<<"完成时间"<<"周转时间"<<"带权周转时间"<while(p)
{
finishtime=p->servtime+startime;
wasttime=finishtime-p->attime;
weighttime=wasttime/p->servtime;
cout<num<attime<servtime<r=p;
p=p->next;
startime+=r->servtime;
}
}
#endif
动态优先权调度算法privilege.h
#ifndefPRIVILEGE_H
#definePRIVILEGE_H
structprocess1
{
intpcb;//进程号PCB
intprivilege,cpu;//进程优先权,所需CPU时间
process1*next;
};
/*******************优先权调度算法所需函数声明*****************************************************/
structprocess1*creatlist1();//新建链表(就绪队列)
voidinsert1(structprocess1*first,structprocess1*s);//增加一个进程或将一个进程查入到队列中
voiddel1(structprocess1*first,structprocess1*s);//删除(撤销)一个进程
voidprint1(structprocess1*first);//打印函数
structprocess1*search(structprocess1*head,structprocess1*s);
/*******************优先权调度算法所需函数*****************************************************/
structprocess1*creatlist1(intn)
{
srand((int)time(0));
structprocess1*first=newprocess1;
first->next=NULL;
for(inti=0;i{
structprocess1*s;
s=newprocess1;
s->pcb=i;
s->privilege=random(20)+5;
s->cpu=random(20)+1;
insert1(first,s);
}
returnfirst;
}
voidinsert1(structprocess1*first,structprocess1*s)//插入节点
{
structprocess1*p=search(first,s);
s->next=p->next;
p->next=s;
//return;
}
structprocess1*search(structprocess1*first,structprocess1*s)//查找第一个到达时间大于等于AT的节点,返回其前一个指针
{
structprocess1*p,*q;
p=first;
q=first->next;
while(q!
=NULL&&q->privilege>=s->privilege)
{
p=q;
q=q->next;
}
returnp;
}
voiddel1(structprocess1*first,structprocess1*s)
{
structprocess1*p,*r;
p=first->next;
r=first;
inta=1;
while(a)
{
if(p==s)
{
r->next=p->next;
//free(p);
a=0;
}
else
{
r=p;
p=p->next;
}
}
}
voidprint1(structprocess1*first)//打印函数
{
structprocess1*p;
p=first->next;
cout<<"进程号"<<"进程优先权"<<"进程所需CPU片数"<while(p)
{
cout<pcb<privilege<cpu<p=p->next;
}
}
voidprivilege(structprocess1*first)
{
structprocess1*p,*r=first,*t;
p=first->next;
intb=0;
while(first->next)
{
r=first;
p=first->next;
cout<<"/********************"<
cout<<"***********进程运行前****************"<print1(r);
p->cpu=p->cpu-1;
p->privilege=p->privilege-3;
if(p->privilege<0)
{
p->privilege=0;
}
cout<<"***********进程运行后**********************"<print1(r);
cout<<"/*************************************************/\n"<if(p->cpu==0)
{
del1(r,p);
}
else
{
t=p;
del1(r,p);
insert1(r,p);
}
b++;
}
}
#endif
时间片轮转调度算法round.h
#ifndefROUND_H
#defineROUND_H
structprocess2
{
intpcb;//进程号PCB
introundpiece;//轮转时间片数
intneedpiece;//进程所需时间片数
intcpupiece;//占用CPU时间片数
process2*next;
};
structprocess2*creatlist2(int);//新建一个链表
voidprint2(structprocess2*first);//打印函数
voidinsert2(structprocess2*first,structprocess2*s);//插入节点
voiddel2(structprocess2*first,structprocess2*s);//删除一个进程
structprocess2*creatlist2(intn)
{
srand((int)time(0));
structprocess2*first=newprocess2;
first->next=NULL;
for(inti=0;i{
structprocess2*s;
s=newprocess2;
s->pcb=i;
s->roundpiece=random
(2)+1;
s->needpiece=random(20);
s->cpupiece=0;
insert2(first,s);
}
returnfirst;
}
voidinsert2(structprocess2*first,structprocess2*s)
{
structprocess2*r=first;
structprocess2*p;
while(r){p=r;r=r->next;}
p->next=s;p=s;
p->next=NULL;
}
voiddel2(structprocess2*first,structprocess2*s)
{
structprocess2*p,*r;
p=first->next;
r=first;
inta=1;
while(a)
{
if(p==s)
{
r->next=p->next;
//free(p);
a=0;
}
else
{
r=p;
p=p->next;
}
}
}
voidprint2(structprocess2*first)//打印函数
{
structprocess2*p;
p=first->next;
//cout<<"随机产生的进程的信息如下"<cout<<"进程名"<<"轮转时间片数"<<"所需时间片数"<<"占用时间片数"<while(p)
{
cout<pcb<roundpiece<needpiece<cpupiece<p=p->next;
}
}
voidround(structprocess2*first)
{
structprocess2*p,*r=first;
p=first->next;
intb=0;
while(first->next)
{
//r=first;
p=first->next;
cout<<"/********************"<
cout<<"-------------进程运行前-------------"<print2(r);
p->needpiece=p->needpiece-1;
p->cpupiece=p->cpupiece+1;
cout<<"-------------进程运行后-------------"<print2(r);
cout<<"/*************************************************/\n"<if(p->needpiece<=0)
{
del2(r,p);
}
else
{
if(p->cpupiece==p->roundpiece)
{
p->cpupiece=0;
del2(r,p);
insert2(r,p);
}
}
b++;
}
}
#endif
六实验结果
输入随机产生的进程数目4
输入选择调度算法1
输入选择调度算法2
输入选择调度算法3
七实验总结
通过次实验模拟在单处理机情况下的处理机调度问题,加深了对进程调度的理解。
在次试验中,由于各个调度算法用到的数据结构不同,所以采用了不同的结构体来存放各个调度算法所用的信息。
在动态优先权调度算法中,当前进程用完时间片后,其优先权减去一个常数3,有可能出现优先权低于0的情况,在此情况下没有采取其它措施,后来经老师提醒改为低于0时,就将其优先权置为0,这样就好了。