银行家算法的实现

时间:2021-08-22 10:14:14
 
//zhu_jie_mian.cpp
#include<iostream.h>
#include"bank.h"
void zhu_jie_mian(list * &L,int &n)
{
	cout<<"         ++++++++++++++++++++进程资源调度管理+++++++++++++++++++++++"<<endl;
	cout<<"         +               1.显示当前进程与资源状态                  +"<<endl;
	cout<<"         +                       2.增加进程                        +"<<endl;
	cout<<"         +                       3.死锁检测                        +"<<endl;
	cout<<"         +                       4.申请资源                        +"<<endl;
	cout<<"         +                       5.撤销资源                        +"<<endl;
	cout<<"         +                       6.结束进程                        +"<<endl;
	cout<<"         +                       7.退出程序                        +"<<endl;
	cout<<"         +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
	int choice;
	cin>>choice;
	switch(choice)
	{
		case 1:xianshi(L,n);break;
		case 2:zengjia(L,n);break;
		case 3:jiance(L,n);break;
		case 4:shenqing(L,n);break;
		case 5:chexiao(L,n);break;
		case 6:jieshu(L,n);break;
		case 7:break;
	}
}

//zengjia.cpp
#include<iostream.h>
#include<malloc.h>
#include"bank.h"
void zengjia(list * &L,int &n)
{
	list *t,*s;
	int choice;
	s=L;
	while(s->next!=NULL)
		s=s->next;
	t=(list *)malloc(sizeof(list));
	cout<<"请输入此进程的号码:"<<endl;
	cin>>t->haoma;
	cout<<"请分别输入此进程申请的A、B、C资源数:"<<endl;
	cin>>t->data.claim[0]>>t->data.claim[1]>>t->data.claim[2];
	cout<<"请分别输入此进程已占有的A、B、C资源数:"<<endl;
	cin>>t->data.allocation[0]>>t->data.allocation[1]>>t->data.allocation[2];
	cout<<"请分别输入此进程仍需要的A、B、C资源是:"<<endl;
	cin>>t->data.need[0]>>t->data.need[1]>>t->data.need[2];
	t->state=0;
	n++;
	t->next=s->next;
	s->next=t;
	cout<<"         ======================1.继续增加进程======================="<<endl;
	cout<<"         =======================2.返回主界面========================"<<endl;
	cin>>choice;
	switch(choice)
	{
		case 1:zengjia(L,n);break;
		case 2:zhu_jie_mian(L,n);break;
	}
}

//xianshi.cpp
#include<iostream.h>
#include"bank.h"
void xianshi(list * &L,int &n)
{
	list *t;
	int choice;
	int A_temp=0,B_temp=0,C_temp=0;
	t=L->next;
	cout<<"                 Claim      Allocation     Need       Available"<<endl;
	cout<<"         号码   A  B  C      A  B  C      A  B  C      A  B  C"<<endl;
	while(t!=NULL)
	{
		cout<<"           "<<t->haoma<<"    "<<t->data.claim[0]<<"  "<<t->data.claim[1]<<"  "<<t->data.claim[2]<<"      "<<t->data.allocation[0]<<"  "<<t->data.allocation[1]<<"  "<<t->data.allocation[2]<<"      "<<t->data.need[0]<<"  "<<t->data.need[1]<<"  "<<t->data.need[2];
		A_temp+=t->data.allocation[0];
		B_temp+=t->data.allocation[1];
		C_temp+=t->data.allocation[2];
		if(t->next==NULL)
			cout<<"      "<<A_total-A_temp<<"  "<<B_total-B_temp<<"  "<<C_total-C_temp<<endl;
		else
			cout<<endl;
		t=t->next;
	}
	cout<<"         =======================1.返回主界面========================"<<endl;
	cout<<"         ========================2.结束程序========================="<<endl;
	cin>>choice;
	switch(choice)
	{
		case 1:zhu_jie_mian(L,n);break;
		case 2:break;
	}
}

//shenqing.cpp
#include"bank.h"
#include<iostream.h>
void shenqing(list * &L,int &n)
{
	int choice,i,request[3],A_temp=0,B_temp=0,C_temp=0,available[3];
	list *t;
	t=L->next;
	cout<<"请输入想要申请资源的进程号:"<<endl;
	cin>>i;
	while(t!=NULL)
	{
		A_temp+=t->data.allocation[0];
		B_temp+=t->data.allocation[1];
		C_temp+=t->data.allocation[2];
		t=t->next;
	}
	available[0]=A_total-A_temp;
	available[1]=B_total-B_temp;
	available[2]=C_total-C_temp;
	t=L->next;
	while(t->haoma!=i)
		t=t->next;
	cout<<"请分别输入此进程申请A、B、C资源的资源数(提示:必须小于可用资源数和最大需求数,否则系统会重新输入!):"<<endl;
	cin>>request[0]>>request[1]>>request[2];
	while(request[0]>available[0]||request[1]>available[1]||request[2]>available[2]||request[0]>t->data.need[0]||request[1]>t->data.need[1]||request[2]>t->data.need[2])
	{
		cout<<"您输入的数据不符合要求,请重新输入:"<<endl;
		cin>>request[0]>>request[1]>>request[2];
	}
	t=L->next;
	while(t!=NULL)
	{
		if(t->haoma==i)
		{
			t->data.allocation[0]+=request[0];
			t->data.allocation[1]+=request[1];
			t->data.allocation[2]+=request[2];
			t->data.need[0]-=request[0];
			t->data.need[1]-=request[1];
			t->data.need[2]-=request[2];
		}
		t=t->next;
	}
	cout<<"         =======================1.返回主界面========================"<<endl;
	cout<<"         ========================2.检测死锁========================="<<endl;
	cin>>choice;
	switch(choice)
	{
		case 1:zhu_jie_mian(L,n);break;
		case 2:jiance(L,n);break;
	}
}

