Chapter 8: Deadlocks( 死锁)

44
1 06/08/22 Chapter 8: Deadlocks 死死 System Model 系系 Deadlock Characterization 系系系系系 () Methods for Handling Deadlocks 系系系系系系 Deadlock Prevention 系系系系系 () Deadlock Avoidance 系系系系系 () Deadlock Detection 系系系系系 () Recovery from Deadlock 系系系系系 () Combined Approach to Deadlock Handling 系系 系系系系

description

Chapter 8: Deadlocks( 死锁). System Model( 系统模型) Deadlock Characterization (死锁特征) Methods for Handling Deadlocks (处理死锁的方法) Deadlock Prevention (死锁预防) Deadlock Avoidance (死锁避免) Deadlock Detection (死锁检测) Recovery from Deadlock (死锁恢复) Combined Approach to Deadlock Handling (综合处理方法). - PowerPoint PPT Presentation

Transcript of Chapter 8: Deadlocks( 死锁)

Page 1: Chapter 8:  Deadlocks( 死锁)

1 04/20/23

Chapter 8: Deadlocks (死锁) System Model (系统模型) Deadlock Characterization (死锁特征) Methods for Handling Deadlocks (处理死锁的方

法) Deadlock Prevention (死锁预防) Deadlock Avoidance (死锁避免) Deadlock Detection (死锁检测) Recovery from Deadlock (死锁恢复) Combined Approach to Deadlock Handling (综合处理

方法)

Page 2: Chapter 8:  Deadlocks( 死锁)

2 04/20/23

The Deadlock Problem 死锁问题

A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set.

(一组等待的进程,其中每一个进程都持有资源,并且等待着由这个组中其他进程所持有的资源)

Page 3: Chapter 8:  Deadlocks( 死锁)

3 04/20/23

The Deadlock Problem

Example (例如) System has 2 tape drives. (系统有两个磁带设备) P1 and P2 each hold one tape drive and each needs

another one. (进程 P1 和 P2 各占有一个磁带设备并且实际需要两个磁带)

Example semaphores A and B, initialized to 1 (信号量 A , B 初

始化为 1 ) P0 P1

wait (A); wait(B)wait (B); wait(A)

Page 4: Chapter 8:  Deadlocks( 死锁)

4 04/20/23

Bridge Crossing Example (过桥的例子)

Traffic only in one direction. (每次只有一个方向通行)

Each section of a bridge can be viewed as a resource.(桥的每一头都可以看成资源)

If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback).(如果死锁发生,它可以由一辆车返回而解决,抢占资源并回退)

Page 5: Chapter 8:  Deadlocks( 死锁)

5 04/20/23

Bridge Crossing Example

Several cars may have to be backed upif a deadlock occurs.(如果死锁发生,可能很多车都不得不返回)

Starvation is possible. (有可能产生饥饿) 注意饥饿与死锁的区别: 饥饿一般不占有资源,死锁进程一定占有

资源。

Page 6: Chapter 8:  Deadlocks( 死锁)

6 04/20/23

System Model (系统模型)

Resource types (资源类型) R1, R2, . . ., Rm

( CPU 周期,内存空间, I/O 设备) Each resource type Ri has Wi instances.

(每一种资源 Ri 有 Wi 种实例) Each process utilizes a resource as follows

(每一个进程如下的利用资源) request (申请) use (使用) Release (释放)

Page 7: Chapter 8:  Deadlocks( 死锁)

7 04/20/23

Deadlock Characterization(死锁的特性)

Deadlock can arise if four conditions hold simultaneously.

(四个条件同时出现,死锁将会发生)

Page 8: Chapter 8:  Deadlocks( 死锁)

8 04/20/23

Deadlock can arise if four conditions hold simultaneously

Mutual exclusion: only one process at a time can use a resource. (互斥:一次只有一个进程可以使用一个资源) Hold and wait: a process holding at least one resource is waiting to

acquire additional resources held by other processes.(占有并等待:一个至少持有一个资源的进程等待获得额外的由其他进程所持有的资源)

No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task.(不可抢占:一个资源只有当持有它的进程完成任务后,自由的释放)

Circular wait: there exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting for a resource that is held by   P1, P1   is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and P0 is waiting for a resource that is held by P0.

 (循环等待:等待资源的进程之间存在环)

Page 9: Chapter 8:  Deadlocks( 死锁)

9 04/20/23

