[OS] Banker's Algorithm

security sequence

If the system allocates resources in this sequence, each process can complete smoothly. As long as a safe sequence can be found, the system is in a safe state.

当然,安全序列可能有多个

popular understanding model

At this point you are a successful banker with 10 billion in your hand...

At this time, there are three companies that want to ask you for a loan, namely company B, company A, and company T

B: "Brother, I want to borrow up to 7 billion"

A: "Brother, I want to borrow up to 4 billion"

T: "Brother, I want to borrow up to 5 billion"

There is a rule: if the money lent to a company fails to meet the maximum requirements put forward by the company, then the money you borrowed will be in vain

Of course, you don’t want your money to be wasted, so you have to consider how to borrow to ensure that your 10 billion will not be wasted

initial loan

maximum requirement already borrowed Borrow at most
B 70 20 50
A 40 10 30
T 50 30 20

此时你手里还有40亿

Analyze the security sequence of borrowing money

  1. At this time B wants to borrow 3 billion from you, do you dare to borrow it?

    1. If you agree: lent B 3 billion, 那么你的手里还有10亿, the above picture is slightly modified, as shown below:

    2. maximum requirement already borrowed Borrow at most
      B 70 20+30=50 50-30=20
      A 40 10 30
      T 50 30 20
    3. If another company proposes to borrow another 2 billion, then you will be dead, obviously you can’t borrow, and your money is wasted, so this money cannot be borrowed.不安全

  2. At this time, A wants to borrow 2 billion from you, do you dare to borrow it?

    1. If you agree: lent A 2 billion, 那么你的手里还有20亿, the above picture is slightly modified, as shown below:

    2. maximum requirement already borrowed Borrow at most
      B 70 20 50
      A 40 10+20=30 30-20=10
      T 50 30 20
    3. Next, you can lend all the 2 billion to T company. When he returns all the money, he will have 5 billion in his hand, and then lend the money to Company B. When he returns all the money, he will have 7 billion in his hand, and finally he will lend it to Company A. This way you get all your money back.

    4. So this loan sequence (safe sequence): T->B->A

    5. Verify this sequence by yourself according to the above ideas: A->T->B

banker's algorithm

main idea

When a process makes a resource application, first check 预判whether this allocation will cause the system to enter 不安全状态. If it will enter an unsafe state, the request will not be granted temporarily, and the process will be blocked and waited first.

The core is: security algorithm

resource representation

Expand single-dimensional numbers into multidimensional vectors. For example: There are 5 processes P0~P4 in the system, 3 kinds of resources R0~R2, the initial number is (10, 5, 7)

process greatest need Already assigned need at most
P0 (7,5,3) (0,1,0) (7,4,3)
P1 (3,2,2) (2,0,0) (1,2,2)
P2 (9,0,2) (3,0,2) (6,0,0)
P3 (2,2,2) (2,1,1) (0,1,1)
P4 (4,3,3) (0,0,2) (4,3,1)

At this point (7,2,5) has been allocated, and (3,3,2) is left

Security Algorithm Analysis System Status

  1. Is the system in a safe state at this time? If so, find a safe sequence.

    1. First, check whether the remaining resources meet the needs of each process

    2. We found that it can meet the needs of P1 and P3. Then assign it to P1 first (add P1 to the security sequence), wait for him to end, then the remaining resources will become (5,3,2); then allocate to P3 (add P3 to the security sequence), wait for him to end, the remaining resources will be becomes (7,4,3). As shown below:

    3. process greatest need Already assigned need at most
      P0 (7,5,3) (0,1,0) (7,4,3)
      P2 (9,0,2) (3,0,2) (6,0,0)
      P4 (4,3,3) (0,0,2) (4,3,1)
    4. Add P4, P2, P0 (the maximum number of resources they need is less than the remaining resources) into the security sequence, and finally a security sequence can be obtained.安全性算法

  2. In the case of actually doing the problem (hand calculation), a faster method can be used to find a safe sequence

    1. (3, 3, 2) can satisfy P1 and P3, which means that in any case, the resource requirements of these two processes must be satisfied in turn, so P1 and P3 must be successfully executed and the resources returned. P1 and P3 can be added to the security sequence first. (2, 0, 0) + (2, 1, 1) + (3, 3, 2) = (7, 4, 3) The remaining P0
      , P2, P4 can be satisfied. Similarly, these processes can all join the security sequence. Therefore, all five processes are added to the security sequence, as explained 此时系统处于安全状态, 暂不可能发生死锁.
  3. special: No safe sequence instance found

    1. process greatest need Already assigned need at most
      P0 (8, 5, 3) (0, 1, 0) (8, 4, 3)
      P1 (3,2,2) (2,0,0) (1,2,2)
      P2 (9, 5 ,2) (3, 0, 2) (6, 5, 0)
      P3 (2,2,2) (2,1,1) (0,1,1)
      P4 (4, 3, 6) (0, 0, 2) (4, 3, 4)
    2. Total resources (10,5,7), remaining available resources (3,3,2)

    3. (3, 3, 2) P1 and P3 can be satisfied, P1 and P3 can be added to the security sequence first, and the remaining available resources are (7, 4, 3)

    4. process greatest need Assigned need at most
      P0 (8, 5, 3) (0, 1, 0) (8, 4, 3)
      P2 (9, 5 ,2) (3, 0, 2) (6, 5, 0)
      P4 (4, 3, 6) (0, 0, 2) (4, 3, 4)
    5. The rest can't be satisfied, every bit contrasted. The remaining P0 needs (8, 4, 3) (7, 4, 3) P2 needs (6, 5, 0) (7, 4, 3) P4 needs (4, 3, 4)

    6. 无法找到任何一个安全序列, 说明此时系统处于不安全状态, 有可能发生死锁

