数理系操作系统原理实验指导书.docx
《数理系操作系统原理实验指导书.docx》由会员分享,可在线阅读,更多相关《数理系操作系统原理实验指导书.docx(34页珍藏版)》请在冰豆网上搜索。
数理系操作系统原理实验指导书
操作系统实验指导书
实验一 进程调度
1.目的和要求
进程调度是处理机管理的核心内容。
本实验要求用一种语言编写和调试一个简单的进程调度程序。
通过本实验可以加深理解有关进程控制块、进程队列的概念,并体会和了解优先数和时间片轮转调度算法的具体实施办法。
2.实验内容
①设计进程控制块PCB表结构,分别适用于优先数调度算法和循环轮转调度算法。
②建立进程就绪队列。
对两种不同算法编制入队子程序。
③编制两种进程调度算法:
1)优先数调度;2)循环轮转调度
3.实验环境
Windows系统,语言自选(建议C++)
4.实验提示
①本程序用两种算法对五个进程进行调度,每个进程可有三个状态,并假设初始状态为就绪状态。
②为了便于处理,程序中的某进程运行时间以时间片为单位计算。
各进程的优先数或轮转时间数以及进程需运行的时间片数的初始值均由用户给定。
③在优先数算法中,优先数可以先取值为98,进程每执行一次,优先数减3,CPU时间片数加1,进程还需要的时间片数减1。
在轮转算法中,采用固定时间片(即:
每执行一次进程,该进程的执行时间片数为已执行了2个单位),这时,CPU时间片数加2,进程还需要的时间片数减2,并排列到就绪队列的尾上。
④对于遇到优先数一致的情况,采用FIFS策略解决。
5、学时数:
2个学时
程序代码如下:
#include
#include
#include
#include
#include
#defineP_NUM5
#defineP_TIME50
enumstate{
ready,
execute,
block,
finish
};
structpcb{
charname[4];
intpriority;
intcputime;
intneedtime;
intcount;
intround;
stateprocess;
pcb*next;
};
pcb*get_process();
pcb*get_process(){
pcb*q;
pcb*t;
pcb*p;
inti=0;
cout<<"inputnameandtime"<while(iq=(structpcb*)malloc(sizeof(pcb));
cin>>q->name;
cin>>q->needtime;
q->cputime=0;
q->priority=P_TIME-q->needtime;
q->process=ready;
q->next=NULL;
if(i==0){
p=q;
t=q;
}
else{
t->next=q;
t=q;
}
i++;
}//while
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){
intbl=1;
while(bl&&q){
bl=bl&&q->needtime==0;
q=q->next;
}
returnbl;
}
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;
p=get_process();
intcpu=0;
while(!
process_finish(p)){
cpu++;
cout<<"cputime:
"<cpuexe(p);
display(p);
}
printf("Allprocesseshavefinished,pressanykeytoexit");
getch();
}
voiddisplay_menu(){
cout<<"CHOOSETHEALGORITHM:
"<cout<<"1PRIORITY"<cout<<"2ROUNDROBIN"<cout<<"3EXIT"<}
pcb*get_process_round(){
pcb*q;
pcb*t;
pcb*p;
inti=0;
cout<<"inputnameandtime"<while(iq=(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=q;
}
else{
t->next=q;
t=q;
}
i++;
}//while
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){
t=t->next;
}
}
returnt;
}
voidset_state(pcb*p){
while(p){
if(p->needtime==0){
p->process=finish;
}
if(p->process==execute){
p->process=ready;
}
p=p->next;
}
}
voiddisplay_round(pcb*p){
cout<<"NAME"<<""<<"CPUTIME"<<""<<"NEEDTIME"<<""<<"COUNT"<<""<<"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"<caseexecute:
cout<<"execute"<casefinish:
cout<<"finish"<}
p=p->next;
}
}
voidround_cal(){
pcb*p;
pcb*r;
p=get_process_round();
intcpu=0;
r=p;
while(!
process_finish(p)){
cpu+=2;
cpu_round(r);
r=get_next(r,p);
cout<<"cpu"<display_round(p);
set_state(p);
}
}
voidmain(){
display_menu();
intk;
scanf("%d",&k);
switch(k){
case1:
priority_cal();break;
case2:
round_cal();break;
case3:
break;
display_menu();
scanf("%d",&k);
}
}
6.设计两组实验数据,记录实验结果并分析。
实验二银行家算法避免死锁实验
一、实验目的:
1)理解死锁基本概念,掌握产生死锁的原因和必要条件以及处理死锁的几种方法,体会银行家算法是避免死锁的一种行之有效的方法。
2)通过编写程序实现银行家算法,进一步理解银行家算法的原理和避免死锁的过程,掌握银行家算法的描述和应用,进一步熟练掌握处理处理机死锁的方法。
二、实验内容
(1)首先对银行家算法原理进行深刻的理解和掌握;
(2)选择一种熟悉的编程语言来实现对N个进程和M种系统资源进行银行家算法判定和资源分配;
(3)根据银行家算法的要求设计相应的数据结构,如:
可利用资源向量、最大需求矩阵、分配矩阵和需求矩阵,其中进程的个数N和资源的种类数目用户可以任意交互输入;
(4)安全性判定要显示过程,要给出找到的安全序列。
三、实验环境
Windows系统,语言自选(建议C++)
四、实验提示
1、初始化
由用户输