广工操作系统实验报告.docx
《广工操作系统实验报告.docx》由会员分享,可在线阅读,更多相关《广工操作系统实验报告.docx(78页珍藏版)》请在冰点文库上搜索。
广工操作系统实验报告
《操作系统》实验报告
学院计算机学院
专业软件工程
学号
姓名
指导教师
日期2014年7月
实验一:
进程调度实验
题目1
1.实验要求:
编写并调试一个模拟的进程调度程序,采用“最高优先数优先”调度算法对五个进程进行调度。
2、实验原理
“最高优先数优先”调度算法的基本思想是把CPU分配给就绪队列中优先数最高的进程。
静态优先数是在创建进程时确定的,并在整个进程运行期间不再改变。
动态优先数是指进程的优先数在创建进程时可以给定一个初始值,并且可以按一定原则修改优先数。
例如:
在进程获得一次CPU后就将其优先数减少1。
或者,进程等待的时间超过某一时限时增加其优先数的值,等等
3、流程图
4、源代码
#include"stdio.h"
#include
#include
#definegetpch(type)(type*)malloc(sizeof(type))
//#defineNULL0
structpcb{/*定义进程控制块PCB*/
charname[10];
charstate;
intsuper;
intntime;
intrtime;
structpcb*link;
}*ready=NULL,*p;
typedefstructpcbPCB;
voidsort()/*建立对进程进行优先级排列函数*/
{
PCB*first,*second;
intinsert=0;
if((ready==NULL)||((p->super)>(ready->super)))/*优先级最大者,插入队首*/
{
p->link=ready;
ready=p;
}
else/*进程比较优先级,插入适当的位置中*/
{
first=ready;
second=first->link;
while(second!
=NULL)
{
if((p->super)>(second->super))/*若插入进程比当前进程优先数大,*/
{/*插入到当前进程前面*/
p->link=second;
first->link=p;
second=NULL;
insert=1;
}
else/*插入进程优先数最低,则插入到队尾*/
{
first=first->link;
second=second->link;
}
}
if(insert==0)first->link=p;
}
}
voidinput()/*建立进程控制块函数*/
{
inti,num;
//clrscr();/*清屏*/
printf("\n请输入进程号?
");
scanf("%d",&num);
for(i=0;i{
printf("\n进程号No.%d:
\n",i);
p=getpch(PCB);
printf("\n输入进程名:
");
scanf("%s",p->name);
printf("\n输入进程优先数:
");
scanf("%d",&p->super);
printf("\n输入进程运行时间:
");
scanf("%d",&p->ntime);
printf("\n");
p->rtime=0;p->state='w';
p->link=NULL;
sort();/*调用sort函数*/
}
}
intspace()
{
intl=0;PCB*pr=ready;
while(pr!
=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
voiddisp(PCB*pr)/*建立进程显示函数,用于显示当前进程*/
{
printf("\nqname\tstate\tsuper\tndtime\truntime\n");
printf("|%s\t",pr->name);
printf("|%c\t",pr->state);
printf("|%d\t",pr->super);
printf("|%d\t",pr->ntime);
printf("|%d\t",pr->rtime);
printf("\n");
}
voidcheck()/*建立进程查看函数*/
{
PCB*pr;
printf("\n****当前正在运行的进程是:
%s",p->name);/*显示当前运行进程*/
disp(p);
pr=ready;
printf("\n****当前就绪队列状态为:
\n");/*显示就绪队列状态*/
while(pr!
=NULL)
{
disp(pr);
pr=pr->link;
}
}
voiddestroy()/*建立进程撤消函数(进程运行结束,撤消进程)*/
{
printf("\n进程[%s]已完成.\n",p->name);
free(p);
}
voidrunning()/*建立进程就绪函数(进程运行时间到,置就绪状态*/
{
(p->rtime)++;
if(p->rtime==p->ntime)
destroy();/*调用destroy函数*/
else
{
(p->super)--;
p->state='w';
sort();/*调用sort函数*/
}
}
intmain()/*主函数*/
{
intlen,h=0;
charch;
input();
len=space();
while((len!
=0)&&(ready!
=NULL))
{
ch=getchar();
h++;
printf("\nTheexecutenumber:
%d\n",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();
running();
printf("\n按任一键继续......");
ch=getchar();
}
printf("\n\n进程已经完成.\n");
ch=getchar();
return0;
}
5、运行结果
6、体会心得
题目2
1、实验要求:
编写并调试一个模拟的进程调度程序,采用“轮转法”调度算法对五个进程进行调度。
2、实验原理:
轮转法可以是简单轮转法、可变时间片轮转法,或多队列轮转法。
简单轮转法的基本思想是:
所有就绪进程按FCFS排成一个队列,总是把处理机分配给队首的进程,各进程占用CPU的时间片相同。
如果运行进程用完它的时间片后还为完成,就把它送回到就绪队列的末尾,把处理机重新分配给队首的进程。
直至所有的进程运行完毕。
3、流程图:
4、源代码:
#include"stdio.h"
#include
#include
#definegetpch(type)(type*)malloc(sizeof(type))
//#defineNULL0
structpcb{/*定义进程控制块PCB*/
charname[10];
charstate;
intsuper;
intntime;
intrtime;
structpcb*link;
}*ready=NULL,*p;
typedefstructpcbPCB;
voidsort()/*FCFS函数*/
{
PCB*loc;
PCB*re;
loc=ready;
if(ready==NULL)
ready=p;
else{
while(loc!
=NULL){
re=loc;
loc=loc->link;
}
re->link=p;
}
}
voidinput()/*建立进程控制块函数*/
{
inti,num=5;
printf("输入5个进程控制块:
");
for(i=0;i{
printf("\n进程号No.%d:
\n",i);
p=getpch(PCB);
printf("\n输入进程名:
");
scanf("%s",p->name);
printf("\n输入进程优先数:
");
scanf("%d",&p->super);
printf("\n输入进程运行时间:
");
scanf("%d",&p->ntime);
printf("\n");
p->rtime=0;
p->state='w';
p->link=NULL;
sort();/*调用sort函数*/
}
}
intspace()
{
intl=0;PCB*pr=ready;
while(pr!
=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
voiddisp(PCB*pr)/*建立进程显示函数,用于显示当前进程*/
{
printf("\nqname\tstate\tsuper\tndtimeruntime\n");
printf("|%s\t",pr->name);
printf("|%c\t",pr->state);
printf("|%d\t",pr->super);
printf("|%d\t",pr->ntime);
printf("|%d\t",pr->rtime);
printf("\n");
}
voidcheck()/*建立进程查看函数*/
{
PCB*pr;
printf("\n****当前正在运行的进程是:
%s",p->name);/*显示当前运行进程*/
disp(p);
pr=ready;
printf("\n****当前就绪队列状态为:
\n");/*显示就绪队列状态*/
while(pr!
=NULL)
{
disp(pr);
pr=pr->link;
}
}
voiddestroy()/*建立进程撤消函数(进程运行结束,撤消进程)*/
{
printf("\n进程[%s]已完成.\n",p->name);
free(p);
}
voidrunning()/*建立进程就绪函数(进程运行时间到,置就绪状态*/
{
(p->rtime)++;
if(p->rtime==p->ntime)
destroy();/*调用destroy函数*/
else
{
//(p->super)--;
p->state='w';
sort();/*调用sort函数*/
}
}
intmain()
{
intlen,h=0;
charch;
input();
len=space();
while((len!
=0)&&(ready!
=NULL))
{
ch=getchar();
h++;
printf("\nTheexcutenumber:
%d\n",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();
running();
printf("\n按任一键继续......");
ch=getchar();
}
printf("\n\n进程已经完成.\n");
ch=getchar();
return0;
}
5、运行结果:
6、体会心得:
实验二:
作业调度实验
题目1
1、实验要求
编写并调试一个单道处理系统的作业等待模拟程序。
作业等待算法:
分别采用先来先服务(FCFS),最短作业优先(SJF)、响应比高者优先(HRN)的调度算法。
对每种调度算法都要求打印每个作业开始运行时刻、完成时刻、周转时间、带权周转时间,以及这组作业的平均周转时间及带权平均周转时间,以比较各种算法的优缺点。
2、实验原理
FCFS:
在计算机并行任务处理中,被视为最简单的任务排序策略,即是无论任务大小和所需完成时间,对先到的任务先处理,后到的任务后处理。
它是一种非抢占式策略。
SJF:
以进入系统的作业所要求的CPU时间为标准,总选取估计计算时间最短的作业投入运行。
HRN:
最高响应比优先法(HRN)是对FCFS方式和SJF方式的一种综合平衡。
FCFS方式只考虑每个作业的等待时间而未考虑执行时间的长短,而SJF方式只考虑执行时间而未考虑等待时间的长短。
因此,这两种调度算法在某些极端情况下会带来某些不便。
3、流程图
4、源代码
#include
#include
usingnamespacestd;
structjcb{/*定义作业控制块PCB*/
stringszJname;//作业名
intiStime;//提交时间
intiRtime;//运行时间
intiBtime;//开始运行时间
intiFtime;//完成时间
floatfTtime;//周转时间
floatfWtime;//带权周转时间
charcState;//作业状态
floatfSuper;//优先权
structjcb*link;
}*ready=NULL,*p;
intiRecord=0;
voidFCFS(intiCnt)
{
iRecord=0;
floatfATtime=0;
floatfAWtime=0;
jcb*q=ready;
cout<<"作业名称\t开始运行时间\t完成时间\t周转时间\t带权周转时间"<for(;q!
=NULL;q=q->link)
{
q->iBtime=iRecord;
q->iFtime=q->iBtime+q->iRtime;
q->fTtime=q->iFtime-q->iStime;
q->fWtime=q->fTtime/q->iRtime;
cout<szJname<<"\t";
cout<iBtime<<"\t";
cout<iFtime<<"\t";
cout<fTtime<<"\t";
cout<fWtime<<"\t"<iRecord+=q->iRtime;
fATtime+=q->fTtime;
fAWtime+=q->fWtime;
}
cout<<"平均周转时间:
"<cout<<"平均带权周转时间:
"<}
voidSJF(intiCnt)
{
iRecord=0;
floatfATtime=0;
floatfAWtime=0;
jcb*pshort,*q;
cout<<"作业名称\t开始运行时间\t完成时间\t周转时间\t带权周转时间"<for(inti=0;i{
if(i==iCnt-1)
{
q=ready;
do
{
if(q->cState=='W')
{
pshort=q;
pshort->cState='F';
break;
}
q=q->link;
}while(q!
=NULL);
}
else
{
pshort=NULL;
q=ready;
do
{
if(q->cState=='W'&&pshort==NULL&&q->iStime<=iRecord)
pshort=q;
elseif(q->cState=='W'&&pshort->iRtime>q->iRtime&&q->iStime<=iRecord)
pshort=q;
q=q->link;
}while(q!
=NULL);
pshort->cState='F';
}
pshort->iBtime=iRecord;
pshort->iFtime=pshort->iBtime+pshort->iRtime;
pshort->fTtime=pshort->iFtime-pshort->iStime;
pshort->fWtime=pshort->fTtime/pshort->iRtime;
cout<szJname<<"\t";
cout<iBtime<<"\t";
cout<iFtime<<"\t";
cout<fTtime<<"\t";
cout<fWtime<<"\t"<iRecord+=pshort->iRtime;
fATtime+=pshort->fTtime;
fAWtime+=pshort->fWtime;
}
cout<<"平均周转时间:
"<cout<<"平均带权周转时间:
"<}
voidreset()//把作业的状态都重新设定为Wait
{
jcb*q=ready;
while(q!
=NULL)
{
q->cState='W';
q=q->link;
}
}
voidHRN(intiCnt)
{
iRecord=0;
floatfATtime=0;
floatfAWtime=0;
jcb*psuper,*q;
reset();//把作业的状态都重新设定为Wait
cout<<"作业名称\t开始运行时间\t完成时间\t周转时间\t带权周转时间"<for(inti=0;i{
if(i==iCnt-1)
{
q=ready;
do
{
if(q->cState=='W')
{
psuper=q;
psuper->cState='F';
break;
}
q=q->link;
}while(q!
=NULL);
}
else
{
psuper=NULL;
q=ready;
do
{
if(q->cState=='W'&&psuper==NULL&&q->iStime<=iRecord)
psuper=q;
elseif(q->cState=='W'&&((psuper->iRtime-psuper->iStime)+psuper->iRtime)/psuper->iRtime
>((q->iRtime-q->iStime)+q->iRtime)/q->iRtime&&q->iStime<=iRecord)
psuper=q;
q=q->link;
}while(q!
=NULL);
psuper->cState='F';
}
psuper->iBtime=iRecord;
psuper->iFtime=psuper->iBtime+psuper->iRtime;
psuper->fTtime=psuper->iFtime-psuper->iStime;
psuper->fWtime=psuper->fTtime/psuper->iRtime;
cout<szJname<<"\t";
cout<iBtime<<"\t";
cout<iFtime<<"\t";
cout<fTtime<<"\t";
cout<fWtime<<"\t"<iRecord+=psuper->iRtime;
fATtime+=psuper->fTtime;
fAWtime+=psuper->fWtime;
}
cout<<"平均周转时间:
"<cout<<"平均带权周转时间:
"<}
intmain()
{
intnum;
jcb*loc;
cout<<"输入作业数量:
";
cin>>num;
for(inti=0;i{
p=newjcb;
cout<cout<<"作业"<
"<cout<<"输入作业名:
";
cin>>p->szJname;
cout<<"输入所需的运行时间:
";
cin>>p->iRtime;
p->iStime=i;
p->cState='W';
p->link=NULL;
if(ready==NULL)
{
ready=p;
loc=p;
}
else
{
loc->link=p;