www.pudn.com > banker00.rar.rar > main.cpp


#include "iostream.h" 
#include "string.h" 
#include "conio.h" 
#include "stdio.h" 
int const M = 3; 
int const N = 5; 
//程序所用到的存储结构如下: 
int Available[M] = {3,3,2}; 
int Max[N][M] = {{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}}; 
int Allocation[N][M] = {{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}}; 
int Need[N][M] = {{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}}; 
 
//所用函数声明如下: 
bool CheckSecurity();     //安全性检测 
bool CheckFinish(bool *End,int size);         //检测Finish数组是否全为true 
void InitFinish(bool *Finish,int size);       //初始化Finish数组 
bool Compare(int *first,int *last,int size);   //数组比较函数 
void OutSafeQueue(int *queue,int size);         //输出安全序列 
void Allocate(int *Request,int size,int index);  //尝试分配资源 
void Recycle(int *Request,int size,int index);   //回收已分配的资源 
void OutResource();                               //输出资源表 
void Banker_Request(int *Request,int size,int index);  //资源请求函数 
void Test();                                         //测试函数 
void Init_Work(int *work,int size);				     //复制Available到Work数组 
void Assign_Work(int *work,int size,int index);			//重新修改Work数组 
 
int main() 
{  
	OutResource(); 
/*	int Request0[M] = {0,2,0}; 
//	int Request0[M] = {0,1,0}; 
	int Request1[M] = {1,0,2}; 
	int Request4[M]={3,3,0}; 
	cout << "\t***************模拟银行家算法****************\n"; 
	 
	cout << "Press any key to start...\n" << endl; 
	getch(); 
	cout << "进程1正在申请资源...\nPress any key to see the result...\n" << endl; 
	getch(); 
	Banker_Request(Request1,M,1); 
	cout << "Press any key to continue...\n"<< endl; 
	getch(); 
	cout << "进程4正在申请资源...\nPress any key to see the result...\n" << endl; 
	Banker_Request(Request4,M,4); 
	cout << "Press any key to continue...\n"<< endl; 
	getch(); 
	cout << "进程0正在申请资源...\nPress any key to see the result...\n" << endl; 
	getch(); 
	Banker_Request(Request0,M,0);*/ 
	int Request[M]; 
	int index; 
	char ch; 
	char nameofResource[] = {'A','B','C'}; 
	do 
	{ 
		cout << "请输入申请资源的进程编号:"; 
		cin >> index; 
		cout << "-----请输入要申请的资源数----\n" << endl; 
		for(int i = 0;i < M;i++) 
		{ 
			cout << "资源" << nameofResource[i] <<':'; 
			cin >> Request[i]; 
		} 
		Banker_Request(Request,M,index); 
		cout << "\n如想继续申请其他资源请输入'y',否则输入'n'" << endl; 
		cin >> ch; 
		if(ch == 'n') 
		{ 
			cout << "程序运行结束!" << endl; 
			break; 
		} 
	}while(ch == 'y'); 
	return 0; 
} 
 
