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

📄 3.txt

📁 模拟实现银行家算法
💻 TXT
字号:
#include<iostream>
#include<conio.h>
using namespace std;
int Allocation[5][3];
int Need[5][3];
int Max[5][3];
int Available[3];
bool Finish[5]={false,false,false,false,false};
int Work[3];
int bak[3];
int workbak[3];
char name[5][10]={{"P1"},{"P2"},{"P3"},{"P4"},{"P5"}};
int Request[3];
void anquan(int );//判断给某个进程请求的资源后系统是否存在安全序列
void GetAll();        //得到各个进程的Max和Allocation
bool anquan1();   //判断当前系统是否存在安全序列
void main()
{
	cout<<"*********************银行家问题**********************"<<endl;
for(;;)
{   
   	GetAll();                //取的数据之后判断系统是否存在安全序列
	for(int t=0;t<3;t++)       //若没有就要求重新输入数据
	workbak[t]=Work[t];
	bool ok=anquan1();
	if(ok==true)
    break;
	cout<<"系统不安全,请重新输入!"<<endl;
}
 cout<<"按任意键继续。。。"<<endl;
 getch();
 for(int t=0;t<3;t++)
		Work[t]=workbak[t];
 int i;
for(;;)
{
int y=0;
cout<<"请选择:1.给进程发送请求   2.结束  ";//选择将要做的事情
cin>>y;
if(y==1)
{
cout<<"请输入要给哪个进程发送请求:";
int b;   
cin>>b;
i=b-1;
cout<<"请输入要申请资源的大小."<<endl;//取得申请资源的数据
cout<<"A:";cin>>Request[0];
cout<<"B:";cin>>Request[1];
cout<<"C:";cin>>Request[2];

int really=0;
for(int x=0;x<3;x++)
{
 if(Request[x]>Need[i][x])             //如果进程请求的资源大于它所需要
  {                                //的最大的NEED资源,则出错,要求重新输入
	  cout<<"请求的资源大于进程所需要的最大Need资源,请重输!"<<endl;
      break;
  }
  really++;
   }
for (int x=0;x<3;x++)
{
 if(Request[x]>Available[x])
    {                          //如果系统没有足够的资源给请求的进程,则让进程等待
	cout<<"没有资源给进程,让进程等待,请输入新的请求!"<<endl;
    }
	really++;
  }
if(really==6)
{                            //只有满足了上面两个条件之后才进行判断
for(int x=0;x<3;x++)
{
Available[x]=Available[x]-Request[x];
Allocation[i][x]=Allocation[i][x]+Request[x];
Need[i][x]=Need[i][x]-Request[x];
Work[x]=Work[x]-Request[x];
}
anquan(i);
}
}
if(y==2)
break;
if(y!=1&&y!=2)
{
	cout<<"选择出错,请选择1或者2。。。"<<endl;
	continue;
}
cout<<"按任意键继续。。。"<<endl;
getch();
for(int t=0;t<3;t++)
		Work[t]=Available[t];
for(;;)                               //判断经过请求之后系统是否还存在安全序列
{                                       //若没有则退出   
   	for(int t=0;t<3;t++)
		workbak[t]=Work[t];
	 bool ok=anquan1();
    for(int t=0;t<3;t++)
		Work[t]=workbak[t];
	if(ok==true)
    break;
	cout<<"系统不安全,请退出!"<<endl;
}
}
getch();
}
void anquan(int i)                //断断答应进程请求后系统是否安全
{			
	int good=0;int way[5]; int q=0;
	for(int t=0;t<3;t++)
		bak[t]=Work[t];           //备份Work,用来还原进程初始数据
    for(int w=0;w<25;w++)
   {	
	   for(int x=0;x<5;x++)
         {
            if(Finish[x]==false)
              {
                int sum=0;
                for(int j=0;j<3;j++)
                   {
                     if(Need[x][j]<=Work[j])
                     sum++;
                   }
           if(sum==3)
             {
               Finish[x]=true;good++;way[q]=x;q++;
               for(int y=0;y<3;y++)
               {
				   Work[y]=Work[y]+Allocation[x][y];}
               }
             }
           }
       if(good==5)          //只有在5个进程都为ture时才说明系统是安全的
         {                           //并打印出安全序列
		  cout<<"系统是安全的,存在以下安全序列:"<<endl;
          cout<<"PCB    Work     Need     Allocation    Work+AlloCation  Finish"<<endl;
		  for(int r=0;r<5;r++)
              {
               int e=way[r];
		       cout<<name[e]<<"     ";
               for(int t=0;t<3;t++)
			   {
				   cout<<bak[t]<<" ";
			   }
			   cout<<"   ";
			   for(int t=0;t<3;t++)
               {
                cout<<Need[e][t]<<" ";
			   }
			   cout<<"      ";
                for(int t=0;t<3;t++)
                {
                 cout<<Allocation[e][t]<<" ";
                 }
				cout<<"         ";
				for(int t=0;t<3;t++)
                {
					bak[t]=bak[t]+Allocation[e][t];
                    cout<<bak[t]<<" ";
                 }
				cout<<"        ";
                cout<<"true"<<endl;
			 }
             for(int t=0;t<5;t++)    //重新还原所有进程的状态值
				 Finish[t]=false;
             return;
          }
	    }
   for(int x=0;x<3;x++)           //如果系统不安全就还原进程值
  {
	Available[x]=Available[x]+Request[x];
    Allocation[i][x]=Allocation[i][x]-Request[x];
    Need[i][x]=Need[i][x]+Request[x];
    Work[x]=Work[x]+Request[x];
    }
  cout<<"系统不安全,请求取消!!";
  for(int t=0;t<5;t++)
				 Finish[t]=false;             //重新初始化各个进程的状态值
  return;
}
bool anquan1()                               //判断当前系统是否存在安全序列
{
	int good=0;int way[5]; int q=0;
	for(int t=0;t<3;t++)
		bak[t]=Work[t];
    for(int w=0;w<25;w++)
   {
	   for(int x=0;x<5;x++)
         {
            if(Finish[x]==false)
              {
                int sum=0;
                for(int j=0;j<3;j++)
                   {
                     if(Need[x][j]<=Work[j])
                     sum++;
                   }
           if(sum==3)
             {
               Finish[x]=true;good++;way[q]=x;q++;
               for(int y=0;y<3;y++)
               {
				   Work[y]=Work[y]+Allocation[x][y];}
               }
             }
           }
       if(good==5)              //只有在5个进程都为ture时才说明系统是安全的
         {                    //并打印出安全序列
		  cout<<"系统是安全的,存在以下安全序列:"<<endl;
          cout<<"PCB    Work     Need     Allocation    Work+AlloCation  Finish"<<endl;
		  for(int r=0;r<5;r++)
              {
               int e=way[r];
		       cout<<name[e]<<"     ";
               for(int t=0;t<3;t++)
			   {
				   cout<<bak[t]<<" ";
			   }
			   cout<<"   ";
			   for(int t=0;t<3;t++)
               {
                cout<<Need[e][t]<<" ";
			   }
			   cout<<"      ";
                for(int t=0;t<3;t++)
                {
                 cout<<Allocation[e][t]<<" ";
                 }
				cout<<"         ";
				for(int t=0;t<3;t++)
                {
					bak[t]=bak[t]+Allocation[e][t];
                    cout<<bak[t]<<" ";
                 }
				cout<<"        ";
                cout<<"true"<<endl;
			 }
             for(int t=0;t<5;t++)
				 Finish[t]=false;
             return true;
          }
	  }
   cout<<"系统不安全,请从新输入进程数据!";
   for(int t=0;t<5;t++)
				 Finish[t]=false;
   return true;
}
void GetAll()                              //用来取得各个进程的数据
{
	for(int z=0;z<5;z++)             //采用逐个输入并接纳的方法接收数据
	{
		cout<<"现在要输入的是第"<<z+1<<"个进程的数据"<<endl;
		cout<<"请输入第"<<z+1<<"个进程的max:"<<endl;
		cout<<"A:";
		cin>>Max[z][0];
		cout<<"B:";
		cin>>Max[z][1];
		cout<<"C:";
		cin>>Max[z][2];
		cout<<"请输入第"<<z+1<<"个进程的Allocation"<<endl;
		cout<<"A:";
		cin>>Allocation[z][0];
		cout<<"B:";
		cin>>Allocation[z][1];
		cout<<"C:";
		cin>>Allocation[z][2];
		for(int y=0;y<3;y++)  //Need等于Max和Allocation之差
		{
			 Need[z][y]=Max[z][y]-Allocation[z][y];
		}
	}
	cout<<"请输入Available"<<endl;
		cout<<"A:";
		cin>>Available[0];
		cout<<"B:";
		cin>>Available[1];
		cout<<"C:";
		cin>>Available[2];
	for(int y=0;y<3;y++)
	{
		Work[y]=Available[y];//Work等于Available
	}
}

⌨️ 快捷键说明

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