Resource-Allocation Graph(资源分配图)

A set of vertices V and a set of edges E. (一个顶点的集合 V 和边的集合 E )V is partitioned into two types: ( V 被分为两个部分)

P = {P1, P2, …, Pn}, the set consisting of all the processes in the system.

 ( P :含有系统中全部的进程) R = {R1, R2, …, Rm}, the set consisting of all resource types in

the system.

 ( R :含有系统中全部的资源) request edge – directed edge P1 Rj

 (请求边:直接 P1 Rj ) assignment edge – directed edge Rj Pi

 (分配边: P1 Rj )

Page 10: Chapter 8:  Deadlocks( 死锁)

10 04/20/23

Resource-Allocation Graph (Cont.)资源分配图续

Process 进程

Resource Type with 4 instances 有四个实例的资源类型

Pi requests instance of Rj ( Pi 请求一个 Rj 的实例)

Pi is holding an instance of Rj ( Pi 持有一个 Rj 的实例)

Pi

PiRj

Rj

Page 11: Chapter 8:  Deadlocks( 死锁)

11 04/20/23

Example of a Resource Allocation Graph

Page 12: Chapter 8:  Deadlocks( 死锁)

12 04/20/23

Resource Allocation Graph With A Deadlock有死锁的资源分配图

Page 13: Chapter 8:  Deadlocks( 死锁)

13 04/20/23

Resource Allocation Graph With A Cycle But No Deadlock

有环但没有死锁的资源分配图

Page 14: Chapter 8:  Deadlocks( 死锁)

14 04/20/23

Basic Facts (基本事实) If graph contains no cycles no deadlock. (如果图没有环,那么不会有死锁) If graph contains a cycle (如果图有环)

if only one instance per resource type, then deadlock.

(如果每一种资源类型只有一个实例,那么死锁发生)

if several instances per resource type, possibility of deadlock.

(如果一种资源类型有多个实例,可能死锁)

Page 15: Chapter 8:  Deadlocks( 死锁)

15 04/20/23

Methods for Handling Deadlocks处理死锁的方法

Ensure that the system will never enter a deadlock state.

(确保系统永远不会进入死锁状态)

Allow the system to enter a deadlock state and then recover.

(允许系统进入死锁状态,然后恢复系统)

Ignore the problem and pretend that deadlocks never occur

in the system; used by most operating systems, including

UNIX. (忽略这个问题,假装系统中从未出现过死锁。这个方法被大部分的操作系统采用,包括 UNIX )

Page 16: Chapter 8:  Deadlocks( 死锁)

16 04/20/23

Deadlock Prevention( 死锁的预防)

Mutual Exclusion – not required for sharable resources; must hold for nonsharable resources.(互斥:共享资源不是必须的,必须保持非共享资源)

Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources.

(占有并等待:必须保证进程申请资源的时候没有占有其他资源) Require process to request and be allocated all its resources

before it begins execution, or allow process to request resources only when the process has none.(要求进程在执行前一次申请全部的资源,只有没有占有资源时才可以分配资源)

Low resource utilization; starvation possible.(利用率低,可能出现饥饿)

Restrain the ways request can be made. (抑制死锁发生的必要条件)

Page 17: Chapter 8:  Deadlocks( 死锁)

17 04/20/23

Deadlock Prevention (Cont.)死锁的预防(续)

No Preemption – (非抢占) If a process that is holding some resources requests another resource

that cannot be immediately allocated to it, then all resources currently being held are released.

 如果一个持有资源的进程的申请没有实现,它要释放所有占有的资源 Preempted resources are added to the list of resources for which the

process is waiting.

 剥夺的资源放入进程等待资源列表中 Process will be restarted only when it can regain its old resources, as

well as the new ones that it is requesting.

进程在重新得到旧的资源的时候可以重新开始 Circular Wait – impose a total ordering of all resource types, and require

that each process requests resources in an increasing order of enumeration.

 循环等待:将所有的资源类型编序,并且要求进程按照资源编序从低到高申请资源

Page 18: Chapter 8:  Deadlocks( 死锁)

18 04/20/23

Deadlock Avoidance (死锁避免)

Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need.

 (一个简单而有效的模型要求每一个进程声明它所需要的资源的最大数)

The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition.

(死锁避免算法动态检查资源分配状态以确保不会出现循环等待的情况)

Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes.

(资源分配状态定义为可用的与已分配的资源数,和进程所需的最大资源量)

