银行家算法及实现代码

1).银行家算法中的数据结构
(1).可利用资源向量Available
(2).最大需求矩阵Max
(3).分配矩阵Allocation
(4).需求矩阵Need
2).银行家算法
Request请求向量,
(1).如果Request[i] <= Need[i][j]转下步,否则它所需要的资源数已超过它所需要的最大值
(2).如果Request[i] <= Available[i][j]转下步,否则尚无足够资源,进程需等待
(3).系统试分配给进程p,并修改Available,Allocation和Need
Available[j] -= Request[j]
Allocation[i][j] += Request[j]
Need[i][j] -= Request[j]
(4)系统执行安全性算法,检查此次资源分配后系统是否处于安全状态.若安全,才正式分配;否则恢复原来的分配状态,让该进程等待
3).安全性算法
(1).设置两个向量,工作向量Work,在执行安全性算法开始时 Work=Available;Finish:表示有足够的资源分配给进程,使之运行完成,Finish[i]=false;当有足够资源分配给进程时,再另Finish[i]=false
(2).从进程集合中找到一个满足该条件的进程:
Finish[i]=false
Need[i][j] <= Work[j]
(3).当进程获得资源后,可顺利执行,并修改Work向量和Finsh向量
Work[i] += Allocation[i][j]
Finish[i]=true
(4).如果所有进程的Finish[i]=true说明系统处于安全状态,否则系统处于不安全状态.

#include <iostream>
#include <cstring>
#define N 100
using namespace std;

int Available[N] = {0};         //可利用资源向量
int Max[N][N] = {0};            //最大需求矩阵
int Allocation[N][N] = {0};     //分配矩阵
int Need[N][N];                 //需求矩阵

int Request[N];                 //请求向量

int Work[N];                    //工作向量
bool Finish[N];                 //true或false
int SafeSeries[N];              //安全序列

int n;                          //进程数
int m;                          //资源数

/*---------------------------主要数据结构和资源的初始化-----------------------------*/
void Init()
{
    cout << "请输入进程总数:"; cin >> n;
    cout << "请输入资源种类数:"; cin >> m;

    cout << "请输入" << m << "类资源的当前可利用资源数目:" << endl;
    for(int i = 0; i < m; ++i )
    {
        cin >> Available[i];
    }

    cout << "最大需求矩阵" << n << "*" << m << "输入:" << endl;
    for(int i = 0; i < n; ++i )
    {
        for(int j = 0; j < m; ++j )
        {
            cin >> Max[i][j];
        }
    }

    cout << "分配矩阵" << n << "*" << m << "输入:" << endl;
    for(int i = 0; i < n; ++i )
    {
        for(int j = 0; j < m; ++j )
        {
            cin >> Allocation[i][j];
        }
    }

    //需求矩阵
    for(int i = 0; i < n; ++i )
    {
        for(int j = 0; j < m; ++j )
        {
            Need[i][j] = Max[i][j] - Allocation[i][j];
        }
    }

    int sum;
    for(int i = 0; i < m; ++i )
    {
        sum = 0;
        for(int j = 0; j < n; j++)
        {
            sum += Allocation[j][i];
        }
        Available[i] -= sum;
    }
}

/*------------------安全性算法------------------------*/
bool IsSafe()
{
    int i;
    for (i = 0; i < n; i++)
    {
        if(Finish[i] == true)
            continue;
        else
            break;
    }
    if(i == n)
        return true;    //安全
    else
        return false;   //不安全
}

bool Select(int &tmp,int tmpNeed[][N])
{
    //选择一个Finish[i]=false,Need[i]<=Work[i]
    int j = 0;
    for (int i = 0; i < n; i++)
    {
        if(Finish[i] == true)
            continue;
        for (j = 0; j < m; j++)
        {
            if(tmpNeed[i][j] > Work[j])
                break;
        }
        if(j == m)
        {
            tmp = i;
            return true;
        }
    }
    return false;
}

