Skip to content

7.1 Avoidance&Detection

:material-circle-edit-outline: 约 1063 个字 :material-clock-time-two-outline: 预计阅读时间 4 分钟

ch7 Deadlocks.pdf 操作系统(本)2024-11-14 第 7-8 节

Avoidance algorithms

Resource-Allocation Graph Algorithm

资源分配图算法,使用于 Single instance of a resource type

基于资源分配图,增加了一个概念叫 Claim edge(需求边),指示一个 process 可能会请求一个 resource,用虚线 dashed line

转化规则如下:

  1. Claim edge converts to request edge(请求边) when the process requests resource
  2. Request edge converted to an assignment edge (分配边) when the resource is allocated to the process
  3. When a resource is released by a process, assignment edge reconverts to a claim edge

[!NOTE] 算法

进程 Pi 申请资源 Rj 时,会形成一条需求边

我们尝试将需求边 Pi → Rj 变成分配边 Rj → Pi 后,检查会不会形成环

如果没有环存在,那么资源分配会使系统处于安全状态,就允许申请

否则进程必须等待

注意虚线的需求边也会参与 loop 的组成

Banker’s Algorithm

银行家算法,适用于 Multiple instances of a resource type

该算法新增 5 个数据结构

n = number of processes

m = number of resources types

  1. Available [j]: Vector of length m.

    • If Available [j] = k, then k instances of Rj available
  2. Max: n x m matrix.

    • If Max [i, j] = k, then process Pi may request at most k instances of Rj.
  3. Allocation: n x m matrix.

    • If Allocation [i, j] = k then Pi is currently allocated k instances of Rj
  4. Need: n x m matrix.

    • If Need [i, j] = k, then Pi may need k more instances of Rj to complete its task.
    • Need [i, j] = Max [i, j] – Allocation [i, j]
  5. Requestn x m matrix.

    If Request [i, j] = k then Pi wants k instances of Rj

[!IMPORTANT] Safety Algorithm 安全算法

银行家算法首先需要一个安全算法,用于检测一个系统当前是否处于安全状态

Let Work and Finish be vectors of length m and n

1-Initialize

  1. Work = Available 即当前空余资源总和
  2. Finish [i] = false for i = 1,2,3, …, n

2-Allocate

Find an index i such that

  1. Finish [i] = false
  2. Need [i] \(\le\) Work 即该进程需求资源总和小于当前空余资源

If no such i exists, go to step 4

3-Release

Work = Work + Allocation [i]

Finish [i] = true

go to step 2

4-Finish

If Finish [i] == true for all i,

then the system is in a safe state

[!IMPORTANT] Safety Algorithm 安全算法

  1. If Request [i] \(\le\) Need [i], go to step 2.
    • Otherwise, raise error condition, since process has exceeded its maximum claim.
  2. If Request [i] \(\le\) Available, go to step 3.
    • Otherwise Pi must wait, since resources are not available.
  3. Pretend to allocate requested resources to Pi:
    • Available = Available - Request [i];
    • Allocation [i] = Allocation [i] + Request [i];
    • Need [i] = Need [i] – Request [i]
  4. Call Safety Algorithm
    • If safe $\rightarrow $ the resources are allocated to Pi.
    • If unsafe $\rightarrow $ Pi must wait, and the old resource-allocation state is restored

[!EXAMPLE] 还没看

image-20241118153807874

image-20241120232531637

image-20241120232550864

image-20241118153902234

image-20241118153910591

image-20241118153916832

image-20241118153924882

image-20241118201007934

Review Questions

image-20241118153936178

  1. 死锁
  2. 按序分配/按需分配/银行家算法

image-20241118155735130

  1. 安全
  2. 不能

image-20241118155643417

  1. 不安全?
  2. 没说

Deadlock Detection

Single Instance of Each Resource Type

使用 wait-for graph 等待图,是资源分配图的变形

图里的 node 都是 process,Pi → Pj if Pi is waiting for Pj

定期使用算法检测是否有 cycle,如果有 cycle 就说明存在 deadlock

[!EXAMPLE] res-alloc -> wait-for

image-20241118201343939

Several Instances of a Resource Type

还是增加几个类似 banker algo 的数据结构

  1. Available: A vector of length m
    • indicates the number of available resources of each type.
  2. Allocation: An n x m matrix
    • defines the number of resources of each type currently allocated to each process.
  3. Request: An n x m matrix
    • indicates the current request of each process.
    • If Request [i, j] = k, then process Pi is requesting k more instances of resource type.

[!IMPORTANT] Detection Algorithm

Let Work be vectors of length m

Let Finish be vectors of length n

1-Initialize

Work = Available

For i = 1,2, …, n, - if Allocation [i] \(\ne\) 0, Finish [i] = false; - otherwise, Finish [i] = true

2-Find an index i such that

Finish [i] == false

Request [i] \(\le\) Work

If no such i exists, go to step 4.

3-

Work = Work + Allocation [i]

Finish [i] = true

go to step 2

4-

If Finish [i] == false, for some i, 1 \(\le\) i \(\le\) n, then the system is in deadlock state.

Moreover, if Finish [i] == false, then Pi is deadlocked.

??????????????????????????????????????????????

[!EXAMPLE]

image-20241118204225797

image-20241118204242152

Detection-Algorithm Usage

检测算法复杂度比较高,周期根据实际情况进行调整

Recovery from Deadlock

打破/解除死锁两种方法: 进程终止,抢占资源

Process Termination

两种方式

  1. Abort all deadlocked processes
  2. Abort one process at a time until the deadlock cycle is eliminated

Resource Preemption

种方式

  1. Selecting a victim,让其资源被抢占
  2. Rollback(回退)– return to some safe state, restart process for that state.

Starvation – same process may always be picked as victim.

Review Questions

image-20241118205332416

  1. 两种方式,等待图,检测算法
  2. image-20241118205736344
  3. 进程终止,资源抢占

image-20241118205805228

C,申请资源都成功可能在资源分配图形成闭环,相互抢占资源

image-20241118205944654

D,A 无法根本解决问题,B 只能缓解无法预防,C????

image-20241118210323085

D,Ⅰ 不可判读,Ⅱ 可判断,Ⅲ 可判断,Ⅳ 不可判断

image-20241118210637158

B,2N+1 = 11