⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 source.cpp

📁 一个很好用的银行家算法
💻 CPP
字号:
#include"iostream.h"
#define M 5
typedef struct{

    int n_a;
    int n_b;
    int n_c;
    int n_d;
}Need;
typedef struct{
	int l_a;
    int l_b;
    int l_c;
	int l_d;
}Allocation;
typedef struct{
	int v_a;
	int v_b;
	int v_c;
	int v_d;
}Available;
 typedef struct{
	int r_a;
	int r_b;
	int r_c;
	int r_d;
}Request;
typedef struct{
	Need N;
	Allocation L;
	bool Finish;
}Process;
void Init(Process P[M],Available &V){                    //构造函数
	 for(int i=0;i<M;i++)
	   {
		cout<<"请输入第"<<i<<"进程当前情况,先为Need,后为ALLOCATION:"<<endl;
		cin>>P[i].N.n_a>>P[i].N.n_b>>P[i].N.n_c>>P[i].N.n_d;
		cin>>P[i].L.l_a>>P[i].L.l_b>>P[i].L.l_c>>P[i].L.l_d;
		P[i].Finish=false;
	   }
       cout<<"请输入(a-d)各资源可用数,即Available:"<<endl;
       cin>>V.v_a>>V.v_b>>V.v_c>>V.v_d;
}
bool Subsecurity(Process P[M],Available V){          //子安全性分析
     int i=0;
	 int flag=0;
	 int tap=0;
	 while(i<M){
		if(P[i].Finish==false&&P[i].N.n_a>V.v_a&&P[i].N.n_b>V.v_b&&P[i].N.n_c>V.v_c&&P[i].N.n_d>V.v_d){
		   flag++;
		   i++;
		 }
		 else 
		    i++;
	 }
	 for(i=0;i<M;i++)
		 if(P[i].Finish==false)
			tap++;
	 if(flag==tap)
		 return true;
	 else 
		 return false;
}
bool Security(Process P[M],Available V){               //安全性分析
	  int sign=0;
	  int T=0;
      while(sign!=M){
         for(int i=0;i<M;i++){
            //cout<<V.v_a<<V.v_b<<V.v_c<<V.v_d;
        	//cout<<P[T].N.n_a<<P[T].N.n_b<<P[T].N.n_c<<P[T].N.n_d;
            if(P[i].Finish==false&&P[i].N.n_a<=V.v_a&&P[i].N.n_b<=V.v_b&&P[i].N.n_c<=V.v_c&&P[i].N.n_d<=V.v_d){
			   //cout<<V.v_a<<V.v_b<<V.v_c<<V.v_d;
			   V.v_a=V.v_a+P[i].L.l_a;
			   V.v_b=V.v_b+P[i].L.l_b;
			   V.v_c=V.v_c+P[i].L.l_c;
			   V.v_d=V.v_d+P[i].L.l_d;
			   P[i].Finish=true;
		       sign++;
			 }
		  }
	   if(sign==M) return true;
		   else if(Subsecurity(P,V))
			       return false;
	}
}
void Banker(int T,Process P[M],Available &V,Request R){        //银行家算法
	   for(int i=0;i<M;i++)
          P[i].Finish==false;
	   if(R.r_a<=V.v_a&&R.r_b<=V.v_b&&R.r_c<=V.v_c&&R.r_d<=V.v_d)
	   {
		  V.v_a-=R.r_a;
		  V.v_b-=R.r_b;
		  V.v_c-=R.r_c;
		  V.v_d-=R.r_d;
		  P[T].L.l_a+=R.r_a;
		  P[T].L.l_b+=R.r_b;
		  P[T].L.l_c+=R.r_c;
		  P[T].L.l_d+=R.r_d;
		  P[T].N.n_a-=R.r_a;
		  P[T].N.n_b-=R.r_b;
		  P[T].N.n_c-=R.r_c;
		  P[T].N.n_d-=R.r_d;
          //cout<<V.v_a<<V.v_b<<V.v_c<<V.v_d;
		  //cout<<P[T].N.n_a<<P[T].N.n_b<<P[T].N.n_c<<P[T].N.n_d;
          if(Security(P,V))
             cout<<"可以满足该进程请求,分配资源!"<<endl;
		  else{
             V.v_a+=R.r_a;
		     V.v_b+=R.r_b;
	     	 V.v_c+=R.r_c;
	    	 V.v_d+=R.r_d;
	    	 P[T].L.l_a-=R.r_a;
		     P[T].L.l_b-=R.r_b;
		     P[T].L.l_c-=R.r_c;
		     P[T].L.l_d-=R.r_d;
		     P[T].N.n_a+=R.r_a;
		     P[T].N.n_b+=R.r_b;
		     P[T].N.n_c+=R.r_c;
		     P[T].N.n_d+=R.r_d;
			 cout<<"对不起,不能分配给该进程资源!"<<endl;
		   }
      }
}
void main(){
    Process p[M];
	Available s;
	Request r;
	int t;
	Init(p,s);
	/*if(Security(p,s))
		cout<<"该状态为安全状态!"<<endl;
	else
		cout<<"该状态为非安全状态!"<<endl;*/
	cout<<"请输入哪个进程要请求多少资源?"<<endl;
	cin>>t>>r.r_a>>r.r_b>>r.r_c>>r.r_d;
    Banker(t,p,s,r);
}

⌨️ 快捷键说明

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