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

📄 main.cpp

📁 操作系统 vector银行家算法 可以参考啊
💻 CPP
字号:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
#define FALSE 0
#define TRUE 1
typedef vector<vector<int> > vec;
int row;  
int column;
//---------------------------------
void main()
{
int i=0,j=0;
char flag='Y';
void parse(vec& v,int a,int b);
void getSource(vector<int>& v,int b);
void vectorInit(vector<int>&v,int b);
void countSource(vec &v,vec& x,vec& y,int a,int b);
void showdata(vector<int>& a,vec& b,vec& c);
void changdata(vector<int> &a,vector<int> &r,vec& c,vec& b,int k);
void rstordata(vector<int> &a,vector<int> &r,vec& c,vec& b,int k);
int chkerr(vector<int> &a,vec& c,vec& b,int s);
vector<int> AVAILABLE,Request;
vec MAX,ALLOCATION,NEED;
//程序开始
cout<<endl<<"请输入进程的个数:"<<endl;
cin>>row;//获得行数
cout<<endl<<"请输入资源的分类数:"<<endl;
cin>>column;//获得列数
cout<<"请输入"<<row<<"个进程对"<<column<<"类资源最大需求量:";
parse(MAX,row,column);
cout<<"请输入系统各类可用资源数:\n";
getSource(AVAILABLE,column);
cout<<"请输入"<<row<<"个进程已经得到"<<column<<"类资源的资源量:";
parse(ALLOCATION,row,column);
countSource(NEED,MAX,ALLOCATION,row,column);
vectorInit(Request,column);//初始化Request,否则可能会导致程序运行中终止
showdata(AVAILABLE,NEED,ALLOCATION);
while(flag=='Y'||flag=='y')
{
  i=-1;
  while(i<0||i>=row)
  {
   cout<<"  请输入需申请资源的进程号(从0到"<<row-1<<",否则重输入!):";
   cin>>i;
   if(i<0||i>=row)cout<<"  输入的进程号不存在,重新输入!"<<endl;
  }
  cout<<"  请输入进程"<<i<<"申请的资源数"<<endl;
  for (j=0;j<column;j++)
  {
   cout<<"  资源"<<j<<":  ";
   cin>>Request[j];
   if(Request[j]>NEED[i][j])
   {
    cout<<"  进程"<<i<<"申请的资源数大于进程"<<i<<"还需要"<<j<<"类资源的资源量!";
    cout<<"申请不合理,出错!请重新选择!"<<endl<<endl;
    flag='N';
    break;
   }
   else
   {
    if(Request[j]>AVAILABLE[j])
    {
     cout<<"  进程"<<i<<"申请的资源数大于系统可用"<<j<<"类资源的资源量!";
     cout<<"申请不合理,出错!请重新选择!"<<endl<<endl;
     flag='N';
     break;
    }
   }
  }
  if(flag=='Y'||flag=='y')
  {
  changdata(AVAILABLE,Request,ALLOCATION,NEED,i);
  if(chkerr(AVAILABLE,ALLOCATION,NEED,i))
   {
    rstordata(AVAILABLE,Request,ALLOCATION,NEED,i);
    showdata(AVAILABLE,NEED,ALLOCATION);
   }
   else
   showdata(AVAILABLE,NEED,ALLOCATION);
  }
  else
   showdata(AVAILABLE,NEED,ALLOCATION);
  cout<<endl;
  cout<<"  是否继续银行家算法演示,按'Y'或'y'键继续,按'N'或'n'键退出演示: ";
  cin>>flag;
}
}
//-----------------------------初始化赋值函数------
void parse(vec& v,int a,int b)
{
int column=b;
int row=a;
v.resize(row);
for(int i=0;i<row;i++)
v[i].resize(column);
cout<<endl<<"获得进程内容:"<<endl;
for(i=0;i<row;i++){
for(int j=0;j<column;j++){
cout<<"第"<<i<<"进程,第"<<j<<"资源:";
cin>>v[i][j];}
}
}
//------------------
void getSource(vector<int>& v,int b)
{
int column=b;
v.resize(column);
for(int i=0;i<column;i++){
cout<<"第"<<i<<"资源:";
cin>>v[i];
}
}
//------------
void vectorInit(vector<int>& v,int b)
{
int col=b;
v.resize(col);
for(int i=0;i<col;i++){
v[i]=0;
}
}
//-----------------
void countSource(vec& v,vec& x,vec& y,int a,int b)
{
int column=b;
int row=a;
v.resize(row);
for(int i=0;i<row;i++)
v[i].resize(column);
for(i=0;i<row;i++){
for(int j=0;j<column;j++){
v[i][j]=x[i][j]-y[i][j];
}
}
cout<<endl<<"获得NEED"<<endl;
}
//-----------------------
void showdata(vector<int>& a,vec& b,vec& c)//显示当前资源情况
{
int i,j;
    cout<<"*****************系统可用的资源数为*****************"<<endl<<endl;
cout<<"      ";
for (j=0;j<column;j++)cout<<"  资源"<<j<<":  "<<a[j];
    cout<<endl;
cout<<"*****************各进程还需要的资源量*****************"<<endl<<endl;
for (i=0;i<row;i++)
{
  cout<<"进程"<<i<<":";
  for (j=0;j<column;j++)cout<<"  资源"<<j<<":  "<<b[i][j];
        cout<<endl;
}
    cout<<endl;
cout<<"*****************各进程已经得到的资源量*****************"<<endl<<endl;
for (i=0;i<row;i++)
{
  cout<<"进程"<<i<<":";
  for (j=0;j<column;j++)cout<<"  资源"<<j<<":  "<<c[i][j];
        cout<<endl;
}
    cout<<endl;
}
//-----------------------
void changdata(vector<int>& a,vector<int>& r,vec& c,vec& b,int k)//分配,改变资源情况
{
int j;
for (j=0;j<column;j++)
{
  a[j]=a[j]-r[j];
  c[k][j]=c[k][j]+r[j];
  b[k][j]=b[k][j]-r[j];
}
}
//------------------------
void rstordata(vector<int> &a,vector<int> &r,vec& c,vec& b,int k)//回收,改变资源情况
{
int j;
for (j=0;j<column;j++)
{
  a[j]=a[j]+r[j];
  c[k][j]=c[k][j]-r[j];
  b[k][j]=b[k][j]+r[j];
}
}
//---------------------------------------------
int chkerr(vector<int> &a,vec& c,vec& b,int s)//检查虚拟分配
{
vector<int> FINISH,temp;
int WORK;
vectorInit(FINISH,row);
vectorInit(temp,row);
int i,j,k=0;
for(i=0;i<row;i++)FINISH[i]=FALSE;
for(j=0;j<column;j++)
{
WORK=a[j];
i=s;
while(i<row)
{
  if (FINISH[i]==FALSE&&b[i][j]<=WORK)
  {
   WORK=WORK+c[i][j];
   FINISH[i]=TRUE;
   temp[k]=i;
   k++;
   i=0;
  }
  else
  {
   i++;
  }
}
for(i=0;i<row;i++)
  if(FINISH[i]==FALSE)
  {
   cout<<endl;
   cout<<"  系统不安全!!! 本次资源申请不成功!!!"<<endl;
   cout<<endl;
   return 1;
  }
}
cout<<endl;
cout<<"  经安全性检查,系统安全,本次分配成功。"<<endl;
cout<<endl;
cout<<"  本次安全序列:";
for(i=0;i<row;i++)cout<<"进程"<<temp[i]<<"->";
cout<<endl<<endl;;
return 0;
}

⌨️ 快捷键说明

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