下面进入正题:银行家算法的实现

银行家算法实现

进程 最大需求Max 已分配Allocation 最多还需要Need
P0 (7, 5, 3) (0, 1, 0) (7, 4, 3)
P1 (3, 2, 2) (2, 0, 0) (1, 2, 2)
P2 (9, 0 ,2) (3, 0, 2) (6, 0, 0)
P3 (2, 2, 2) (2, 1, 1) (0, 1, 1)
P4 (4, 3, 3) (0, 0, 2) (4, 3, 1

数据结构:

n*m 矩阵 Max 表示各进程对资源的最大需求数

n*m 矩阵 Allocation 表示已经给各进程分配 了多少资源

Max – Allocation = Need 矩阵表示各进程最多还需要多少资源

长度为 m 的一维数组 Available 表示还有多少可用资源 [表示:Available = (3, 3, 2)]

用长度为 m 的一位数组 Requesti表示进程此次申请的各种资源数 [表示:Request0 = (2, 1, 1) ]

思路分析

  1. 如果 Requesti[j]≤Need[i, j] (0≤j≤m)便转向 2 ; 否则认为出错 :因为它所需要的资源数已超过它所宣布的最大值

  2. 如果 Requesti[j]≤Available[j] (0≤j≤m), 便转向 3 ; 否则表示尚无足够资源, Pi必须等待

  3. 系统试探着把资源分配给进程Pi, 并修改相应的数据(并非真的分配, 修改数值只是为了做预判

    Available = Available - Requesti;
    Allocation[i, j] = Allocation[i, j] + Requesti[j];
    Need[i, j] = Need[i, j]Requesti[j]  
    
  4. 操作系统执行安全性算法, 检查此次资源分配后, 系统是否处于安全状态。 若安全, 才正式分配; 否则, 恢复相
    应数据, 让进程阻塞等待

银行家算法步骤

  1. 检查此次申请是否超过了之前声明的最大需求数
  2. 检查此时系统剩余的可用资源是否还能满足这次请求\
  3. 试探着分配, 更改各数据结构
  4. 用安全性算法检查此次分配是否会导致系统进入不安全状态

安全性算法步骤

检查当前的剩余可用资源是否能满足某个进程的最大需求, 如果可以, 就把该进程加入安全序列,并把该进程持有的资源全部回收。
不断重复上述过程, 看最终是否能让所有进程都加入安全序列。

系统处于不安全状态未必死锁, 但死锁时一定处于不安全状态。 系统处于安全状态一定不会死锁

升华思维

死锁预防是保证系统不进入死锁状态的静态策略,其解决办法是破坏产生死锁的四个必要条件之一、下列方法中 破坏了“循环等待”条件的是( )。

A. 银行家算法
B. 一-次性分配策略
C. 剥夺资源法
D. 资源有序分配策略

产生死锁的四个必要条件:互斥、不剥夺、请求和保持、循环等待
资源有序分配策略可以限制循环等待条件的发生。选项A判断是否为不安全状态;选项B破坏了占有请求条件;选项C破坏了非剥夺条件。

某系统中有三个并发进程都需要四个同类资源,则该系统必然不会发生死锁的最少资源是( )。

A. 9
B. 10
C. 11
D. 12

资源数为9时,存在三个进程都占有三个资源,为死锁;资源数为10 时,必然存在一个进程能拿到4个资源,然后可以顺利执行完其他进程。

在这里插入图片描述

A. Ⅱ、Ⅲ

B. Ⅰ、Ⅱ

C. Ⅰ

D. Ⅰ、Ⅲ

在这里插入图片描述

Guess you like

Origin blog.csdn.net/qq_43216714/article/details/124484094