//jieshu.cpp
#include"bank.h"
#include<iostream.h>
#include<malloc.h>
void jieshu(list * &L,int &n)
{
	int i,choice;
	list *t,*s;
	t=L;
	s=t->next;
	cout<<"请输入您想结束的进程的进程号:"<<endl;
	cin>>i;
	while(s!=NULL)
	{
		if(s->haoma==i)
		{
			t->next=s->next;
			free(s);
			s=t->next;
			n--;
		}
		else
		{
			t=t->next;
			s=s->next;
		}
	}
	cout<<"         ======================1.继续结束进程======================="<<endl;
	cout<<"         =======================2.返回主界面========================"<<endl;
	cin>>choice;
	switch(choice)
	{
		case 1:jieshu(L,n);break;
		case 2:zhu_jie_mian(L,n);break;
	}
}

//jiance.cpp
#include"bank.h"
#include<iostream.h>
void jiance(list * &L,int &n)
{
	int i,A_temp=0,B_temp=0,C_temp=0,flag[100],j=0,available[3],choice;
	list *t;
	t=L->next;
	while(t!=NULL)
	{
		A_temp+=t->data.allocation[0];
		B_temp+=t->data.allocation[1];
		C_temp+=t->data.allocation[2];
		t->state=0;
		t=t->next;
	}
	available[0]=A_total-A_temp;
	available[1]=B_total-B_temp;
	available[2]=C_total-C_temp;
	for(i=0;i<n;i++)
	{
		for(t=L->next;t!=NULL;t=t->next)
		{
			if(t->state==1)
				continue;
			else if((t->data.need[0]<=available[0])&&(t->data.need[1]<=available[1])&&(t->data.need[2]<=available[2]))
			{
				available[0]+=t->data.allocation[0];
				available[1]+=t->data.allocation[1];
				available[2]+=t->data.allocation[2];
				flag[j++]=t->haoma;
				t->state=1;
			}
		}
	}
	if((available[0]==A_total)&&(available[1]==B_total)&&(available[2]==C_total))
	{
		cout<<"         此状态很安全,无死锁,安全序列为:";
		for(i=0;i<n;i++)
		{
			cout<<flag[i]<<"->";
		}
		cout<<endl;
	}
	else
		cout<<"         此状态存在死锁,可以采取结束进程来结束死锁状态。"<<endl;
	cout<<"         =======================1.返回主界面========================"<<endl;
	cout<<"         ========================2.结束程序========================="<<endl;
	cin>>choice;
	switch(choice)
	{
		case 1:zhu_jie_mian(L,n);break;
		case 2:break;
	}
}


//chexiao.cpp
#include"bank.h"
#include<iostream.h>
void chexiao(list * &L,int &n)
{
	int choice,i,request[3];
	list *t;
	t=L->next;
	cout<<"请输入想要撤销资源的进程号:"<<endl;
	cin>>i;
	cout<<"请分别输入此进程撤销A、B、C资源的资源数(提示:必须小于已占有资源数,否则系统会重新输入!):"<<endl;
	cin>>request[0]>>request[1]>>request[2];
	while(t->haoma!=i)
		t=t->next;
	while(request[0]>t->data.allocation[0]||request[1]>t->data.allocation[1]||request[2]>t->data.allocation[2])
	{
		cout<<"您输入的数据不符合要求,请重新输入:"<<endl;
		cin>>request[0]>>request[1]>>request[2];
	}
	t=L->next;
	while(t!=NULL)
	{
		if(t->haoma==i)
		{
			t->data.allocation[0]-=request[0];
			t->data.allocation[1]-=request[1];
			t->data.allocation[2]-=request[2];
		}
		t=t->next;
	}
	cout<<"         =======================1.返回主界面========================"<<endl;
	cout<<"         ========================2.检测死锁========================="<<endl;
	cin>>choice;
	switch(choice)
	{
		case 1:zhu_jie_mian(L,n);break;
		case 2:jiance(L,n);break;
	}
}

//bank.cpp
#include"bank.h"
#include<iostream.h>
#include<malloc.h>
int main()
{	
	list *L;
	int n=0;
	L=(list *)malloc(sizeof(list));
	L->haoma=NULL;
	L->next=NULL;
	zhu_jie_mian(L,n);
	return 0;
}

//bank.h
#define A_total 10
#define B_total 5
#define C_total 7
typedef struct 
{
	int claim[3];
	int allocation[3];
	int need[3];
}jincheng;
typedef struct node
{
	int haoma;
	jincheng data;
	int state;
	struct node *next;
}list;
void zhu_jie_mian(list *&,int &);
void xianshi(list * &,int &);
void zengjia(list * &,int &);
void jiance(list * &,int &);
void shenqing(list * &,int &);
void chexiao(list * &,int &);
void jieshu(list * &,int &);