1 / 44

Chapter 8: Deadlocks( 死锁)

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 (综合处理方法).

Télécharger la présentation

Chapter 8: Deadlocks( 死锁)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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(综合处理方法) 110/31/2014

  2. 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. (一组等待的进程,其中每一个进程都持有资源,并且等待着由这个组中其他进程所持有的资源) 210/31/2014

  3. 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) 310/31/2014

  4. 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). (如果死锁发生,它可以由一辆车返回而解决,抢占资源并回退) 410/31/2014

  5. Bridge Crossing Example • Several cars may have to be backed upif a deadlock occurs. (如果死锁发生,可能很多车都不得不返回) • Starvation is possible.(有可能产生饥饿) • 注意饥饿与死锁的区别:  饥饿一般不占有资源,死锁进程一定占有资源。 510/31/2014

  6. 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(释放) 610/31/2014

  7. Deadlock Characterization(死锁的特性) • Deadlock can arise if four conditions hold simultaneously. (四个条件同时出现,死锁将会发生) 710/31/2014

  8. 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 byP1, P1is 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.  (循环等待:等待资源的进程之间存在环) 810/31/2014

  9. 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) 910/31/2014

  10. Resource-Allocation Graph (Cont.)资源分配图续 • Process进程 • Resource Type with 4 instances有四个实例的资源类型 • Pirequests instance of Rj (Pi请求一个Rj的实例) • Pi is holding an instance of Rj(Pi持有一个Rj的实例) Pi Rj Pi Rj 1010/31/2014

  11. Example of a Resource Allocation Graph 1110/31/2014

  12. Resource Allocation Graph With A Deadlock有死锁的资源分配图 1210/31/2014

  13. Resource Allocation Graph With A Cycle But No Deadlock有环但没有死锁的资源分配图 1310/31/2014

  14. 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. (如果一种资源类型有多个实例,可能死锁) 1410/31/2014

  15. 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) 1510/31/2014

  16. 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.(抑制死锁发生的必要条件) 1610/31/2014

  17. 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.  循环等待:将所有的资源类型编序,并且要求进程按照资源编序从低到高申请资源 1710/31/2014

  18. Deadlock Avoidance(死锁避免) Requires that the system has some additional a priori information available.(需要系统有一些额外的信息) • 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. (资源分配状态定义为可用的与已分配的资源数,和进程所需的最大资源量) 1810/31/2014

  19. 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. • (系统处于安全态,如果存在一个安全序列) 1910/31/2014

  20. 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 Pjhave 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. 2010/31/2014

  21. 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. (死锁的避免:确保系统永远不会进入不安全状态) 2110/31/2014

  22. Safe, unsafe , deadlock state spaces安全、不安全、死锁状态空间 2210/31/2014

  23. Resource-Allocation Graph Algorithm资源分配图算法 • Claim edgePi 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) 2310/31/2014

  24. Resource-Allocation Graph For Deadlock Avoidance死锁避免的资源分配图 2410/31/2014

  25. Unsafe State In A Resource-Allocation Graph不安全的状态图 2510/31/2014

  26. 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. (当一个进程得到所有的资源,它必须在有限的时间释放它们) 2610/31/2014

  27. Data Structures for the Banker’s Algorithm银行家算法的数据结构 • Available: Vector of length m. If available [j] = k, there are k instances of resource type Rjavailable. 可用资源向量 • Max: n m matrix. If Max [i,j] = k, then process Pimay 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 Rjto complete its task.剩余需求矩阵 Need [i,j] = Max[i,j] – Allocation [i,j]. Let n = number of processes, and m = number of resources types. 2710/31/2014

  28. 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 + AllocationiFinish[i] := truego to step 2. 4. If Finish [i] = true for all i, then the system is in a safe state. 2810/31/2014

  29. 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 Needigo 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 2910/31/2014

  30. 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的片段) AllocationMaxAvailable 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 3010/31/2014

  31. 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满足了安全的标准) 3110/31/2014

  32. 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. AllocationNeedAvailable 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)可以通过?) 3210/31/2014

  33. Deadlock Detection(死锁检测) • Allow system to enter deadlock state (允许进入死锁状态) • Detection algorithm(检测死锁) • Recovery scheme(恢复策略) 3310/31/2014

  34. Single Instance of Each Resource Type每一种资源类型只有一个实例 • Maintain wait-for graph(维护等待图) • Nodes are processes.(节点是进程) • Pi Pj if Piis 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为图中的节点数) 3410/31/2014

  35. Resource-Allocation Graph And Wait-for Graph资源分配图和等待图 Resource-Allocation Graph Corresponding wait-for graph 3510/31/2014

  36. 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的实例) 3610/31/2014

  37. 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) 3710/31/2014

  38. Detection Algorithm (Cont.) 3. Work := Work + AllocationiFinish[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 的操作来判断是否系统处于死锁状态 3810/31/2014

  39. 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. 3910/31/2014

  40. 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) 4010/31/2014

  41. 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. (如果检测算法被随意的调用,可能图中存在很多的环以至于我们无法判断是哪一个进程引起了死锁的发生) 4110/31/2014

  42. 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? (进程是交互的还是批处理) 4210/31/2014

  43. 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. (饥饿:同样进程的可能总是被选中) 4310/31/2014

  44. 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.(对每一类使用适当的技术来处理死锁) 4410/31/2014

More Related