bool Safe(int *tmpAvail,int tmpAlloc[][N],int tmpNeed[][N])
{
    for(int i = 0; i < n; i++)
    {
        Finish[i] = false;
    }
    for (int j = 0; j < m; j++)
    {
        Work[j] = tmpAvail[j];
    }
    int tmp = 0;
    int index = 0;
    while(Select(tmp, tmpNeed))
    {
        Finish[tmp] = true;
        for (int k = 0; k < m; k++)
        {
            Work[k] += tmpAlloc[tmp][k];
        }
        SafeSeries[index] = tmp;
        index++;
    }
    if(IsSafe() == true)
        return true;    //安全
    else
        return false;   //不安全
}

/*------------------银行家算法-----------------------*/
void BankA()
{
    int i=0;
    int index=0;
    cout<<"请输入请求资源的进程下标:"; cin >> index;
    //assert(index < n && index >= 0);

    cout << "请输入当前的请求资源:" << endl;
    for (i = 0;i < m;++i)
    {
        cin >> Request[i];
    }
    for (i = 0;i < m; i++)
    {
        if(Request[i] <= Need[index][i])
        {
            continue;
        }
        else
        {
            cout << "第一次试分配失败" << endl;
            return;
        }
    }

    for(i = 0;i < m; i++)
    {
        if(Request[i] <= Available[i])
        {
            continue;
        }
        else
        {
            cout<<"第二次试分配失败"<<endl;
            return;
        }
    }

    //开始尝试分配
    int tmpAvail[N]={0};                //临时可利用资源
    int tmpAlloc[N][N]={0};             //临时分配矩阵
    int tmpNeed[N][N]={0};              //临时需求矩阵
    memmove(tmpAvail, Available, sizeof(int) * N);
    memmove(tmpAlloc, Allocation, sizeof(int) * N * N);
    memmove(tmpNeed, Need,sizeof(int) * N * N);

    for (int i = 0; i < m; i++)
    {
        tmpAvail[i] -= Request[i];
        tmpAlloc[index][i] += Request[i];
        tmpNeed[index][i] -= Request[i];
    }
    //开始执行安全性算法
    bool ret=Safe(tmpAvail,tmpAlloc,tmpNeed);
    if(ret == true)
    {
        //如果试分配成功则更新Available,Allocation,Allocation的状态
        memmove(Available, tmpAvail, sizeof(int) * N);
        memmove(Allocation, tmpAlloc, sizeof(int) * N * N);
        memmove(Need, tmpNeed, sizeof(int) * N * N);
        cout<<"进程p"<< index << "请求资源允许" <<endl;

        cout<<"所得安全序列为:";
        for (int i = 0; i < n ; ++i)
        {
            cout << "p" << SafeSeries[i] << " ";
        }
        cout << endl;
    }
    else
    {
        //只要试分配失败则将Finish置为false
        for(int i = 0; i < n; ++i)
        {
            Finish[i] = false;
        }
        cout<<"第三次试分配失败"<<endl;
    }
}

/*------------------------打印矩阵----------------------------*/
void display()
{
    cout << "当前可利用的资源数目:" <<endl;
    for(int i = 0; i < m; ++i)
    {
        cout << Available[i] <<" ";
    }
    cout << endl;

    cout << "最大需求矩阵:" << endl;
    for(int i = 0; i < n; i++ )
    {
        for(int j = 0; j < m; j++)
        {
            cout << Max[i][j] << " ";
        }
        cout << endl;
    }

    cout << "分配矩阵:" << endl;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cout << Allocation[i][j] <<" ";
        }
        cout << endl;
    }

    cout << "需求矩阵:" << endl;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++ )
        {
            cout << Need[i][j] <<" ";
        }
        cout << endl;
    }
}

int main()
{
    cout << "银行家算法测试开始:" << endl;
    cout << "-------------------------------------" << endl;
    Init();
    cout << "-------------------------------------" << endl;
    display();
    cout << "-------------------------------------" << endl;
    while(1){
        BankA();
        cout << "-------------------------------------" << endl;
        display();
        cout << "-------------------------------------" << endl;
    }
    return 0;
}

  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值