进程调度实验报告.docx
《进程调度实验报告.docx》由会员分享,可在线阅读,更多相关《进程调度实验报告.docx(12页珍藏版)》请在冰豆网上搜索。
进程调度实验报告
天津大学仁爱学院
操作系统
实验报告
实验类型:
必修
实验日期:
2014年4月18日
实验名称:
进程调度
实验地点:
二实验楼504
学生姓名:
李帅帅指导教师:
张磊
班级:
计科一班
计算机科学与技术系
实验报告内容:
1)实验目的
用c语言编写和调试一个进程调度程序,以加深对进程的概念及进程调度算法的理解。
2)实验器材和设备
硬件:
二实验楼504计算机
开发工具:
MicrosoftVisualC++6.0
3)实验任务
本实验模拟单处理器系统的进程调度,加深对进程的概念及进程调度算法的理解。
用c语言编写和调试一个进程调度的算法程序,有一些简单的界面,能够运行,仿真操作系统中进程调度的原理和过程。
通过对调度算法的模拟,进一步理解进程的基本概念,加深对进程运行状态和进程调度
4)实验原理
无论是在批处理系统还是分时系统中,用户进程数一般都多于处理机数、这将导致它们互相争夺处理机。
另外,系统进程也同样需要使用处理机。
这就要求进程调度程序按一定的策略,动态地把处理机分配给处于就绪队列中的某一个进程,以使之执行。
基本状态:
1.等待态:
等待某个事件的完成;
2.就绪态:
等待系统分配处理器以便运行;
3.运行态:
占有处理器正在运行。
运行态-等待态往往是由于等待外设,等待主存等资源分配或等待人工干预而引起的。
等待态-就绪态则是等待的条件已满足,只需分配到处理器后就能运行。
运行态-就绪态不是由于自身原因,而是由外界原因使运行状态的进程让出处理器,这时候就变成就绪态。
例如时间片用完,或有更高优先级的进程来抢占处理器等。
就绪态-运行态系统按某种策略选中就绪队列中的一个进程占用处理器,此时就变成了运行态
5)实验过程描述
a)打开MicrosoftVisualC++6.0,创建工程。
b)根据要求用c语言代码实现应用程序,并调试完成
c)运行程序,根据提示输入相应的字符。
d)输入实验测试内容,并观察执行窗口显示的过程。
e)重复c、d过程,认真体会领悟。
6)实验代码//Iss.cpp:
Definestheentrypointfortheconsoleapplication.//
#include"stdafx.h"
#include
#includevconio.h>
#include
#include#defineP_NUM3#defineP_TIME50enumstate{ready,execute,block,finish
};
struct
pcb{
charname[4];
int
priority;
int
cputime;
int
needtime;
int
count;
int
round;
stateprocess;
pcb
*next;
};
pcb*get_process()
{_
pcb*q;
pcb*t;
pcb*p;
inti=0;
t=(structpcb*)malloc(sizeof(pcb));p=(structpcb*)malloc(sizeof(pcb));cout<<"InputNameandTime"<while(iq=(structpcb*)malloc(sizeof(pcb));
cin>>q->name;
cin>>q->needtime;
q->cputime=O;
q->priority=P_TIME-q->needtime;
q->process=ready;
q->next=NULL;
if(i==0)
{
p=q;
t->next=q;
}
else
{
q->next=t->next;
t=q;
q=p;
}
i++;
}
returnp;
}
voiddisplay(pcb*p)
<<""
{
cout«"name"<<""<<"cputime"<<"needtime"<<""<<"priority"<<"state"<while(p)
{
cout<name;
cout<<"";
cout<cputime;
cout<<"";
cout<needtime;
cout<<"";
cout<priority;
cout<<"";
switch(p->process)
{
caseready:
cout<<"ready"<caseexecute:
cout<<"execute"<caseblock:
cout<<"block"<casefinish:
cout<<"finish"<p=p->next;
}
}
intprocess_finish(pcb*q)
{_
intb1=1;
while(b1&&q)
{
b1=b1&&q->needtime==O;q=q->next;
}
returnb1;
}
voidcpuexe(pcb*q)
{
pcb*t=q;
inttp=0;
while(q)
{
if(q->process!
=finish)
{q->process=ready;
if(q->needtime==0)
{q->process=finish;
}
}
if(tppriority&&q->process!
=finish){
tp=q->priority;t=q;
}q=q->next;
}
if(t->needtime!
=0)
{t->priority-=3;t->needtime--;
t->process=execute;t->cputime++;
voidpriority_cal()
{_
pcb*p;
system("cls");
P=get_process();
intcpu=0;
system("cls");
while(!
process_finish(p))
{_
cpu++;
cout«"cuptime:
"<cpuexe(p);
display(p);
Sleep(1000);
}
printf("Allprocesseshavefinished,pressanykeytoexit");
getch();
}
voiddisplay_menu()
{_
cout«"\nCHOOSETHEALGORITHMKendl;
cout<<"1PRIORITY"«endl;
cout<<"2roundrobin"<cout<<"3EXIT"<}//显示调度算法菜单,可供用户选择优先权调度算法和时间轮转调度算法
pcb*get_process_round()
{一一
pcb*q;
pcb*t;
pcb*p;
inti=0;
t=(structpcb*)malloc(sizeof(pcb));
p=(structpcb*)malloc(sizeof(pcb));
cout<<"inputnameandtime"<while(i{
q=(structpcb*)malloc(sizeof(pcb));
cin>>q->name;
cin>>q->needtime;
q->cputime=0;
q->round=0;
q->count=0;
q->process=ready;
q->next=NULL;
if(i==0)
{
p=q;
t->next=q;
}
else
{
q->next=t->next;t=q;q=p;
}
i++;
}
returnp;
}
voidcpu_round(pcb*q)
{_
q->cputime+=2;
q->needtime-=2;
if(q->needtime<0)
{
q->needtime=0;
}
q->count++;
q->round++;q->process=execute;
}
pcb*get_next(pcb*k,pcb*head)
{_
pcb*t;
t=k;
do{
t=t->next;
}
while(t&&t->process==finish);
if(t==NULL)
{
t=head;
while(t->next!
=k&&t->process==finish)
{
}
}returnt;
}voidset_state(pcb*p)
{_
while(p)
{
if(p->needtime==0)
{
p->process=finish;
//如果所需执行时间为0,则设置运行状态为结束
}
if(p->process==execute)
{p->process=ready;//如果未执行状态则设置为就绪
}
p->next;
}
}//设置队列中进程执行状态
voiddisplay」ound(pcb*p)
{_
coutvv"NAME<<""<""’ROUND"
<<'"<<"STATE'<while(p)
{
cout<name;
cout<<"";
cout<cputime;
cout<<"";
cout<needtime;
cout<<"";
cout<count;
cout<<"";
cout<round;
cout<<"";
switch(p->process)
{
caseready:
cout<<"ready"<cout<<"execute"<cout<<"finish"<}p=p->next;
}
}〃时间片轮转调度算法输出调度信息
voidround_cal()
{_
pcb*p;
pcb*r;
system("cls");
P=get_process_round();
intcpu=0;
system("cls");
r=p;
while(!
process_finish(p))
{_
cpu+=2;
cpu_round(r);
r=get_next(r,p);
cout<<"cpu"<set_state(p);
Sleep(1000);
}
}
intmain(intargc,char*argv[])
{
display_menu();
intk;
scanf("%d",&k);
while
(1)
{
switch(k){
case1:
priority_cal();break;
case2:
round_cal();break;
case3:
return0;
}
display_menu();scanf("%d",&k);
}
printf("\n本调度算法成功结束!
\n;");
return0;
7)实验结果截图
g作系统上机\lss\Debug\lss.eze
eputime-1
nameeputine
needtime
prioritysta
A1
1135
execute
eputine
nameeputime
needtime
prioritystate
a2
1032
execute
cputime:
3
nameeputime
needtime
prioritystate
a3
929
execute
cputime:
4
nameeputine
needtine
prioritystate
a4
826
execute
eputinc-5
Inameeputime
needtime
prioritystate
卜5
723
execute
leputime
InameCputime
ine
priorityState
U6
620
execute
eputirie:
7
namQcpu,tine
needtime
prioritysta
A7
517
execute
eputime-S
nameeputime
needtime
prioritystate
a8
414
exedutft
eputime:
9
namecputime
needtime
prioritystate
鬲?
311
execute
eputline:
18
nan?
cpu,tine
need^inc
pr*ioi*itysta七日
a10
28
execu
eputime-11
nameeputine
needtime
prioritystate
&11
1S
execute
eputime:
12
nameeputime
needtime
prioritystate
a12
02
execute
havefinishcd,pressan*/keytoexit
Allprocesses
CHOOSETHEALGORITHM:
1PRIORITY
2POUNVROBIN
3EXIT
►J
8)对实验的总结
本次实验,任务是用c语言代码实现进程调度模拟系统,从而观察进程的运行过程及加深对进程的了解。
这次实验,加深了我对进程概念及进程调度的理解;熟悉了进程调度算法。
使得理论知识得到的实践,也使我的编程能力得到了进一步提高。
实验中,我们小组共同学习、共同努力,虽然在实验中遇到了一些问题,但在查阅资料后都解决了。
实验过程也让我认识到自己的不足,好多知识已经模糊不清,在今后自己要多一些实验及相关的小项目,来提高自己的编程能力。