bool CheckSecurity() 
{ 
	int Work[M]; 
	bool Finish[N]; 
	int queue[N]; 
	int i,k = 0; 
	InitFinish(Finish,N); 
	Init_Work(Work,M); 
	int flag = 0; 
	while(CheckFinish(Finish,N) == false) 
	{ 
		for(i = 0;i < N;i++) 
		{ 
			if(Finish[i] == false && Compare(Need[i],Work,sizeof(Work)/sizeof(int))) 
			{ 
				Assign_Work(Work,M,i); 
				Finish[i] = true; 
				queue[k++] = i; 
				flag = 1; 
				break; 
			} 
				 
		} 
		if(flag == 0) 
			return false; 
		flag = 0; 
	} 
	if(CheckFinish(Finish,N)) 
		{ 
			cout << "存在一个安全序列:" << endl; 
			OutSafeQueue(queue,k); 
		} 
	else  
		cout << "不存在一个安全序列!" << endl; 
	return true; 
} 
bool CheckFinish(bool *End,int size) 
{ 
	int n = size; 
	for(int i = 0;i < n;i++) 
	{ 
		if(End[i] == false) 
			break; 
	} 
	if(i < n) 
		return false; 
	else return true; 
} 
void InitFinish(bool *Finish,int size) 
{ 
	int n = size; 
	int i = 0; 
	for(i = 0;i < n;i++) 
	{ 
		Finish[i] = false; 
	} 
} 
bool Compare(int *first,int *last,int size) 
{ 
	int n = size; 
	for(int i = 0;i < n;i++) 
	{ 
		if(first[i] > last[i]) 
			break; 
	} 
	if(i < n) 
		return false; 
	else  
		return true; 
} 
void OutSafeQueue(int *queue,int size) 
{ 
	int n = size; 
	cout << "\t"; 
	cout << '{'; 
	for(int i = 0;i < n;i++) 
	{ 
		cout << "P" << queue[i] ; 
		if(i != n-1) 
		{ 
			cout << ", "; 
		} 
	} 
	cout << '}'; 
	cout << endl; 
} 
void Allocate(int *Request,int size,int index) 
{ 
	int k = index; 
	int n = size; 
	int *Allocate,*need; 
	Allocate = Allocation[k]; 
	need = Need[k]; 
	for(int i = 0;i < n;i++) 
	{ 
		Available[i] -= Request[i]; 
		Allocate[i] += Request[i]; 
		need[i] -= Request[i]; 
	} 
} 
void Recycle(int *Request,int size,int index) 
{ 
	int n = size; 
	int k = index; 
	int *Allocate,*need; 
	Allocate = Allocation[k]; 
	need = Need[k]; 
	for(int i = 0;i < n;i++) 
	{ 
		Available[i] += Request[i]; 
		Allocate[i] -= Request[i]; 
		need[i] += Request[i]; 
	} 
} 
void Banker_Request(int *Request,int size,int index) 
{ 
	int n = size; 
	int k = index; 
	if(Compare(Request,Need[k],n)) 
		if(Compare(Request,Available,n)) 
		{ 
			Allocate(Request,n,k); 
		} 
		else 
		{ 
			cout << "目前尚无足够资源!进程需等待。" << endl; 
			return; 
		} 
	else 
	{ 
		cout << "申请的资源数已超过它所宣布的最大值!" << endl; 
		return; 
	} 
	if(!CheckSecurity()) 
	{ 
		Recycle(Request,n,k); 
		cout << "此时刻不存在一个安全序列"<< endl; 
	} 
} 
void Test() 
{ 
	for(int i = 0;i < N;i++) 
		for(int j = 0;j < M;j++) 
		{ 
			cout << Allocation[i][j] << ' '; 
		} 
	cout << endl; 
	for(i = 0;i < N;i++) 
		for(int j = 0;j < M;j++) 
			cout << Need[i][j] << ' '; 
	cout << endl; 
	for(i = 0;i < M;i++) 
    	cout << Available[i] << " "; 
	cout << endl; 
} 
void Init_Work(int *work,int size) 
{ 
	int n = size; 
	for(int i = 0;i < n;i++) 
	{ 
		work[i] = Available[i]; 
	} 
} 
void Assign_Work(int *work,int size,int index) 
{ 
	int i = index; 
	int n = size; 
	for(int j = 0;j < n;j++) 
	{ 
		work[j] = work[j] + Allocation[i][j]; 
	} 
} 
void OutResource() 
{ 
	cout << "此时刻资源情况如下:" << endl; 
	cout << "\t" << "MAX" << "\t " << "ALLOCATION" << "\t " << "NEED"  
		<< "\t " << "AVAILABLE" << endl; 
	 
	for(int i = 0;i < N;i++) 
	{    
		cout << "\t"; 
		for(int j = 0;j < M;j++) 
		{ 
			cout << Max[i][j] << " "; 
		} 
		cout << "\t "; 
		for(j = 0;j < M;j++) 
		{ 
			cout << Allocation[i][j] << " "; 
		} 
		cout << "\t\t "; 
		for(j = 0;j < M;j++) 
		{ 
			cout << Need[i][j] << " "; 
		} 
		if(i == 0)  
		{ 
			cout << "\t "; 
			for(int k = 0;k < M;k++) 
				cout << Available[k] << " "; 
		} 
		cout << endl; 
	} 
}