您的位置:首页 > 其它

操作系统 - 模拟银行家算法实验

2017-06-15 23:36 134 查看
/*   实验以3.7.4为例
N=5个进程(p0,p1,p2,p3,p4) M=3类资源(A=10,B=5,C=7)
在T0时刻资源分配如下
Max             Allocation           Need            Available
A    B    C       A     B     C       A    B     C      A    B      C
p0          7    5    3       0     1     0       7    4     3      3    3      2
p1          3    2    2       2     0     0       1    2     2
p2          9    0    2       3     0     2       6    0     0
p3          2    2    2       2     1     1       0    1     1
p4          4    3    3       0     0     2       4    3     1
*/

#include <iostream>

using namespace std;

#define N 5//进程数
#define M 3//资源数

int Available[M]={3,3,2};//可用资源向量

//最大需求矩阵
int Max
[M]={ {7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3} };

//分配矩阵
int Allocation
[M]={ {0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}  };

//需求矩阵
int Need
[M] = {  {7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}  };

//请求向量
int Request[M];

bool savety(int allocation
[M],int need
[M],int ava[M]);
int check(int a,int request[]);
void execute(int a,int request[M]);
void print();
void result(int res);
int main()
{
//安全性检测
int allocation
[M];
int need
[M];
int ava[M];
for(int i=0;i<M;i++) ava[i]=Available[i];
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
allocation[i][j]=Allocation[i][j];
need[i][j]=Need[i][j];
}
}
if(savety(allocation,need,ava))
{
cout<<"系统目前处于安全状态"<<endl<<endl;

//p1发出请求向量
Request[0]=1,Request[1]=0,Request[2]=2;
cout<<"P1发出请求向量Request(1,0,2)"<<endl;
int res=check(1,Request);
result(res);

cout<<endl<<endl;

//p4发出请求向量
Request[0]=3,Request[1]=3,Request[2]=0;
cout<<"P4发出请求向量Request(3,3,0)"<<endl;
res=check(4,Request);
result(res);

cout<<endl<<endl;

//p0发出请求向量
Request[0]=0,Request[1]=2,Request[2]=0;
cout<<"P0发出请求向量Request(0,2,0)"<<endl;
res=check(0,Request);
result(res);
}

}

//处理结果
void result(int res){
if(res==1){
cout<<"系统是安全的,同意请求"<<endl;
//执行
execute(1,Request);
//打印
print();
}
else if(res==3)
{
cout<<"分配后系统是不安全的,拒绝请求!"<<endl;
}
else if(res==2)
{
cout<<"系统资源不足以分配,请等待"<<endl;
}

}

bool savety(int allocation
[M],int need
[M],int ava[M]){
int count=0;
int count2=0;
//安全序列
int SaveList
={-1,-1,-1,-1,-1};

cout<<"       Work       Need      Allocation      Work+Allocation    Finish"<<endl;
cout<<"    A   B   C   A   B   C    A   B   C       A   B   C"<<endl;
//虚拟分配
while(count<5&&count2<5){
for(int i=0;i<N;i++){
//排除已经执行完的进程
if(SaveList[0]==i)continue;
if(SaveList[1]==i)continue;
if(SaveList[2]==i)continue;
if(SaveList[3]==i)continue;
if(SaveList[4]==i)continue;
//寻找可以执行完毕的进程
if(need[i][0]<=ava[0]&&need[i][1]<=ava[1]&&need[i][2]<=ava[2]){
SaveList[count++]=i;
//回收资源
for(int j=0;j<M;j++)
ava[j]=ava[j]+allocation[i][j];
cout<<"p"<<i<<"  "<<ava[0]<<"   "<<ava[1]<<"   "<<ava[2]<<"   ";
cout<<need[i][0]<<"   "<<need[i][1]<<"   "<<need[i][2]<<"    ";
cout<<allocation[i][0]<<"   "<<allocation[i][1]<<"   "<<allocation[i][2]<<"       ";
cout<<ava[0]+allocation[i][0]<<"   "<<ava[1]+allocation[i][1]<<"   "<<ava[2]+allocation[i][2]<<"          true"<<endl;
}
}
count2++;
}
if(count==5){
cout<<"存在安全序列:";
for(int i=0;i<N;i++)
cout<<SaveList[i]<<"   ";
cout<<endl;
return true;
}
else
{
cout<<"不存在安全序列 "<<endl;
return false;
}
}

//a为线程号,request为请求表,返回结果:1.通过 2,等待  3.不通过
int  check(int a,int request[]){
//检查是否小于需求
if(!(request[0]<=Need[a][0]&&request[1]<=Need[a][1]&&request[2]<=Need[a][2])){
return 3;
}
//检查是否小于可用资源
if(!(request[0]<=Available[0]&&request[1]<=Available[1]&&request[2]<=Available[2])){
return 2;
}
//试探分配
int ava[M];
for(int i=0;i<M;i++)ava[i]=Available[i];
int allocation
[M];
int need
[M];
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
allocation[i][j]=Allocation[i][j];
need[i][j]=Need[i][j];
}
}
//分配并修改
for(int i=0;i<M;i++){
ava[i]-=request[i];
need[a][i]-=request[i];
allocation[a][i]+=request[i];
}
//安全性检查
if(savety(allocation,need,ava))
return 1;
else
return 3;
}
//执行函数,a为修改进程号,request为请求表
void execute(int a,int request[M]){
for(int i=0;i<M;i++){
Allocation[a][i]+=request[i];
Need[a][i]-=request[i];
Available[i]-=request[i];
}
}
void print(){
cout<<"执行后:"<<endl;
cout<<"         MAX       Allocation     Need"<<endl;
cout<<"       A  B  C      A  B  C      A  B  C "<<endl;
for(int i=0;i<N;i++)
cout<<"p"<<i<<"     "<<Max[i][0]<<"  "<<Max[i][1]<<"  "<<Max[i][2]<<"      "<<Allocation[i][0]<<"  "<<Allocation[i][1]<<"  "<<Allocation[i][2]<<"      "<<Need[i][0]<<"  "<<Need[i][1]<<"  "<<Need[i][2]<<endl;
cout<<"Available:A("<<Available[0]<<")   B("<<Available[1]<<")    C("<<Available[2]<<")"<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  操作系统