操作系统实验讲诉Word文档下载推荐.docx
《操作系统实验讲诉Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《操作系统实验讲诉Word文档下载推荐.docx(14页珍藏版)》请在冰豆网上搜索。
Need[i]=Need[i]–Requesti
5)系统调用安全性算法进行资源分配检查,若安全则执行分配,否则恢复试探分配前状态,并使Pi等待。
2、银行家算法之安全性子算法:
1)令Work=Available,Finish=FALSE
2)从进程集合中查找一个满足Finish[i]=FALSE且Need[i]<
=Work的进程Pi。
若找到,则可假定Pi能获得所需资源并顺利执行,故有:
Work=Work+Allocation[i]
Finish[i]=True
然后重复执行第2步;
否则转至第3步执行
3)如果Finish=TRUE,则表示系统处于安全状态;
否则系统处于不安全状态
三.实验内容
1、创建C语言工程项目,按照教材上的有关说明,定义相应的数据结构。
intAllocMatrix[5][4]//已经分配资源矩阵
intRequestMatrix[5][4]//需求矩阵
intAvailResource[4]//可用资源向量
intTryProcess[5]//尝试序列
2、给各个数据结构设定合适的初始值。
按照教材课后习题31的内容给上述数据结构设定初始值。
如:
intAllocMatrix[5][4]={//已经分配资源矩阵
{0,0,3,2},
{1,0,0,0},
{1,3,5,4},
{0,3,3,2},
{0,0,1,4}
};
注意:
步骤1、2可同时进行,即利用C语言中的定义变量就可同时初始化的方式进行数值初设。
3、依据银行家算法的描述依次进行资源的试探性分配,直至成功或失败,成功则说明当前状态是安全的;
失败后,还应该将资源回到初始状态,并进行另一次试探;
只有所有的试探都失败了,才能说明当前状态是不安全的。
通常,这种试探性算法采用递归的方法是很合适的,程序也是很简洁的。
代码片段:
if(AvailResource[0]>
=RequestMatrix[k][0]
&
&
AvailResource[1]>
=RequestMatrix[k][1]
AvailResource[2]>
=RequestMatrix[k][2]
AvailResource[3]>
=RequestMatrix[k][3])//若资源能使进程ProcessID结束
for(inti=0;
i<
4;
i++)
AvailResource[i]+=AllocMatrix[k][i];
//恢复资源
intSearchSecurity(intlevel)//递归函数
{
if(level==4){
……
}
else{
SearchSecurity(level+1);
//有条件递归调用自己
……
}其余由大家自行完成。
四.实验程序
#include"
malloc.h"
stdio.h"
stdlib.h"
#definealloclensizeof(structallocation)
#definemaxlensizeof(structmax)
#defineavalensizeof(structavailable)
#defineneedlensizeof(structneed)
#definefinilensizeof(structfinish)
#definepathlensizeof(structpath)
structallocation
{
intvalue;
structallocation*next;
};
structmax
structmax*next;
structavailable/*可用资源数*/
structavailable*next;
structneed/*需求资源数*/
structneed*next;
structpath
structpath*next;
structfinish
intstat;
structfinish*next;
intmain()
introw,colum,status=0,i,j,t,temp,processtest;
structallocation*allochead,*alloc1,*alloc2,*alloctemp;
structmax*maxhead,*maxium1,*maxium2,*maxtemp;
structavailable*avahead,*available1,*available2,*workhead,*work1,*work2,*worktemp,*worktemp1;
structneed*needhead,*need1,*need2,*needtemp;
structfinish*finihead,*finish1,*finish2,*finishtemp;
structpath*pathhead,*path1,*path2;
printf("
\n请输入系统资源的种类数:
"
);
scanf("
%d"
&
colum);
请输入现时内存中的进程数:
row);
请输入已分配资源矩阵:
\n"
for(i=0;
row;
for(j=0;
j<
colum;
j++)
请输入已分配给进程p%d的%c种系统资源:
i,'
A'
+j);
if(status==0)
allochead=alloc1=alloc2=(structallocation*)malloc(alloclen);
alloc1->
next=alloc2->
next=NULL;
allochead->
value);
status++;
}
else
alloc2=(structallocation*)malloc(alloclen);
%d,%d"
alloc2->
if(status==1)
next=alloc2;
alloc1=alloc2;
status=0;
请输入最大需求矩阵:
请输入进程p%d种类%c系统资源最大需求:
maxhead=maxium1=maxium2=(structmax*)malloc(maxlen);
maxium1->
next=maxium2->
maxium2=(structmax*)malloc(maxlen);
maxium2->
maxhead->
next=maxium2;
maxium1=maxium2;
请输入现时系统剩余的资源矩阵:
种类%c的系统资源剩余:
'
avahead=available1=available2=(structavailable*)malloc(avalen);
workhead=work1=work2=(structavailable*)malloc(avalen);
available1->
next=available2->
work1->
next=work2->
value=available1->
value;
available2=(structavailable*)malloc(avalen);
work2=(structavailable*)malloc(avalen);
available2->
work2->
value=available2->
avahead->
next=available2;
workhead->
next=work2;
available1=available2;
work1=work2;
alloctemp=allochead;
maxtemp=maxhead;
needhead=need1=need2=(structneed*)malloc(needlen);
need1->
next=need2->
value=maxtemp->
value-alloctemp->
need2=(structneed*)malloc(needlen);
need2->
value=(maxtemp->
value)-(alloctemp->
needhead->
next=need2;
need1=need2;
maxtemp=maxtemp->
next;
alloctemp=alloctemp->
finihead=finish1=finish2=(structfinish*)malloc(finilen);
finish1->
next=finish2->
stat=0;
finish2=(structfinish*)malloc(finilen);
finish2->
finihead->
next=finish2;
finish1=finish2;
/*Initializationcompleated*/
processtest=0;
for(temp=0;
temp<
temp++)
needtemp=needhead;
finishtemp=finihead;
worktemp=workhead;
worktemp1=worktemp;
if(finishtemp->
stat==0)
for(j=0;
j++,needtemp=needtemp->
next,worktemp=worktemp->
next)
if(needtemp->
value<
=worktemp->
value)
processtest++;
if(processtest==colum)
worktemp1->
value+=alloctemp->
worktemp1=worktemp1->
pathhead=path1=path2=(structpath*)malloc(pathlen);
path1->
next=path2->
value=i;
path2=(structpath*)malloc(pathlen);
path2->
pathhead->
next=path2;
path1=path2;
finishtemp->
stat=1;
for(t=0;
t<
t++)
needtemp=needtemp->
finishtemp=finishtemp->
\n系统处于非安全状态!
exit(0);
\n系统处于安全状态.\n"
\n安全序列为:
\n"
do
p%d"
pathhead->
while(pathhead=pathhead->
next);
return0;
四.运行截图
五.实验总结
通过本次实验,加深了我对银行家算法的了解,懂得了如何利用银行家算法避免死锁。
实验中遇到很多困难,通过查阅相关资料,还有网上的资源,,能够很快的解决。
银行家算法是为了使系统保持安全状态。