// 银行家算法.cpp : 定义控制台应用程序的入口点。 //

#include "stdafx.h"

#include<iostream> using namespace std;

 const int MaxNumber=20;   

  int Available[MaxNumber];//可利用资源向量  int Max[MaxNumber][MaxNumber];//最大需求矩阵

 int Allocation[MaxNumber][MaxNumber];//Allocation[i][j]=K表示进程i当前已分得Rj类资源的数目为K个;  int Need[MaxNumber][MaxNumber];//Need[i][j]=K表示进程i还需要Rj类的资源K个

 int Request[MaxNumber];//Request(i)[j]表示进程Pi需要K个Rj类型的资源  int SafeOrder[MaxNumber]; //安全序列;

 int x=0;//记录安全序列的存储位置;  int n=0;//进程数目  int m=0;//资源数目  int index=0;//请求资源的进程下标

 bool Safety_algorithm();

 //**********************************银行家算法********************************************************;  bool Bank_algorithm(){     int i=0;      for(i=0;i<m;i++)   {    if(Request[i]<=Need[index][i])//判断需要的资源有没有超过它所宣布的资源最大值    {//若请求的资源<=还需要的最大资源        if(Request[i]<=Available[i])//判断需要的资源有没有大于系统当前资源;     {//若需要的资源<=系统当前的资源      Available[i]-=Request[i];//更新系统资源:=系统资源-当前请求的资源      Allocation[index][i]+=Request[i];//更新已分配资源:=已分配资源+当前请求的资源      Need[index][i]-=Request[i]; //更新还需要的最大资源:=还需要的最大资源-当前请求的资源          }         else     {cout<<"尚不足够资源,P"<<index<<" 须等待"<<endl<<endl<<endl;return false;}          }    else     {cout<<"所需资源数已超过它所宣布的最大值"<<endl<<endl<<endl;return false;}

  }   return true;   }  //**********************************安全性算法********************************************************;     bool Safety_algorithm(){   int Work[MaxNumber];//工作向量   bool Finish[MaxNumber]={false};//表示系统有足够资源分配给该进程   bool sourse=true; //找到满足条件是的判断   int i=0,num=n;  

  for(i=0;i<m;Work[i++]=Available[i]);//工作向量初始化:=系统当前可提供的各类资源    //  cout<<"如果申请成功后,当前还可提供的资源"<<endl;       for(i=0;i<m;cout<<Work[i++]<<" ");      cout<<endl;

        while(num--)   {    for(i=0;i<n;i++)    {     sourse=true;     if(!Finish[i])     {//找到满足系统不够分配资源的进程      //判断还需要的资源是否>(大于)系统当前可提供的资源,如果是将相应的sourse置成false;      for(int j=0;j<m;j++)       if(Need[i][j]>Work[j]){sourse=false;break;}            if(sourse)  break; //如果sourse为假则表示系统处于不安全状态;              }        }    if(sourse)    {     for(int j=0;j<m;j++)       Work[j]+=Allocation[i][j];//更新当前系统可提供的资源;=原本加上 已经分配给进程的资源     Finish[i]=true;//表示有足够资源分配给进程     SafeOrder[x++]=i;//将进程下标放进安全系列              }    else    {cout<<"系统处于不安全状态"<<endl;return false;}          }   return true;   }

int _tmain(int argc, _TCHAR* argv[]) {  freopen("text.txt","rt",stdin);     //cout<<"输入进程个数N"<<endl;   cin>>n;   //cout<<"输入资源种类数目M"<<endl;   cin>>m;

  int i=0,j=0;   //初始化进程已分配的资源   for(i=0;i<n;i++)       for(j=0;j<m;cin>>Allocation[i][j++]);   //初始化进程还需要的进程      for(i=0;i<n;i++)    for(j=0;j<m;cin>>Need[i][j++]);       //初始化当前系统还有的资源   for(i=0;i<m;cin>>Available[i++]);      cout<<"####################当前还可提供的资源"<<endl;

        if(Safety_algorithm())   {//判断当前时刻系统是否处于安全状态    cout<<"@@@@@@@@@@@@@@此时的安全序列为:"<<endl;      for(j=0;j<n;j++)       cout<<"p"<<SafeOrder[j]<<",";      cout<<endl<<endl<<endl;

  }   while(cin>>index){//输入请求服务进程的下标    

   cout<<"进程P"<<index<<"申请资源"<<endl;     //输入进程请求的资源数    for(j=0;j<m;j++)    {cin>>Request[j];cout<<Request[j]<<" ";}    cout<<endl;

   //用银行家算法判断是否应该分配给资源    if(Bank_algorithm())    {     cout<<"###########如果申请成功后,当前还可提供的资源"<<endl;

    if(Safety_algorithm())      {       cout<<"@@@@@@@@@@@@@@此时的安全序列为:"<<endl;         for(j=0;j<n;j++)          cout<<"p"<<SafeOrder[j]<<",";         cout<<endl<<endl<<endl;       }     else  {cout<<endl<<endl<<endl;return 0;}        }   }

 return 0; }

 

posted on 2012-11-09 20:36  叶城宇  阅读(415)  评论(0编辑  收藏  举报