南昌大学操作系统实验报告WORDWord格式.docx
《南昌大学操作系统实验报告WORDWord格式.docx》由会员分享,可在线阅读,更多相关《南昌大学操作系统实验报告WORDWord格式.docx(14页珍藏版)》请在冰豆网上搜索。
//systemclock
structblock
{
inttime_used;
intprior;
};
//定义结构体,使用时间,优先级
typedefstructpro
charname[20];
enumstastatus;
inttime_required;
intarrive_time;
structblockswitch_block;
structpro*next;
}*pcb;
//定义pcb块
pcbready;
voidmenu();
voidinsertpcb(pcbnewp)//插入至就绪队列
pcbp;
p=(pcb)malloc(sizeof(structpro));
if(!
p)exit(overflow);
p=ready;
if(p->
next==null)
{
newp->
next=ready->
next;
ready->
next=newp;
}
else
while(newp->
switch_block.prior<
p->
next->
switch_block.prior&
&
next!
=null)
p=p->
next=p->
p->
}
voidcreat_pcb()//创建一个进程
pcbnewp;
newp=(pcb)malloc(sizeof(structpro));
newp)exit(overflow);
newp->
status=wait;
time(&
t);
arrive_time=t;
printf("
processname:
"
);
scanf("
%s"
newp->
name);
priority:
%d"
&
newp->
switch_block.prior);
arrivetime:
arrive_time);
cputime_required:
time_required);
switch_block.time_used=0;
insertpcb(newp);
menu();
voidprintpcb()//打印现状
if(ready->
next){
printf("
namestatuspriorityserver_timearrive_timecputime_used\n"
for(p=ready->
p!
=null;
p=p->
next)
{
printf("
p->
switch(p->
status)
{
casewait:
printf("
wait"
break;
caserun:
run"
casefinished:
finished"
}
%7d"
%15d"
%20d"
%13d"
switch_block.time_used);
putchar('
\n'
}
elseprintf("
queueisempty!
\n"
voidswitchprocess()//将内存中的进程切换至外存
chartemp[20];
pcbp,q;
inputprocessname:
scanf("
temp);
for(p=ready;
=null&
strcmp(temp,p->
name)!
=0;
next);
if(p!
q=p->
p->
next=q->
remove%ssuccessfully!
q->
free(q);
printpcb();
elseprintf("
foundnoprocess\n"
queueempty!
voidrunprocess()//进程调度
pcbp=ready->
if(p)
%sisrunning.....\n"
status=run;
switch_block.prior--;
switch_block.time_used+=runtime;
if(p->
switch_block.time_used>
=p->
time_required)
status=finished;
ready->
%shavebeenremovedfromtheready_queue....\n"
free(p);
else
%shaveuseduptherun_time\n"
insertpcb(p);
voidmenu()//选择菜单
intcoos;
1.creataprocess\n"
2.runaprocess\n"
3.removeaprocess\n"
4.exit\n"
choose(1-4):
coos);
switch(coos)
case1:
creat_pcb();
case2:
runprocess();
case3:
switchprocess();
case4:
exit(0);
intmain()
//clrscr();
ready=(pcb)malloc(sizeof(structpro));
ready)exit(overflow);
ready->
next=null;
return0;
实验过程:
优先度高者调度算法实验结果截图:
先创建进程a、b、c,
设a进程优先级为2,到达时间为2,cpu处理时间为1;
设b进程优先级为5,到达时间为0,cpu处理时间为6;
设c进程优先级为4,到达时间为2,cpu处理时间为2;
设定进程a、b、c
开始运行进程,首先运行0时刻到达的优先级最高的进程b
运行结束后,进程b的优先级变为4,cpu使用时间为2,此时进程b和c优先级相同,系统仍然调用进程b;
再次运行进程b以后,b的优先级掉为3,而且进程b还差2个时间单位才能运行结束,此时c为优先级最高的进程,系统调用进程c
调度进程c
因为进程c的cpu需要时间仅为2个时间单位,所以进程c运行结束,finished,c被移除就绪序列;
进程b回归
进程c结束
然后系统重新调用进程b,b运行结束;
进程b结束
调度进程a
最后系统调度优先级最低的进程a,结束进程调度过程,队列空。
先来先服务算法:
#include<
iostream>
iomanip>
using
namespace
std;
//FCFS
struct
process{
char
name[10];
double
arrivetime;
//到到时间
servetime;
//服务时间
starttime;
//开始时间
finnishtime;
//完成时间
circletime;
//周转时间
dcircletime;
//带权周转时间
}a[100];
//用结构体存放各进程的相关信息
int
main()
i,j,k,n;
process
temp;
cout<
<
请输入进程数:
;
cin>
>
n;
请输入进程的信息(包括进程名、进程到达时间、进程服务时间):
for(i=0;
i<
i++)
请输入第"
i+1<
个进程的信息:
a[i].name>
a[i].arrivetime>
a[i].servetime;
for(j=0;
j<
i;
j++)
if(a[i].arrivetime<
a[j].arrivetime){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}//按照FCFS算法对进程进行排序
\nFCFS进程调度顺序为:
for(k=0;
k<
k++)
a[k].name;
endl;
a[0].starttime=a[0].arrivetime;
a[0].finnishtime=a[0].servetime+a[0].starttime;
a[0].circletime=a[0].finnishtime-a[0].arrivetime;
a[0].dcircletime=a[0].circletime/a[0].servetime;
//计算第一个进程的相关信息
for(i=1;
a[i].starttime=a[i-1].servetime+a[i-1].starttime;
a[i].finnishtime=a[i].servetime+a[i].starttime;
a[i].circletime=a[i].finnishtime-a[i].arrivetime;
a[i].dcircletime=a[i].circletime/a[i].servetime;
}//计算其他进程的相关信息
\n进程调度列表:
名称"
setw(10)<
到达时间"
服务时间"
开始时间"
完成时间"
周转时间"
setw(14)<
带权周转时间\n"
a[i].name<
a[i].arrivetime<
a[i].servetime<
a[i].starttime<
a[i].finnishtime<
a[i].circletime<
a[i].dcircletime<
return
0;
实验结果及其解释:
周转时间=完成时间-到达时间
带权周转时间=周转时间/服务时间
很明显地观察到先到达的进程先开始运行,直到该进程结束运行后才调度下一个进程。
进程调度顺序为D、C、A、B,实现了FirstComeFirstServed。
五、实验体会或对改进实验的建议
本次实验做的是进程调度算法中的优先度高者调度算法和先来先服务算法。
其中按优先级高的调度算法采用的是动态优先级,即优先级在进程创建之初被赋予,然后其值随着进程的推进而减少,有较好的调度性能。
而FCFS则是将新进入内存的进程放入队列的末尾,按先来先服务的原则等待调度。
在做FCFS的时候,在虚拟机上出了点问题,所以直接用win8下的dev解决了。
通过这两个实验对比着加深了对按优先度高者调度算法和先来先服务算法的理解和掌握。
六、参考资料
《计算机操作系统》修订版汤子瀛主编西安电子科技大学出版社