考研 操作系统:银行家算法

最近复习到死锁这一张了,之前找工作背八股的时候,这个知识点,背的挺溜的,但是思想什么的虽然都理解了,但是不如动手实现一下来的方便,所以用编程语言实现了一个银行家算法来解决死锁问题。

编写的代码:

#include <iostream>
#include <queue>
#include <vector>
/*进程个数和资源个数*/
int n, m;

class Bank {
 public:
  Bank(int pid,
       std::vector<int> request,
       std::vector<int> _avli,
       std::vector<std::vector<int>> _max,
       std::vector<std::vector<int>> _alloc)
      : Pid(pid),
        Request(request),
        Avliable(_avli),
        Max(_max),
        Allocation(_alloc) {}
  void GetNeed();
  bool pdyc();
  void ChangeMatrixState();
  void RecoverMatruxState();
  bool CheekSafe();
  void fpzy();

 private:
  int Pid;                           /*进程id*/
  std::vector<int> Avliable;         /*系统拥有每类资源的个数*/
  std::vector<int> Request;          /*某进程提出的请求*/
  std::queue<int> Que;               /*存放进程id队列*/
  std::vector<std::vector<int>> Max; /*每个进程需求的最资源数*/
  std::vector<std::vector<int>> Allocation; /*每个进程已分配的资源数*/
  std::vector<std::vector<int>> Need; /*每个进程还需要的进程资源个数*/
};

void Bank::GetNeed() {
  std::vector<int> col(n, 0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      col[j] = Max[i][j] - Allocation[i][j];
    }
    Need.push_back(col);
  }
}

bool Bank::pdyc() {
  for (int i = 0; i < m; i++) {
    if (Request[i] > Need[Pid][i]) {
      std::cout << "error!over Need[i] sources!" << std::endl;
      return true;
    }
  }

  for (int i = 0; i < m; i++) {
    if (Request[i] > Avliable[i]) {
      std::cout << "error!over Avliable[i] sources!" << std::endl;
      return true;
    }
  }
  return false;
}

void Bank::ChangeMatrixState() {
  for (int i = 0; i < m; i++) {
    Avliable[i] -= Request[i];
  }

  for (int i = 0; i < m; i++) {
    Need[Pid][i] -= Request[i];
  }
  for (int i = 0; i < m; i++) {
    Allocation[Pid][i] += Request[i];
  }
}

void Bank::RecoverMatruxState() {
  for (int i = 0; i < m; i++) {
    Avliable[i] += Request[i];
  }

  for (int i = 0; i < m; i++) {
    Need[Pid][i] += Request[i];
  }
  for (int i = 0; i < m; i++) {
    Allocation[Pid][i] -= Request[i];
  }
}

bool Bank::CheekSafe() {
  std::vector<int> work(Avliable);
  std::vector<std::vector<int>> wneed(Need);

  while (1) {
    int flag = 0;
    for (int i = 0; i < wneed.size(); i++) {
      int js = 0;
      for (int j = 0; j < m; j++) {
        if (wneed[i][j] <= work[j]) {
          js++;
        }
      }
      if (js == m) {
        flag = 1;
        Que.push(i); /*将进程放入队列*/
        for (int j = 0; j < m; j++) {
          work[j] += wneed[i][j];
        }

        wneed.erase(wneed.begin() + i);
        i--;
      }
    }
    if (!flag) {
      break;
    }
    if (Que.size() == n) {
      return true;
    }
  }
  return false;
}
void Bank::fpzy() {
  GetNeed();
  if (pdyc()) {
    return;
  }
  ChangeMatrixState();
  if (!CheekSafe()) {
    RecoverMatruxState();
    std::cout << "fpzy fail!" << std::endl;
  } else {
    std::cout << "fpzy successful!" << std::endl;
  }
}

int main() {
  n = 5, m = 3;
  std::vector<int> Avaliable = {3, 3, 2};
  std::vector<std::vector<int>> Max = {
      {7, 5, 3}, {3, 2, 2}, {9, 0, 2}, {2, 2, 2}, {4, 3, 3}};
  std::vector<std::vector<int>> Allocation = {
      {0, 1, 0}, {2, 0, 0}, {3, 0, 2}, {2, 1, 1}, {0, 0, 2}};
  Bank bk(1, {0, 1, 0}, Avaliable, Max, Allocation);
  bk.fpzy();

  return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值