银行家问题实验

#include <iostream>
#include <string.h>
#include<stdio.h>
using namespace std;
#define M 3   //资源的种类数 
#define N 5   //进程的个数 
void output(int iMax[N][M], int iAllocation[N][M], int iNeed[N][M], int iAvailable[M], char cName[N]);  //统一的输出格式 
bool safety(int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N]); 
bool banker(int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N]); 
void main()
{
	int i, j;   //当前可用每类资源的资源数  
	int iAvailable[M]={3,3,2};   //系统中N个进程中的每一个进程对M类资源的最大需求 
	int iMax[N][M]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}}; 
	int iNeed[N][M],iAllocation[N][M]={{0,1,1},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
	//进程名   
	char cName[N]={'a','b','c','d','e'}; 
	bool bExitFlag=true;  
	//退出标记  
	char ch;      //接收选择是否继续提出申请时传进来的值 
	bool bSafe;   //存放安全与否的标志  //计算iNeed[N][M]的值  
	for(i=0;i<N;i++)   
	for(j=0;j<M;j++)   
		iNeed[i][j]=iMax[i][j]-iAllocation[i][j];  //输出初始值  
	output(iMax,iAllocation,iNeed,iAvailable,cName);  //判断当前状态是否安全  
	bSafe=safety(iAllocation,iNeed,iAvailable,cName);  //是否继续提出申请  
	while(bExitFlag)  
	{   
		cout<<"\n"<<"继续提出申请?\ny为是;n为否。\n";
		cin>>ch;   
		switch(ch)  
		{         
		case 'y': 
			bSafe = banker(iAllocation, iNeed, iAvailable, cName);  
			if (bSafe)   //安全,则输出变化后的数据  
				output(iMax,iAllocation,iNeed,iAvailable,cName);  
			break;        
		case 'n':    
			cout<<"退出。\n";   
			bExitFlag=false;   
			break;      
		default:          
			cout<<"输入有误,请重新输入:\n"; 
		} 
	} 
}
void output(int iMax[N][M], int iAllocation[N][M], int iNeed[N][M], int iAvailable[M], char cName[N])
{
	int i, j; 
	cout << "\n\t   Max  \tAllocation\t  Need  \t Available" << endl;  
	cout << "\tA   B   C\tA   B   C\tA   B   C\t A   B   C" << endl; 
	for (i = 0; i<N; i++) 
	{
		cout << cName[i] << "\t"; 
		for (j = 0; j<M; j++)   
			cout << iMax[i][j] << "   ";  
		cout << "\t";  
		for (j = 0; j<M; j++)  
			cout << iAllocation[i][j] << "   "; 
		cout << "\t";   for (j = 0; j<M; j++) 
			cout << iNeed[i][j] << "   "; 
		cout << "\t";   cout << " ";   //Available只需要输出一次  
		if (i==0)  
		for(j=0;j<M;j++)   
			cout<<iAvailable[j]<<"   ";   
		cout<<endl; 
	}  
}
bool safety(int iAllocation[N][M], int iNeed[N][M], int iAvailable[M], char cName[N])
{
	int i, j, flag, x = 0; 
	char num[5];  
	int Work[M];  
	bool Finish[N];  
	//定义基本变量 
	for(j=0;j<3;j++) 
		Work[j]=iAvailable[j];  
	//将iAvailable的值赋给Work  
	for(i=0;i<5;i++)           //将Finish全部置为False 
		Finish[i]=false;  
	while(true)       //执行无限循环,满足条件时跳出  
	{   
		flag=0;    //每次循环开始时将记录本次循环中是否有使有满足条件iAllocation的标志置为0,若为0表示不存在,若不为0表示存在  
		for(i=0;i<5;i++)   //执行循环,看有没有满足条件的iAllocation  
		{    
			if(Finish[i]==false&&Work[0]>=iNeed[i][0]&&Work[1]>=iNeed[i][1]&&Work[2]>=iNeed[i][2])  
			{   
				for(j=0;j<3;j++) 
				{  
					Work[j]+= iAllocation[i][j] ;   
					  }      
				Finish[i]=true;            
				//将 Finish置true    
				flag++;       //标志加1     
				num[x++]=cName[i];     
				//将该序列名赋给数组num[]   
			}   
		}   
		if(flag==0)  
		{  
			cout<<"无安全序列"; //标志为0,证明已无满足条件iAllocation ,退出循环,返回false    
			return false;
		}  
		if (Finish[0] == true && Finish[1] == true && Finish[2] == true && Finish[3] == true && Finish[4] == true)
		{ 
			cout << "\n"; 
			cout << "安全序列为:"; 
			for (x = 0; x<5; x++)    
				cout << num[x] << " "; 
			cout << "\n";   
			return true; 
		}
	}   return true;
}
bool banker(int iAllocation[N][M], int iNeed[N][M], int iAvailable[M], char cName[N]) 
{
	int iMax[N][M] = { { 7, 5, 3 }, { 3, 2, 2 }, { 9, 0, 2 }, { 2, 2, 2 }, { 4, 3, 3 } }; 
	int t, i, Request[3], check_1[3];  
	char x;      //定义变量       
	cout<<"请输入进程名:";       
	//输入进程名  
	cin>>x;  
	if(x=='a')i=0;   
	if(x=='b')i=1;    
	if(x=='c')i=2;   
	if(x=='d')i=3;  
	if(x=='e')i=4;    
	cout << "请输入各资源数量:";  //输入变量名  
	for(t=0;t<3;t++) 
		cin>>Request[t];
	for(t=0;t<3;t++)        //检查数值   
	{   
		check_1[t]=Request[t]+iAllocation[i][t];  
	}  
	for(t=0;t<3;t++) 
	{   
		if((iMax[i][t]-check_1[t])<0) 
		{    
			cout<<"\n资源申请超过最大需求量!!!\n";  
		return false;  
		}  
	}   
	for(t=0;t<3;t++) 
	{
		if ((iAvailable[t] - Request[t])<0)  
		{ cout << "\n不能满足进程!!!\n";  
		return false;
		}
	}    
	for (t = 0; t<3; t++)  //将该变量的 iAvailable、iAllocation、iNeed重新赋值 
	{     
		iAvailable[t]-=Request[t]; 
		iAllocation[i][t]+=Request[t];  
		iNeed[i][t]-=Request[t];  
	}   
	safety(iAllocation,iNeed,iAvailable,cName); 
	//执行安全数列算法  
	return true; 
	return true; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值