目的:利用银行家算法实现系统资源的动态分配及进程安全状态检测问题

编写银行家算法程序,能够对进程提出的资源请求进行判断,并打印出计算得到的安全序列

#include <iostream>
#include<cstring>
using namespace std;

#define False 0
#define True 1

//主要数据结构
int N = 50;//进程的最大数
int M = 100;//资源的最大数
char NAME[100] = { 0 };//资源的名称
int Max[50][100] = { 0 };//最大需求矩阵
int Avaliable[100] = { 0 };//可用资源矩阵
int Allocation[50][100] = { 0 };//系统已分配矩阵
int Need[50][100] = { 0 };//还需要资源矩阵
int Request[100] = { 0 };//请求资源向量
int Security[100] = { 0 };//存放安全序列
int Work[100] = { 0 };//存放系统可提供资源

/********
初始化数据:输入进程数量、资源种类、
各种资源可利用数量、
各进程的资源已分配数量、
各进程对资源最大需求量等。
********/
void chushihua()
{
    /* n为进程个数,即矩阵行数,m为资源个数,即矩阵列数。*/

    int i, j, n, m;
    int number, flag;
    char name;//输入资源名称
    cout << "系统可用资源个数为:";
    cin >> m;
    M = m;
    for (i = 0; i < m; i++)
    {
        cout << "资源" << i << "的名称:";
        cin >> name;
        NAME[i] = name;
        cout << "资源" << name << "的初始个数为:";
        cin >> number;
        Avaliable[i] = number;
    }
    cout << endl;
    cout << "请输入进程的数量:";
    cin >> n;
    N = n;
    cout << "请输入各进程的最大需求矩阵的值(" << n << "*" << m << "矩阵)[Max]:" << endl;
    for (i = 0; i < n; i++)
        for (j = 0; j < m; j++)
            cin >> Max[i][j];
    int temp[100] = { 0 };
    do
    {
        flag = 0;
        cout << "请输入各进程已经分配的资源量(" << n << "*" << m << "矩阵)[Allocation]:" << endl;
        for (i = 0; i < n; i++)
            for (j = 0; j < m; j++)
            {
                cin >> Allocation[i][j];
                if (Allocation[i][j] > Max[i][j]) flag = 1;
                Need[i][j] = Max[i][j] - Allocation[i][j];
                temp[j] += Allocation[i][j];
            }
        if (flag == 1)
            cout << "申请的资源大于最大需求量,请重新输入!";
        cout << endl;
    } while (flag);
    for (j = 0; j < m; j++)
        Avaliable[j] = Avaliable[j] - temp[j];
}



/********
显示资源分配矩阵
********/
void showdata()
{
    int i, j;
    cout << "*************************************************************" << endl;
    cout << "系统目前可用的资源[Avaliable]:" << endl;
    for (i = 0; i < M; i++)
        cout << NAME[i] << " ";
    cout << endl;
    for (j = 0; j < M; j++)
        cout << Avaliable[j] << " ";//输出分配资源
    cout << endl;
    cout << "系统当前的资源分配情况如下:" << endl;
    cout << "           Max      Allocation     Need" << endl;
    cout << "进程名     ";
    for (j = 0; j < 3; j++)
    {
        for (i = 0; i < M; i++)
            cout << NAME[i] << " ";
        cout << "      ";
    }
    cout << endl;
    for (i = 0; i < N; i++)
    {
        cout << " P" << i << "         ";
        for (j = 0; j < M; j++)
            cout << Max[i][j] << " ";
        cout << "      ";
        for (j = 0; j < M; j++)
            cout << Allocation[i][j] << " ";
        cout << "      ";
        for (j = 0; j < M; j++)
            cout << Need[i][j] << " ";
        cout << endl;
    }
}
/********
安全性算法
********/
int safe()
{
    int i, j, k = 0, n, apply;
    int Finish[100] = { 0 };
    for (j = 0; j < N; j++)
        Work[j] = Avaliable[j];
    for (i = 0; i < N; i++)
    {
        apply = 0;
        for (j = 0; j < M; j++)
        {
            if (Finish[i] == False && Need[i][j] <= Work[j])
            {
                apply++;
                if (apply == M)
                {
                    for (n = 0; n < M; n++)
                        Work[n] = Work[n] + Allocation[i][n];//变分配数
                    Finish[i] = True;
                    Security[k] = i;
                    i = -1;
                    k++;
                }
            }
        }
    }
    for (i = 0; i < N; i++)
    {
        if (Finish[i] == False)
        {
            cout << "系统不安全,已瘫痪" << endl;//不成功系统不安全
            return -1;
        }
    }
    cout << "系统是安全的!" << endl;//如果安全,输出成功
    cout << "存在一个安全序列:";
    for (i = 0; i < N; i++)
    {//输出运行进程数组
        cout << "P" << Security[i];
        if (i < N - 1) cout << "->";
    }
    cout << endl;
    return 0;
}

