安徽大学操作系统实验-银行家算法

银行家算法,最后界面和汤小丹第四版操作系统书第113页一样

#include <iostream>
#include <iomanip>
#include <stdbool.h>
#include <stdio.h>
#define N 30
using namespace std;
int Available[N];//可利用资源向量
int Max[N][N];//最大需求矩阵
int Allocation[N][N];//分配矩阵
int Need[N][N];//需求矩阵
int Request[N][N];//请求矩阵
int Work[N];//工作向量
bool Finish[N];//状态向量
//int WA[N][N];
int n,m,i,j;//n为进程数,m为资源总数
int p[N];
void Init();//数据初始化
bool Safe();//安全性判断算法
void yinhangjia();//银行家算法
void display();//输出显示

void Init()//初始化
{
    cout << "请输入进程数n" << endl;
    cin >> n;
    cout << "请输入资源总数m" << endl;
    cin >> m;
    cout << "请输入最大需求矩阵Max" << endl;
    for(i = 0; i < n; i++)
        for(j = 0; j < m; j++)
            cin >> Max[i][j];//初始化最大需求矩阵
    cout << "请输入分配矩阵Allocation" << endl;
    for(i = 0; i < n; i++)//初始化分配矩阵和需求矩阵
    {
        for(j = 0 ;j < m; j++)
        {
            cin >> Allocation[i][j];
            Need[i][j] = Max[i][j] - Allocation[i][j];
                if(Need[i][j] < 0)
        {
          cout << "第" << i+1 << "个进程所拥有的第" << j+1 <<"个资源错误,请重新输入"<<endl;
          j--;
          continue;
        }
        }
    }
    cout << "请输入各类资源现有数目:" << endl;
    for(j = 0; j < m; j++)
        cin >> Available[j];
}
void display()//输出显示,setw(2)表示输出宽度,默认右对齐
{
    int i,j;
    cout << "资源分配表为" << endl << "进程  Max    Allocation   Need      Available" << endl;
    for(i = 0; i < n; i++)
    {
        cout << "P" << i << ":";
        for(j = 0; j < m; j++)
          cout << setw(2) << Max[i][j] << " ";
          cout << " ";
        for(j = 0; j < m; j++)
            cout << setw(2) << Allocation[i][j] << " ";
            cout << " ";
        for(j = 0; j < m; j++)
            cout << setw(2) << Need[i][j] << " ";
            cout << "   ";
        if(i == 0)
            for(j = 0; j < m; j++)
            cout << setw(2) << Available[j] << " ";
            cout << endl;
    }
}
bool Safe()
{
      int Work[N];//表示系统可提供的各类资源数目
      int z,i,j,k,l = 0;
      for(i = 0; i < m; i++)
      {
           Work[i] = Available[i];//初始化工作向量Work=Available
      }
      for(i = 0; i < n; i++)
      {
          Finish[i] = false;//先令所有进程都是false
      }
      cout << "安全性检查:" << endl << "进程    Work      Need     Allocation    W+A      Finish" << endl;
      for(i = 0; i < n; i++)//P113页第二步
      {//从进程集合中找一个能满足下列条件的进程
          if(Finish[i] == true)
            continue;//从第一个进程开始连续的检查状态
          else
          {
            for(j = 0; j < m; j++)
            {
                if(Need[i][j] > Work[j])//需求数目是否小于当前各类资源数目
                  break;
            }
            if(j == m)
            {
                Finish[i] = true;
                cout << "P" << i << ":";
                cout << "  ";
                for(z = 0; z < m; z++)
                cout << setw(2) << Work[z] << " ";
                cout << "  ";
                for(z = 0; z < m; z++)
                cout << setw(2) << Need[i][z] << " ";
                cout << "  ";
                for(z = 0; z < m; z++)
                cout << setw(2) << Allocation[i][z] << " ";
                cout << "  ";
                for(k = 0; k < m; k++)
                    Work[k] += Allocation[i][k];//进程Pi获得资源后,顺利执行
                //执行完毕后,释放分配给他的资源
                for(z = 0; z < m; z++)
                    cout << setw(2) << Work[z] << " ";
                    cout << "    true" <<endl;
                    p[l++] = i;//确定序列顺序
                    i = -1;// 每次都从进程0开始扫描确定下一个进程,i=-1,然后i++正好等于0
            }
                else
                    continue;
            }
            if(l == n)
            {
                cout << "系统是安全的!" <<endl;
                cout << "安全序列" <<endl;
                for(i = 0; i < l; i++)
                {
                    cout << p[i];
                    if(i != l-1)
                        cout << "->";
                }
                cout << " " << endl;
                display();
                return true;
            }
      }
            cout << "系统是不安全的!" << endl;
            display();
            return false;
      }

void yinhangjia()
{
    int i,j,pneed;//要申请的资源号
    char flag;
    while(1)
    {
        cout << "请输入要申请的资源号:" << endl;
        cin >> pneed;
        cout << "请输入进程所请求的各资源的数量:" << endl;
        for(j = 0; j < m; j++)
            cin >> Request[pneed][j];
        for(j = 0; j < m; j++)
        {
            if(Request[pneed][j] > Need[pneed][j])
            {
                cout << "你输入的对" << j << "进程的需求资源数已经超过其所宣布的最大值,请重新输入" << endl;
                continue;
            }
            if(Request[pneed][j] > Available[j])
            {
                cout << "你输入的对" << j <<"进程的请求超过系统的资源数,请重新输入!" << endl;
                continue;
            }
        }
        for(j = 0; j < m; j++)
        {
            Available[j] = Available[j] - Request[pneed][j];
            Allocation[pneed][j] = Allocation[pneed][j] + Request[pneed][j];
            Need[pneed][j] = Need[pneed][j] - Request[pneed][j];
            if(Safe())
                cout << "同意分配请求" << endl;
            else
            {
                cout << "您的请求被拒绝" << endl;
                for (j = 0; j < m; j++)//本次试探作废,恢复原来的资源分配状态。
                {
                    Available[j] = Available[j] + Request[pneed][j];
                    Allocation[pneed][j] = Allocation[pneed][j] - Request[pneed][j];
                    Need[pneed][j] = Need[pneed][j] + Request[pneed][j];
                }
            }
            for(i = 0; i < n; i++)
            Finish[i] = true;
            cout << "你还想继续请求分配吗?是请按Y/y,否按其他键";
            cin >> flag;
            if(flag == 'Y' || flag == 'y')
            break;
        }
    }
}
int main()
{
    printf("******17组银行家算法********\n");
    printf("1:请初始化资源分配\n");
    printf("2:进行安全性检查\n");
    printf("3:进行银行家算法分配\n");
    Init();
    Safe();
    yinhangjia();
    return 0;
}

实验截图
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值