bank.cpp

来自「银行家算法.解决死锁问题」· C++ 代码 · 共 230 行

CPP
230
字号
#include "iostream.h"
#include "stdlib.h"
#include "stdio.h"

#define theSize 5
#define theKind 3
#define theValue 15

class banker
{
public:
	int* re;

	void newbanker(int size)
	{
		int i;
		re = new int[size];
		for (i=0; i<size; i++) re[i] = 0;
	}
};


banker max[theSize], allocation[theSize], need[theSize], work[theSize], available;
bool finish[theSize];
int order[theSize], count;

void allnew()
{
	int i;
	for (i=0; i<theSize; i++)
	{
		max[i].newbanker(theKind);
		allocation[i].newbanker(theKind);
		need[i].newbanker(theKind);
		work[i].newbanker(theKind);
	}
	available.newbanker(theKind);
}

void init()
{
	int i,k;
	for (i=0; i<theSize; i++)
	{
		for (k=0; k<theKind; k++)
		{
			max[i].re[k]=rand()%theValue;
			if (max[i].re[k]) allocation[i].re[k]=rand()%max[i].re[k];
				else allocation[i].re[k]=0;
			need[i].re[k]=max[i].re[k]-allocation[i].re[k];
		}
	}
	for (k=0; k<theKind; k++)
		available.re[k]=rand()%theValue/2;
}

void print()
{
	int i,k;
	cout<<"P	Max		Allocation	Need		Available"<<endl;
	for (i=0; i<theSize; i++)
	{
		cout<<"P"<<i<<"	";
		for (k=0; k<theKind; k++)
		{cout.width(2); cout<<max[i].re[k]<<" ";}
		cout <<"	";
		for (k=0; k<theKind; k++)
		{cout.width(2); cout<<allocation[i].re[k]<<" ";}
		cout <<"	";
		for (k=0; k<theKind; k++)
		{cout.width(2); cout<<need[i].re[k]<<" ";}
		if (i==0) {cout <<"	"; for (k=0; k<theKind; k++) cout<<available.re[k]<<" ";}
		cout <<endl;
	}
	cout<<endl;
}


void println()
{
	int i,k,j;
	cout<<"P	Work		Allocation	Need		Finish"<<endl;
	for (j=0; j<theSize; j++)
	{
		i=order[j];
		cout<<"P"<<i<<"	";
		for (k=0; k<theKind; k++)
		{cout.width(2); cout<<work[i].re[k]<<" ";}
		cout <<"	";
		for (k=0; k<theKind; k++)
		{cout.width(2); cout<<allocation[i].re[k]<<" ";}
		cout <<"	";
		for (k=0; k<theKind; k++)
		{cout.width(2); cout<<need[i].re[k]<<" ";}
		cout <<"	";
		if (finish[i]) cout<<"true	"; else cout<<"false  ";
		cout <<endl;
	}
}


bool ok()
{
	int i;
	for(i=0;i<theSize;i++)
		if(!finish[i]) return true;
	return false;
}


bool safe()
{
	int i,k; bool isdo, isnext;
	count=0;

	for(i=0; i<theSize; i++)
	{
		finish[i]=false;
		for(k=0; k<theKind; k++)
			work[i].re[k]=0;
	}

	while(isnext && ok())
	{
		isnext=false;
		for(i=0; i<theSize; i++)
		{
			isdo=true;
			if (!finish[i])
			{
			  for(k=0; k<theKind; k++)
				if(need[i].re[k]>available.re[k])
				{
					isdo=false;
					break;
				}
			}
			else isdo=false;

			if (isdo)
				{
				  for (k=0; k<theKind; k++)
				  {
					  work[i].re[k]=available.re[k];
					  available.re[k]=available.re[k]+allocation[i].re[k];					  					  
				  }
					finish[i]=true;
					isnext=true;
					order[count++]=i;
				}
		}
	}
	if (count<theSize)
	{
		for (i=0; i<theSize; i++)
			if (!finish[i]) order[count++]=i;
		return false;
	}
	return true;
}

bool request(banker re, int p)
{
	bool check=true;
	int x;
	for (x=0; x<theKind; x++)
	  if ((need[p].re[x]<re.re[x] || available.re[x]<re.re[x]) && re.re[x]>0)
		{
			check=false;
			break;
		}
	if (check)
	{
		for (x=0; x<=theKind; x++)
		{
			available.re[x]-=re.re[x];
			allocation[p].re[x]+=re.re[x];
			need[p].re[x]-=re.re[x];
		}
		return true;
	}
	return false;
}

void main()
{
int x,p;
banker temp;
char ch='y';

	temp.newbanker(theKind);
	allnew();

while (ch=='Y' || ch=='y')
{
	do init(); while(!safe());
	system("cls");
	print();

	do
	{
		cout<<"输入提出请求的进程号:"<<endl; cin>>p;
		if (p<0 || p>=theSize) cout<<"进程号输入错误!"<<endl;
	} while(p<0 || p>=theSize);

	cout<<"输入请求资源(共 "<<theKind<<" 种):"<<endl;
	for (x=0; x<theKind; x++) cin>>temp.re[x];
	if (request(temp,p))
	{
		print();
		safe();
		println();
	}
		else cout<<"资源请求错误!"<<endl;

	do
	{
		cout<<"继续(Y/N)?";
		cin>>ch;
	}
	while (ch!='Y' && ch!='y' && ch!='N' && ch!='n');
}
}






⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?