操作系统上机实验资料Word文档格式.docx
《操作系统上机实验资料Word文档格式.docx》由会员分享,可在线阅读,更多相关《操作系统上机实验资料Word文档格式.docx(35页珍藏版)》请在冰点文库上搜索。
进程名、优先数、到达时间、需要运行时间、已用CPU时间、进程状态等等。
进程的优先数及需要的运行时间可以事先人为地指定(也可以由随机数产生)。
进程的到达时间为进程输入的时间。
进程的运行时间以时间片为单位进行计算。
每个进程的状态可以是就绪W(Wait)、运行R(Run)、或完成F(Finish)三种状态之一。
就绪进程获得CPU后都只能运行一个时间片。
用已占用CPU时间加1来表示。
如果运行一个时间片后,进程的已占用CPU时间已达到所需要的运行时间,则撤消该进程,如果运行一个时间片后进程的已占用CPU时间还未达所需要的运行时间,也就是进程还需要继续运行,此时应将进程的优先数减1(即降低一级),然后把它插入就绪队列等待CPU。
每进行一次调度程序都打印一次运行进程、就绪队列、以及各个进程的PCB,以便进行检查。
重复以上过程,直到所要进程都完成为止。
调度算法的流程图如下:
进程调度源程序如下:
jingchendiaodu.cpp
#include"
stdio.h"
#include<
stdlib.h>
conio.h>
#definegetpch(type)(type*)malloc(sizeof(type))
#defineNULL0
structpcb{/*定义进程控制块PCB*/
charname[10];
charstate;
intsuper;
intntime;
intrtime;
structpcb*link;
}*ready=NULL,*p;
typedefstructpcbPCB;
sort()/*建立对进程进行优先级排列函数*/
{
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->
(second->
super))/*若插入进程比当前进程优先数大,*/
{/*插入到当前进程前面*/
link=second;
first->
link=p;
second=NULL;
insert=1;
else/*插入进程优先数最低,则插入到队尾*/
first=first->
second=second->
if(insert==0)first->
input()/*建立进程控制块函数*/
inti,num;
clrscr();
/*清屏*/
printf("
\n请输入进程号?
"
);
scanf("
%d"
&
num);
for(i=0;
i<
num;
i++)
\n进程号No.%d:
\n"
i);
p=getpch(PCB);
\n输入进程名:
%s"
p->
name);
\n输入进程优先数:
super);
\n输入进程运行时间:
ntime);
rtime=0;
state='
w'
;
link=NULL;
sort();
/*调用sort函数*/
intspace()
intl=0;
PCB*pr=ready;
while(pr!
l++;
pr=pr->
return(l);
disp(PCB*pr)/*建立进程显示函数,用于显示当前进程*/
\nqname\tstate\tsuper\tndtime\truntime\n"
|%s\t"
pr->
|%c\t"
state);
|%d\t"
rtime);
check()/*建立进程查看函数*/
PCB*pr;
\n****当前正在运行的进程是:
/*显示当前运行进程*/
disp(p);
pr=ready;
\n****当前就绪队列状态为:
/*显示就绪队列状态*/
disp(pr);
destroy()/*建立进程撤消函数(进程运行结束,撤消进程)*/
\n进程[%s]已完成.\n"
free(p);
running()/*建立进程就绪函数(进程运行时间到,置就绪状态*/
(p->
rtime)++;
if(p->
rtime==p->
ntime)
destroy();
/*调用destroy函数*/
else
super)--;
/*调用sort函数*/
main()/*主函数*/
intlen,h=0;
charch;
input();
len=space();
while((len!
=0)&
&
(ready!
=NULL))
ch=getchar();
h++;
\nTheexecutenumber:
%d\n"
h);
p=ready;
ready=p->
R'
check();
running();
\n按任一键继续......"
\n\n进程已经完成.\n"
}
实验二、银行家算法
(一)目的和要求
银行家算法是由Dijkstra设计的最具有代表性的避免死锁的算法。
本实验要求用高级语言编写一个银行家的模拟算法。
通过本实验可以对预防死锁和银行家算法有更深刻的认识。
(二)实验内容
1、设置数据结构
包括可利用资源向量(Availiable),最大需求矩阵(Max),分配矩阵(Allocation),需求矩阵(Need)
2、设计安全性算法
设置工作向量Work表示系统可提供进程继续运行可利用资源数目,Finish表示系统是否有足够的资源分配给进程
(三)实验环境
1、pc
2、vc++
(四)、程序源代码:
/*子函数声明*/
intIsprocessallover();
//判断系统中的进程是否全部运行完毕
voidSystemstatus();
//显示当前系统中的资源及进程情况
intBanker(int,int*);
//银行家算法
voidAllow(int,int*);
//若进程申请不导致死锁,用此函数分配资源
voidForbidenseason(int);
//若发生死锁,则显示原因
/*全局变量*/
intAvailiable[3]={3,3,2};
//初始状态,系统可用资源量
intMax[5][3]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
//各进程对各资源的最大需求量
intAllocation[5][3]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
//初始状态,各进程占有资源量
intNeed[5][3]={{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}};
//初始状态时,各进程运行完毕,还需要的资源量
intover[5]={0,0,0,0,0};
//标记对应进程是否得到所有资源并运行完毕
iostream.h>
/*主函数*/
voidmain()
intprocess=0;
//发出请求的进程
intdecide=0;
//银行家算法的返回值
intRequest[3]={0,0,0};
//申请的资源量数组
intsourcenum=0;
//申请的各资源量
/*判断系统中进程是否全部运行完毕*/
step1:
if(Isprocessallover()==1)
{
cout<
<
系统中全部进程运行完毕!
return;
}
/*显示系统当前状态*/
Systemstatus();
/*人机交互界面*/
step2:
cout<
\n输入发出请求的进程(输入0退出系统):
"
cin>
>
process;
if(process==0)
放弃申请,退出系统!
if(process<
1||process>
5||over[process-1]==1)
系统无此进程!
gotostep2;
此进程申请各资源(A,B,C)数目:
for(inth=0;
h<
3;
h++)
char(65+h)<
资源:
sourcenum;
Request[h]=sourcenum;
/*用银行家算法判断是否能够进行分配*/
decide=Banker(process,Request);
if(decide==0)
/*将此进程申请资源分配给它*/
Allow(process,Request);
gotostep1;
else
/*不能分配,显示原因*/
Forbidenseason(decide);
}
/*子函数Isprocessallover()的实现*/
intIsprocessallover()
{
intprocessnum=0;
for(inti=0;
5;
i++)
/*判断每个进程是否运行完毕*/
if(over[i]==1)
processnum++;
if(processnum==5)
/*系统中全部进程运行完毕*/
return1;
return0;
/*子函数Systemstatus()的实现*/
voidSystemstatus()
此刻系统中存在的进程:
if(over[i]!
=1)
cout<
P"
i+1<
endl;
此刻系统可利用资源(单位:
个):
ABC\n"
for(inta=0;
a<
a++)
Availiable[a]<
此刻各进程已占有资源如下(单位:
个):
\n"
<
ABC\n"
for(intb=0;
b<
b++)
if(over[b]==1)
continue;
b+1<
for(intc=0;
c<
c++)
Allocation[b][c]<
各进程运行完毕还需各资源如下(单位:
for(intf=0;
f<
f++)
if(over[f]==1)
f+1<
for(intg=0;
g<
g++)
Need[f][g]<
/*子函数Banker(int,int&
)的实现*/
intBanker(intp,int*R)
intnum=0;
//标记各资源是否能满足各进程需要
intFinish[5]={0,0,0,0,0};
//标记各进程是否安全运行完毕
intwork[5]={0,0,0,0,0};
//用于安全检查
intAvailiableTest[3];
//用于试分配
intAllocationTest[5][3];
//同上
intNeedTest[5][3];
/*判断申请的资源是否大于系统可提供的资源总量*/
for(intj=0;
j<
j++)
if(*(R+j)>
Availiable[j])
/*返回拒绝分配原因*/
/*判断该进程申请资源量是否大于初始时其申明的需求量*/
{
if(*(R+i)>
Need[p-1][i])
/*返回拒绝原因*/
return2;
/*为检查分配的各数据结构赋初值*/
for(intt=0;
t<
t++)
AvailiableTest[t]=Availiable[t];
for(intu=0;
u<
u++)
for(intv=0;
v<
v++)
AllocationTest[u][v]=Allocation[u][v];
for(intw=0;
w<
w++)
for(intx=0;
x<
x++)
NeedTest[w][x]=Need[w][x];
/*进行试分配*/
for(intk=0;
k<
k++)//修改NeedTest[]
AvailiableTest[k]-=*(R+k);
AllocationTest[p-1][k]+=*(R+k);
NeedTest[p-1][k]-=*(R+k);
/*检测进程申请得到满足后,系统是否处于安全状态*/
for(intl=0;
l<
l++)
work[l]=AvailiableTest[l];
for(intm=1;
m<
=5;
m++)
for(intn=0;
n<
n++)
num=0;
/*寻找用此刻系统中没有运行完的进程*/
if(Finish[n]==0&
over[n]!
=1)
{
for(intp=0;
p<
p++)
{
if(NeedTest[n][p]<
=work[p])
num++;
}
if(num==3)
for(intq=0;
q<
q++)
{
work[q]=work[q]+AllocationTest[n][q];
}
Finish[n]=1;
}
for(intr=0;
r<
r++)
if(Finish[r]==0&
over[r]!
/*返回拒绝分配原因*/
return3;
return0;
/*子函数Allow(int,int&
voidAllow(intp,int*R)
可以满足申请!
staticintovernum;
/*对进程所需的资源进行分配*/
t++)
Availiable[t]=Availiable[t]-*(R+t);
Allocation[p-1][t]=Allocation[p-1][t]+*(R+t);
Need[p-1][t]=Need[p-1][t]-*(R+t);
/*分配后判断其是否运行完毕*/
overnum=0;
if(Need[p-1][v]==0)
overnum++;
if(overnum==3)
/*此进程运行完毕,释放其占有的全部资源*/
for(intq=0;
Availiable[q]=Availiable[q]+Allocation[p-1][q];
/*标记该进程运行完毕*/
over[p-1]=1;
进程P"
所需资源全部满足,此进程运行完毕!
/*子函数Forbidenseason(int)的实现*/
voidForbidenseason(intd)
不能满足申请,此进程挂起,原因为:
switch(d)
case1:
cout<
申请的资源量大于系统可提供的资源量!
break;
case2:
申请的资源中有某种资源大于其声明的需求量!
case3:
若满足申请,系统将进入不安全状态,可能导致死锁!
实验三、页式地址重定位模拟
一、实验目的:
1、用高级语言编写和调试模拟实现页式地址重定位。
2、加深理解页式地址重定位技术在多道程序设计中的作用和意义。
二、实验原理:
当进程在CPU上运行时,如指令中涉及逻辑地址时,操作系统自动根据页长得到页号和页内偏移,把页内偏移拷贝到物理地址寄存器,再根据页号,查页表,得到该页在内存中的块号,把块号左移页长的位数,写到物理地址寄存器。
三、实验内容:
1、设计页表结构
2、设计地址重定位算法
3、有良好的人机对话界面
四、程序源代码:
#definepagesize1024
#definepagetablelength64
/*系统页表*/
constintpagetable[pagetablelength]={0,42,29,15,45,31,44,43,
41,28,1,30,12,24,6,32,
14,27,13,46,7,33,10,22,
40,2,51,11,39,23,49,50,
26,16,25,4,47,17,3,48,
52,36,58,35,57,34,21,63,
5,37,18,8,62,56,20,54,
60,19,38,9,61,55,59,53};
#include<
iomanip.h>
intlogicaladdress=0;
intpagenum=0;
intw=0;
系统页号对应块号情况(页号——>
块号):
for(