Requires that the system has some additional a priori information available. (需要系统有一些额外的信息)

Page 19: Chapter 8:  Deadlocks( 死锁)

19 04/20/23

Safe State (安全状态)

When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state.(当进程申请一个有效的资源的时候,系统必须确定分配后是安全的)

System is in safe state if there exists a safe sequence of all processes.

(系统处于安全态,如果存在一个安全序列)

Page 20: Chapter 8:  Deadlocks( 死锁)

20 04/20/23

Safe State (安全状态) Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j<I.(进程序列是安全的,如果每一个进程 Pi所申请的可以被满足的资源数加上其他进程所持有的该资源数小于系统总数) If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished.

When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate.

When Pi terminates, Pi+1 can obtain its needed resources, and so on.

Page 21: Chapter 8:  Deadlocks( 死锁)

21 04/20/23

Basic Facts (基本事实) If a system is in safe state no deadlocks.(如果一个系统在安全状态,就没有死锁)

If a system is in unsafe state possibility of deadlock.(如果一个系统不是处于安全状态,就有可能死锁)

Avoidance ensure that a system will never enter an unsafe state. (死锁的避免:确保系统永远不会进入不安全状态)

Page 22: Chapter 8:  Deadlocks( 死锁)

22 04/20/23

Safe, unsafe , deadlock state spaces安全、不安全、死锁状态空间

Page 23: Chapter 8:  Deadlocks( 死锁)

23 04/20/23

Resource-Allocation Graph Algorithm资源分配图算法

Claim edge Pi Rj indicated that process Pj may request resource Rj; represented by a dashed line.

( 边 Pi Rj 代表进程 Pi 申请资源 Ri ,表示为虚线) Claim edge converts to request edge when a process

requests a resource.

 (一个进程申请资源的时候连一个边) When a resource is released by a process, assignment edge

reconverts to a claim edge.

 (当资源被释放的时候,取消边) Resources must be claimed a priori in the system.

(系统中的资源必须被声明为一个 priori )

Page 24: Chapter 8:  Deadlocks( 死锁)

24 04/20/23

Resource-Allocation Graph For Deadlock Avoidance

死锁避免的资源分配图

Page 25: Chapter 8:  Deadlocks( 死锁)

25 04/20/23

Unsafe State In A Resource-Allocation Graph不安全的状态图

Page 26: Chapter 8:  Deadlocks( 死锁)

26 04/20/23

Banker’s Algorithm (银行家算法) Multiple instances.(多个实例) Each process must a priori claim maximum use.(每一个进程必须事先声明使用的最大量)

When a process requests a resource it may have to wait. (当一个进程请求资源,它可能要等待)

When a process gets all its resources it must return them in a finite amount of time.(当一个进程得到所有的资源,它必须在有限的时间释放它们)

Page 27: Chapter 8:  Deadlocks( 死锁)

27 04/20/23

Data Structures for the Banker’s Algorithm

银行家算法的数据结构

Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj available.

可用资源向量 Max: n m matrix. If Max [i,j] = k, then process Pi may request at most k

instances of resource type Rj.

最大需求矩阵 Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated

k instances of Rj. 请求矩阵 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].

Let n = number of processes, and

m = number of resources types.

Page 28: Chapter 8:  Deadlocks( 死锁)

28 04/20/23

Safety Algorithm (安全算法)1. Let Work and Finish be vectors of length m and n, respectively. Initialize

(让 Work 和 Finish 作为长度为 m 和 n 的向量)Work := Available

Finish [i] = false for i - 1,3, …, n.

2. Find and i such that both: (找到 i )(a) Finish [i] = false

(b) Needi Work

If no such i exists, go to step 4.

3. Work := Work + Allocationi

Finish[i] := truego to step 2.

4. If Finish [i] = true for all i, then the system is in a safe state.

Page 29: Chapter 8:  Deadlocks( 死锁)

29 04/20/23

Resource-Request Algorithm for Process Pi

进程 Pi 的资源请求

Requesti = request vector for process Pi. If   Requesti [j] = k then process Pi wants k instances of resource type Rj.

1. If Requesti Needi go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim.

2. If Requesti Available, go to step 3. Otherwise Pi must wait, since resources are not available.

3. Pretend to allocate requested resources to Pi by modifying the state as follows:

Available := Available - Requesti;

Allocationi := Allocationi + Requesti;

