文档介绍:淮海工学院计算机工程学院
实验报告书
课程名: 《操作系统原理》
题 目: 银行家算法
班 级:
学 号:
姓 名:
评语:
成绩: 指导教师:
批阅时间: 年 月 日
由于资源占用
是互斥的,当某个进程提出申请资源后,使得有关进程在无外力协助下,永远分配不到必需的资源
而无法继续运行,这就产生了一种特殊现象死锁。
通过这次实验,加深了我对银行家算法的了解,掌握了如何利用银行家算法避免死锁。在实验
中,难免会遇到问题,通过自己在网上查找资料、询问同学,这些问题都得到了解决,完成了本次
实验。通过这次的实验,使我的理论知识更加的牢固。
附录
#include<>
#include<>
#include<>
#defineFalse0
#defineTrue1
intMax[100][100]={0};// 各进程所需各类资源的最大需求
intAvaliable[100]={0};// 系统可用资源
charname[100]={0};// 资源的名称
intAllocation[100][100]={0};// 系统已分配资源
intNeed[100][100]={0};// 还需要资源
intRequest[100]={0};// 请求资源向量
inttemp[100]={0};// 存放安全序列
intWork[100]={0};// 存放系统可提供资源
intM=100;// 作业的最大数为 100
intN=100;// 资源的最大数为 100
voidshowdata()// 显示资源矩阵
{
inti,j;
cout<<" 系统目前可用的资源
[Avaliable]:"<<endl;
for(i=0;i<N;i++)
cout<<name[i]<<"";
cout<<endl;
for(j=0;j<N;j++)
cout<<Avaliable[j]<<"";//
输出分配资源
cout<<endl;
cout<<" Max Allocation Need"<<endl;
cout<<" 进程名 ";
for(j=0;j<3;j++){
for(i=0;i<N;i++)
cout<<name[i]<<"";
cout<<" ";
}
cout<<endl;
for(i=0;i<M;i++){
cout<<""<<i<<" ";
for(j=0;j<N;j++)
cout<<Max[i][j]<<"";
cout<<" ";
for(j=0;j<N;j++)
cout<<Allocation[i][j]<<"";
cout<<" ";
for(j=0;j<N;j++)
cout<<Need[i][j]<<"";
cout<<endl;
}
}
intchangdata(inti)// 进行资源分配
{
intj;
for(j=0;j<M;j++){
Avaliable[j]=Avaliable[j]-Request[j];
Allocation[i][j]=Allocation[i][j]+Request[j];
Need[i][j]=Need[i][j]-Request[j];
}
return1;
}
intsafe()// 安全性算法
{
inti,k=0,m,apply,Finish[100]={0};
intj;
intflag=0;
Work[0]=Avaliable[0];
Work[1]=Avaliable[1];
Work[2]=Avaliable[2];
for(i=0;i<M;i++){
apply=0;
for(j=0;j<N;j++){
if(Finish[i]==False&&Need[i][j]<=Work[j]){
apply++;
if(apply==N){
for(m=0;m<N;m++)
Work[m]=Work[m]+Allocation[i][m];// 变分配数
Finish[i]=True;
temp[k]=i;
i=-1;
k++;
flag++;
}
}
}
}
for(i=0;i<M;i++){
if(Finish[i]==False){
cout<<" 系统不安全"<<endl;// 不成功系统不安全
return-1;
}
}
cout<<" 系统是安全的!"<<endl;// 如果安全,输出成功
cout<<" 分配的序列:";
for(i=0;i<M;i++){// 输出运行进程数组
cout<<temp[i];
if(i<M-1)cout<<"->";
}
cout<<endl;
return0;
}
voidshare()// 利用银行家算法对申请