/********
尝试分配资源
********/
int test(int i)//进行资源分配
{
    int j;
    for (j = 0; j < N; j++)
    {
        Avaliable[j] = Avaliable[j] - Request[j];
        Allocation[i][j] = Allocation[i][j] + Request[j];
        Need[i][j] = Need[i][j] - Request[j];
    }
    return 1;
}
/********
利用银行家算法对申请资源对进行试分配
********/
void bank()
{
    char ch;
    int i, j;
    ch = 'y';
    cout << "请输入请求分配资源的进程号(0-" << N - 1 << "):";
    cin >> i;//输入须申请资源的进程号
    cout << "请输入进程P" << i << "要申请的资源个数:" << endl;
    for (j = 0; j < M; j++)
    {
        cout << NAME[j] << ":";
        cin >> Request[j];//输入需要申请的资源
    }
    for (j = 0; j < M; j++)
    {
        if (Request[j] > Need[i][j])//判断申请是否大于需求,若大于则出错
        {
            cout << "进程P" << i << "申请的资源大于它需要的资源";
            cout << " 分配不合理,不予分配!" << endl;
            ch = 'm';
            break;
        }
        else
        {
            if (Request[j] > Avaliable[j])//判断申请是否大于当前可分配资源,若大于则出错
            {
                cout << "进程" << i << "申请的资源大于系统现在可利用的资源";
                cout << endl;
                cout << " 系统尚无足够资源,不予分配!" << endl;
                ch = 'm';
                break;
            }
        }
    }
    if (ch == 'y')
    {
        test(i);//根据进程需求量变换资源
        showdata();//根据进程需求量显示变换后的资源
        safe();//根据进程需求量进行银行家算法判断
    }
}

int main()//主函数
{
    char choice=1;
    
    cout << "\t---------------------------------------------------" << endl;
    cout << "\t||                                               ||" << endl;
    cout << "\t||               银行家算法的实现                ||" << endl;
    cout << "\t||                                               ||" << endl;
    cout << "\t||           班级:xxx                           ||" << endl;
    cout << "\t||           学生: xxx     xxx    xxx           ||" << endl;
    cout << "\t||           学号:   11     12     13            ||" << endl;
    cout << "\t---------------------------------------------------" << endl;
    chushihua();//初始化数据
    showdata();//显示各种资源
    safe();//用银行家算法判定系统是否安全
    while (choice)
    {
        cout << "*************************************************************" << endl;
        cout << endl;
        cout << endl;
        cout << "\t-------------------银行家算法演示------------------" << endl;
        cout << "                     R(r):请求分配   " << endl;
        cout << "                     E(e):退出       " << endl;
        cout << "\t---------------------------------------------------" << endl;
        cout << "请选择:";
        cin >> choice;
        switch (choice)
        {
        case 'r':
        case 'R':

            bank(); break;
        case 'e':
        case 'E':

            choice = 0; break;
        default: cout << "请正确选择!" << endl; break;
        }
    }
}

Logo

鸿蒙生态一站式服务平台。

更多推荐