我们现在上操作系统,老师让我们用C或C++写一个银行家算法,,但是我不知道该从何下手,更不用说写了,请哪位高手给我写写哈!必有高分!

解决方案 »

  1.   

    `
    #pragma hdrstop#pragma argsused
    #include <windows.h>
    #include "string.h"
    #include "iostream.h"
    #define M 5          
    #define N 3           
    #define FALSE 0
    #define TRUE 1int MAX[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};int AVAILABLE[N]={10,5,7};int
    ALLOCATION[M][N]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};int NEED[M][N]={{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}};
    int Request[N]={0,0,0};void main(int argc, char* argv[])
    {
    int i=0,j=0;
    char flag='Y';
    void showdata();
    void changdata(int);
    void rstordata(int);
    int chkerr(int);
    showdata();
    while(flag=='Y'||flag=='y')
    {
      i=-1;
      while(i<0||i>=M)
      {
       cout<<"请输入需申请资源的进程号(0-4):";
       cin>>i;
       if(i<0||i>=M)cout<<"输入的进程号不存在,重新输入!"<<endl;
      }
      cout<<"  请输入进程"<<i<<"申请的资源数"<<endl;
      for (j=0;j<N;j++)
      {
       cout<<"  资源"<<j<<"的申请数是:  ";
       cin>>Request[j];
       if(Request[j]>NEED[i][j])
       {
        cout<<"进程"<<i<<"申请的资源数大于进程"<<i<<"的"<<j<<"类资源的最大资源量!";
        cout<<"WRONG!请重新选择!"<<endl<<endl;
        flag='N';
        break;
       }
       else
       {
        if(Request[j]>AVAILABLE[j])
        {
         cout<<"进程"<<i<<"申请的资源数大于系统可用"<<j<<"类资源的资源量!";
         cout<<"WRONG!请重新选择!"<<endl<<endl;
         flag='N';
         break;
        }
       }
      }
      if(flag=='Y'||flag=='y')
      {
       changdata(i);
       if(chkerr(i))
       
       {
        rstordata(i);
        showdata();
       }
       else
        showdata();
      }
      else
       showdata();
      cout<<endl;
      cout<<"如想继续银行家算法演示,请按'y'键继续,请按或'n'键退出: ";
      cin>>flag;
    }
    }void showdata()
    {
    int i,j;
        cout<<endl<<endl<<"              银行家算法演示程序    "<<endl;Sleep(500);
    cout <<endl<<"_______________________________________________"<<endl<<"系统可用的资源数为:"<<endl;
    cout<<endl;
    for (j=0;j<N;j++)cout<<"     资源"<<j<<":"<<AVAILABLE[j];
        cout<<endl;    cout<<endl;
    cout<<"_______________________________________________"<<endl<<"各进程还需要的资源量:"<<endl<<endl;
    for (i=0;i<M;i++)
    {
      cout<<"进程"<<i<<":";
      for (j=0;j<N;j++)cout<<"     资源"<<j<<":"<<NEED[i][j];
            cout<<endl;
    }
        cout<<endl;
    cout<<"________________________________________________"<<endl<<"各进程已经得到的资源量:"<<endl<<endl;
    for (i=0;i<M;i++)
    {
      cout<<"进程"<<i<<":";
      for (j=0;j<N;j++)cout<<"     资源"<<j<<":"<<ALLOCATION[i][j];
            cout<<endl;
    }
        cout<<endl;
    };
    void changdata(int k)
    {
    int j;
    for (j=0;j<N;j++)
    {
      AVAILABLE[j]=AVAILABLE[j]-Request[j];
      ALLOCATION[k][j]=ALLOCATION[k][j]+Request[j];
      NEED[k][j]=NEED[k][j]-Request[j];
    }
    };
    void rstordata(int k)
    {
    int j;
    for (j=0;j<N;j++)
    {
      AVAILABLE[j]=AVAILABLE[j]+Request[j];
      ALLOCATION[k][j]=ALLOCATION[k][j]-Request[j];
      NEED[k][j]=NEED[k][j]+Request[j];
    }
    };
    int chkerr(int s)
    {
    int WORK,FINISH[M],temp[M];
    int i,j,k=0;
    for(i=0;i<M;i++)FINISH[i]=FALSE;
    for(j=0;j<N;j++)
    {
    WORK=AVAILABLE[j];
    i=s;
    while(i<M)
    {
      if (FINISH[i]==FALSE&&NEED[i][j]<=WORK)
      {
       WORK=WORK+ALLOCATION[i][j];
       FINISH[i]=TRUE;
       temp[k]=i;
       k++;
       i=0;
      }
      else
      {
       i++;
      }
    }
    for(i=0;i<M;i++)
      if(FINISH[i]==FALSE)
      {
       cout<<endl;
       cout<<"SORRY! 本次资源申请不成功!"<<endl;
       cout<<endl;
       return 1;
      }
    }
    cout<<endl;
    cout<<"经安全性检查证明系统安全,本次分配成功。"<<endl;
    cout<<endl;
    cout<<"本次安全序列:";
    for(i=0;i<M;i++)cout<<"进程"<<temp[i]<<"->";
    cout<<endl<<endl;;
    return 0;
    };
      

  2.   

    上面的文件也许有错误,因为的现在电脑没有VC,我直接把CPP文件贴上来,发现头文件好象有点问题了,
      chkerr()// 安全检测函数,我个人觉得你该修改,此算法是老师提的,我觉得老师有错误,~,当时要求每一人完成一个。后来的操作系统作业 我选择了 读者-写者 问题: http://blog.csdn.net/surstar/archive/2005/02/05/282109.aspx