银行家算法 C++

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

#include "stdafx.h"
#include <iostream>
using namespace std;

#define M 5/*进程数*/
#define N 3/*资源数*/
#define False 0
#define True 1
int Available[N]={3,3,2};//系统可用资源数
int Max[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};//M个进程对N类资源最大资源需求
int Allocation[M][N]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};//M个进程已分配到的资源
int Need[M][N]={{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}};//M个进程还需要得到的资源情况

void Showdata();//打印数组
void Applydata();//申请资源
int Check();//安全性检测

void main()
{
    int n,i,j;

    //Need矩阵的计算
    //插入代码
for ( i = 0; i < M; i++)
    {
        for ( j = 0; j < N; j++)
        {
            Need[i][j] = Max[i][j] - Allocation[i][j];
            if (Need[i][j]<0)
            {
                j--;
                continue;
            }

        }

    }

    while(1)
    {
        cout<<"****************************************************"<<endl;
        cout<<"********************请选择操作**********************"<<endl;
        cout<<"******1 申请资源     2 显示当前资源状态    3退出*****"<<endl;
        cout<<"****************************************************"<<endl;
        cin>>n;

        switch(n)
        {
        case 1:
            Applydata();//请求资源
            break;
        case 2:
            Showdata();//显示资源分配情况;
            break;
        default:
            return;
        }
    }
}

void Applydata()
{
    int i,j,flag=0,a=0,b=0;
    int temp[N];//定义临时数组,存放申请的资源量。
    cout<<"输入进程的编号"<<endl;
    cin>>i;
    cout<<"输入每个资源的请求"<<endl;
    for(j=0;j<N;j++)
    {
        cin>>temp[j];   
    }

//判断Request≤Available && Need
       /******** 插入代码  ********/
for ( i = 0; i < N; i++)
    {
        if(temp[i]<=Need[a][i]){

            if(temp[i]<=Available[i]){
                flag++;
            }
        }
    }


    if(flag==N)
    {
        for(j=0;j<N;j++)
        {
            //修改Available、Need和Allocation的值
            /******** 插入代码  ********/
Available[j] -= temp[j];
            Allocation[a][j] += temp[j];
            Need[a][j] -= temp[j];

        }
    }
    int wflag=Check();
    if(wflag!=0)//如果系统不安全,回滚到原来的状态
    {
        cout<<"本次资源请求分配失败!!!"<<endl;
        for(j=0;j<N;j++)
        {
            //Available、Need和Allocation的值进行回滚
            /******** 插入代码  ********/
Available[j] += temp[j];
            Allocation[a][j] -= temp[j];
            Need[a][j] += temp[j];

        }
    }
    else
    {
        cout<<"本次资源请求分配成功!";
        cout<<endl<<endl;
    }
}

/*安全性检查函数*/
int Check()//在假定分配资源的情况下检查系统的安全性
{
    int Work[N],Finish[M],temp[M];//temp[]用来记录进程安全执行的顺序
    int i,j,m,k=0,count1=0,count2=0;

    for(i=0;i<M;i++)
        Finish[i]=false;

    for(j=0;j<N;j++)
        Work[j]=Available[j];//把可利用资源数赋给Work[]


while(count2<M)
    {
        for(i=0;i<M;i++)
        {
            for(j=0;j<N;j++)
            {
                if( Finish[i]==false&&Need[i][j]<=Work[j]
 )//当前进程没有在安全队列里面,并且每个进程的资源需求都可以满足
                    count1++;
            }

            if(count1==N)//当进程各类资源都满足Need<=Work时
            {
                for(m=0;m<N;m++)
                {
                    //插入代码 //资源使用完成释放回系统,系统可用资源增加
Work[m]+=Allocation[i][m];

                }
                Finish[i]=True;
                temp[k]=i;//记录下满足条件的进程
                k++;
            }
            count1=0;
            if (i==M-1)
            {
                count2++;
            }
        }
    }

    for(i=0;i<M;i++)//任何进程不满足,系统即为不安全状态
    {
        if(Finish[i]==false)
        {
            cout<<"系统处于不安全状态!!!"<<endl<<endl;
            return 1;
        }
    }

    cout<<endl<<"经安全性检查,系统处于安全状态"<<endl<<endl;
    cout<<"本次安全序列:";
    for(i=0;i<M;i++)//打印安全系统的进程调用顺序
    {
        cout<<"进程"<<temp[i];
        if(i<M-1)
            cout<<"->";
    }
    cout<<endl;
    return 0;
}

void Showdata()//显示数组的状态
{
    int i,j;

    cout<<"\t*****进程对各资源的最大需求Max*****"<<endl;
    cout<<"\t资源类别:R0 R1 R2"<<endl;
    for(i=0;i<M;i++)
    {
        cout<<"\t"<<i<<"号进程:";
        for(j=0;j<N;j++)
        {
            cout<<" "<<Max[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<endl<<endl;

    cout<<"\t*****各进程已分配资源情况 Allocation *****"<<endl;
    cout<<"\t资源类别:R0 R1 R2"<<endl;
    for(i=0;i<M;i++)
    {
        cout<<"\t"<<i<<"号进程:";
        for(j=0;j<N;j++)
        {
            cout<<" "<<Allocation[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<endl<<endl;

    cout<<"\t*********各进程还需要的资源量 Need *********"<<endl;
    cout<<"\t资源类别:R0 R1 R2"<<endl;
    for(i=0;i<M;i++)
    {
        cout<<"\t"<<i<<"号进程:";
        for(j=0;j<N;j++)
        {
            cout<<" "<<Need[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<endl<<endl;

    cout<<"\t*****系统可用资源向量 Available *****"<<endl;
    cout<<"\t资源类别:R0 R1 R2"<<endl;
    cout<<"\t资源数目:";
    for(j=0;j<N;j++)    
        cout<<Available[j]<<"  ";
    cout<<endl<<endl;

    Check();
    cout<<endl;
}

posted @ 2018-05-29 16:05  hunterxing  阅读(468)  评论(0编辑  收藏  举报