#include <stdio.h>
#include <iostream>
using namespace std;
#define MAXJINCHENG 10
#define MAXZIYUAN 50
int Available[MAXZIYUAN];//每类资源可用数目
int Max[MAXJINCHENG][MAXZIYUAN];//每个进程的最大需求
int Allocation[MAXJINCHENG][MAXZIYUAN];//最大资源分配矩阵
int Need[MAXJINCHENG][MAXZIYUAN];//最大需求矩阵
int Work[MAXZIYUAN];//work
int WorkAddAllocation[MAXJINCHENG][MAXZIYUAN];//work+allocation
int Request[MAXZIYUAN];//资源请求
int Finish[MAXJINCHENG];//安全数组
int P[MAXJINCHENG];
int JC;//当前进程数
int ZY;//当前资源数
void init()
{
cout<<"请输入进程数目:";
cin>>JC;
cout<<"请输入资源数目:";
cin>>ZY;
cout<<"请输入资源分配矩阵Allocation:"<<endl;
for(int i=0;i<JC;i++)
{
for(int j=0;j<ZY;j++)
{
cin>>Allocation[i][j];
}
}
cout<<"请输入需求矩阵Max:"<<endl;
for(int i=0;i<JC;i++)
{
for(int j=0;j<ZY;j++)
{
cin>>Max[i][j];
}
}
cout<<"请输入各类资源现有数目:"<<endl;
for(int i=0;i<ZY;i++)
{
cin>>Available[i];
}
for(int i=0;i<JC;i++)
{
for(int j=0;j<ZY;j++)
{
Need[i][j]=Max[i][j]-Allocation[i][j]; //计算Need
}
}
bool flag=true;
cout<<"-----------------------------------------------------------------"<<endl;
cout<<" Allocation Max Need Available "<<endl;
for(int i=0;i<JC;i++)
{
cout<<" "<<"P"<<i+1<<" ";
for(int j=0;j<ZY;j++)
{
cout<<Allocation[i][j]<<" ";
}
cout<<" ";
for(int j=0;j<ZY;j++)
{
cout<<Max[i][j]<<" ";
}
cout<<" ";
for(int j=0;j<ZY;j++)
{
cout<<Need[i][j]<<" ";
}
cout<<" ";
if(flag)
{
for(int i=0;i<ZY;i++)
{
cout<<Available[i]<<" ";
flag=false;
}
}
cout<<" "<<endl;
}
cout<<"-----------------------------------------------------------------"<<endl;
}
void Show()
{
bool flag=true;
cout<<"-----------------------------------------------------------------"<<endl;
cout<<" Allocation Max Need Available "<<endl;
for(int i=0;i<JC;i++)
{
cout<<" "<<"P"<<i+1<<" ";
for(int j=0;j<ZY;j++)
{
cout<<Allocation[i][j]<<" ";
}
cout<<" ";
for(int j=0;j<ZY;j++)
{
cout<<Max[i][j]<<" ";
}
cout<<" ";
for(int j=0;j<ZY;j++)
{
cout<<Need[i][j]<<" ";
}
cout<<" ";
if(flag)
{
for(int i=0;i<ZY;i++)
{
cout<<Available[i]<<" ";
flag=false;
}
}
cout<<" "<<endl;
}
cout<<"-----------------------------------------------------------------"<<endl;
}
bool Safe()
{
int i, j, k, l = 0;
for (i = 0; i < ZY; i++)
{
Work[i] = Available[i];
}
for (i = 0; i < JC; i++)
{
Finish[i] = 0; //FINISH记录每个进程是否安全
}
while (l < JC) //正常的话,共执行m次
{
int init_index = l;
for (i = 0; i < JC; i++)
{
if (Finish[i] == 1)
{
continue;
}
for (j = 0; j < ZY ; j++)
{
if (Need[i][j] > Work[j])
{
break;
}
}
if (j == ZY )
{
Finish[i] = 1;
for (k = 0; k < ZY ; k++)
{
Work[k] += Allocation[i][k];
}
P[l++] = i;//记录进程号
}
else
continue;
}
if (l==init_index)
{
cout << "系统是不安全的" << endl;
return false;
}
}
cout << "系统是安全的" << endl;
cout << "安全序列:" << endl;
for (i = 0; i < l; i++)
{
cout << P[i];
if (i != l - 1)
{
cout << "-->";
}
}
cout << endl;
return true;
}
void Bank()
{
int count=0;//记录Need的0的个数
int p;//进程号
int again;//继续or结束
while(1)
{
cout<<"请输入需要申请资源的进程号"<<endl;
cin>>p ;
if(p>JC)
{
cout<<"error!"<<endl;
continue;
}
cout<<"请输入需要申请各资源的数目"<<endl;
for(int i=0;i<ZY;i++)
{
cin>>Request[i];
}
for(int i=0;i<ZY;i++)
{
if(Request[i]>Need[p][i])
{
cout<<"所需要资源已超出他宣布的最大值!"<<endl;
continue;
}
if(Request[i]>Available[i])
{
cout<<"没有足够资源!"<<endl;
continue;
}
}
for(int i=0;i<ZY;i++)
{
Available[i]-=Request[i]; //可用资源-请求资源
Allocation[p][i]+=Request[i];//被分配资源+申请的资源
Need[p][i]-=Request[i];//线程还需要的资源-申请的资源
}
if(Safe())//判断申请后系统是否安全,不安全则将已分配资源还回系统;
{
cout<<"分配资源成功!"<<endl;
for(int j=0;j<ZY;j++)
{
if(Need[p][j]==0)
{
count++;
}
}
if(count==ZY)
{
for(int i=0;i<ZY;i++)
{
Available[i]+=Max[p][i];
}
}
Show();
}
else
{
cout<<"分配资源失败!"<<endl;
for(int i=0;i<ZY;i++)
{
Available[i]+=Request[i]; //可用资源+请求资源
Allocation[p][i]-=Request[i];//被分配资源-申请的资源
Need[p][i]+=Request[i];//线程还需要的资源+申请的资源
}
Show();
}
cout<<"是否继续分配?继续输入“1”,退出输入“0”"<<endl;
cin>>again;
if(again==0)
{
break;
}
}
}
int main()
{
init();
Safe();
Bank();
}
07-08
1万+