资源分配管理银行家算法

#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();
}
 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值