Needi := Needi – Requesti;;

• If safe the resources are allocated to Pi.

• If unsafe Pi must wait, and the old resource-allocation state is restored

Page 30: Chapter 8:  Deadlocks( 死锁)

30 04/20/23

Example of Banker’s Algorithm银行家算法的例子-判断系统是否为安全状态

5 processes P0 through P4; 3 resource types A (10 instances), B (5instances, and C (7 instances). ( 5 个进程 P0 到 P4:3 个资源类型A(10 个实例), B ( 5 个实例), C ( 7 个实例))

Snapshot at time T0: (时刻 Tn 的片段)Allocation Max Available

A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2

P1 2 0 0 3 2 2

P2 3 0 2 9 0 2

P3 2 1 1 2 2 2

P4 0 0 2 4 3 3

Page 31: Chapter 8:  Deadlocks( 死锁)

31 04/20/23

Example (Cont.)(例子续:判断系统是否为安全状态)剩余需求矩阵:

Need

A B C

P0 7 4 3

P1 1 2 2

P2 6 0 0

P3 0 1 1

P4 4 3 1

The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria. (系统在安全的状态因为序列P1,P3,P4,P2,P0满足了安全的标准)

Page 32: Chapter 8:  Deadlocks( 死锁)

32 04/20/23

Example (Cont.): 例子续  P1 request (1,0,2)

Check that Request Available , P1 request (1,0,2)

  (that is, (1,0,2) (3,3,2) true.

Allocation Need Available

A B C A B C A B C

P0 0 1 0 7 4 3 2 3 0

P1 3 0 2 0 2 0

P2 3 0 1 6 0 0

P3 2 1 1 0 1 1

P4 0 0 2 4 3 1

Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2> satisfies safety requirement.

 (执行安全算法表明序列 p1,p3,p4,p0,p2 满足要求) Can request for (3,3,0) by P4 be granted? ( p4 的 (3,3,0) 可以通过?) Can request for (0,2,0) by P0 be granted? ( pn 的 (0,2,0) 可以通过?)

Page 33: Chapter 8:  Deadlocks( 死锁)

33 04/20/23

Deadlock Detection (死锁检测)

Allow system to enter deadlock state (允许进入死锁状态)

Detection algorithm (检测死锁) Recovery scheme (恢复策略)

Page 34: Chapter 8:  Deadlocks( 死锁)

34 04/20/23

Single Instance of Each Resource Type每一种资源类型只有一个实例

Maintain wait-for graph (维护等待图) Nodes are processes. (节点是进程) Pi Pj if Pi is waiting for Pj.

( Pi Pj 表明 Pi 在等待 Pj. ) Periodically invoke an algorithm that searches for acycle

in the graph. (定期调用算法来检查是否有环) An algorithm to detect a cycle in a graph requires an order

of n2 operations, where n is the number of vertices in the graph. (一个检查图中是否有环的算法需要 n2 的操作来进行, n 为图中的节点数)

Page 35: Chapter 8:  Deadlocks( 死锁)

35 04/20/23

Resource-Allocation Graph And Wait-for Graph资源分配图和等待图

Resource-Allocation Graph Corresponding wait-for graph

Page 36: Chapter 8:  Deadlocks( 死锁)

36 04/20/23

Several Instances of a Resource Type一个资源类型的多个实例

Available: A vector of length m indicates the number of available resources of each type.

(可用:一个向量的长度m 代表每一种资源类型有效的数目) Allocation: An n x m matrix defines the number of resources

of each type currently allocated to each process.

(分配:一个 n x m 的矩阵定义了当前分配的每一种资源类型的实例数目)

Request: An n x m matrix indicates the current request of each process. If Request [ij] = k, then process Pi is requesting k more instances of resource type. Rj.

(请求:一个 n x m 的矩阵使命了当前的进程请求。如果Request[ij]=k, 那么进程 Pi 请求 k 个资源 Rj 的实例)

Page 37: Chapter 8:  Deadlocks( 死锁)

37 04/20/23

Detection Algorithm( 死锁检测算法)

1.Let Work and Finish be vectors of length m and n, respectively Initialize( 让 Work 和 Finish 作为长度为 m 和 n 的向量)(a) Work : = Available

(b)For i = 1,2, …, n, if Allocationi 0, then Finish[i] := false;otherwise, Finish[i] := true.

2. Find an index i such that both (找到下标 i )(a) Finish[i] = false

(b)Requesti Work

If no such i exists, go to step 4. (如果没有这样的 i 存在,转 4 )

Page 38: Chapter 8:  Deadlocks( 死锁)

38 04/20/23

Detection Algorithm (Cont.)

3. Work := Work + Allocationi

Finish[i] := truego to step 2.

4. If Finish[i] = false, for some i, 1 i n, then the system is in deadlock state. Moreover, if Finish[i] = false, then Pi is deadlocked.

Algorithm requires an order of m x n2 operations to detect whether the system is in deadlocked state.

算法需要 m x n2 的操作来判断是否系统处于死锁状态

Page 39: Chapter 8:  Deadlocks( 死锁)

39 04/20/23

Example of Detection Algorithm检测死锁算法的例子

Five processes P0 through P4; three resource types A (7 instances), B (2 instances), and C (6 instances). (五个进程 pn 到 p4, 三个资源类型 A ( 7 个实例), B ( 2 个实例) ,C ( 6 个实例)

Snapshot at time T0 (时刻 Tn 的状态)Allocation Request Available

A B C A B C A B C

P0 0 1 0 0 0 0 0 0 0

P1 2 0 0 2 0 2

P2 3 0 3 0 0 0

P3 2 1 1 1 0 0

P4 0 0 2 0 0 2

Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i.

Page 40: Chapter 8:  Deadlocks( 死锁)

40 04/20/23

Example (Cont.) (例子续) P2 requests an additional instance of type C. ( P2 请求 C 的实例)

Request

A B C

P0 0 0 0

P1 2 0 1

P2 0 0 1

P3 1 0 0

P4 0 0 2

State of system? (系统的状态) Can reclaim resources held by process P0, but insufficient resources

to fulfill other processes; requests.

(可以归还 Pn 所有的资源,但是资源不够完成其他进程的请求) Deadlock exists, consisting of processes P1, P2, P3, and P4.

(死锁存在,包括进程 P1,P2,P3 和 P4)

Page 41: Chapter 8:  Deadlocks( 死锁)

41 04/20/23

Detection-Algorithm Usage检测算法的应用

When, and how often, to invoke depends on:

(何时应用死锁检测方法,取决于) How often a deadlock is likely to occur?

(死锁可能发生的频率是多少) How many processes will need to be rolled back?

(多少进程可能需要回滚) one for each disjoint cycle (每一个独立的环需要一个)

If detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we would not be able to tell which of the many deadlocked processes “caused” the deadlock.

(如果检测算法被随意的调用,可能图中存在很多的环以至于我们无法判断是哪一个进程引起了死锁的发生)

Page 42: Chapter 8:  Deadlocks( 死锁)

42 04/20/23

Recovery from Deadlock: Process Termination从死锁中恢复:进程终止

Abort all deadlocked processes.

(中断所有的死锁进程) Abort one process at a time until the deadlock cycle is eliminated.

(一次中断一个进程知道死锁环消失) In which order should we choose to abort? (选择中断顺序)

Priority of the process. (进程的优先级 ) How long process has computed, and how much longer to completion.

(多少个进程需要计算,需要多长时间) Resources the process has used. (进程使用的资源) Resources process needs to complete.

(进程完成还需要多少资源) How many processes will need to be terminated.

(多少个进程需要被中断) Is process interactive or batch?

(进程是交互的还是批处理)

Page 43: Chapter 8:  Deadlocks( 死锁)

43 04/20/23

Recovery from Deadlock: Resource Preemption

从死锁中恢复:资源优先级 Selecting a victim – minimize cost.

(选择一个:最小化代价) Rollback – return to some safe state, restart process

fro that state.

(回退:返回到安全的状态,然后重新开始进程) Starvation – same process may always be picked as

victim, include number of rollback in cost factor.

(饥饿:同样进程的可能总是被选中)

Page 44: Chapter 8:  Deadlocks( 死锁)

44 04/20/23

Combined Approach to Deadlock Handling综合死锁处理方式

Combine the three basic approaches

(把三种方式组合起来) Prevention (预防) Avoidance (避免) Detection (检测)

allowing the use of the optimal approach for each of resources in the system. (允许使用优化的方式来使用系统中的资源)

Partition resources into hierarchically ordered classes.

(把资源划分为不同的等级) Use most appropriate technique for handling deadlocks within

each class. (对每一类使用适当的技术来处理死锁)