1 / 35

Chapter 7: Deadlocks

Chapter 7: Deadlocks. Chapter 7: Deadlocks. The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Recovery from Deadlock. Chapter Objectives.

Télécharger la présentation

Chapter 7: 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 7: Deadlocks

  2. Chapter 7: Deadlocks • The Deadlock Problem • System Model • Deadlock Characterization • Methods for Handling Deadlocks • Deadlock Prevention • Deadlock Avoidance • Recovery from Deadlock

  3. Chapter Objectives • To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks • To present a number of different methods for preventing or avoiding deadlocks in a computer system

  4. 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 • Example • System has 2 disk drives • P1 and P2 each hold one disk drive and each needs another one • Example • semaphores A and B, initialized to 1 P0P1 wait (A); wait(B) wait (B); wait(A)

  5. System Model • Resource types R1, R2, . . ., Rm :CPU cycles, memory space, I/O devices • Each resource type Ri has Wi instances (Example: two CPUs) • In normal operation a process must request a resource before using it, and release it when it is done,  in the following sequence: • request :If the request cannot be immediately granted, then the process must wait until the resource(s) it needs become available, for example the system calls open( ), malloc( ), new( ), and request( ). • Use: The process uses the resource, e.g. prints to the printer or reads from the file • Release:The process relinquishes the resource. so that it becomes available for other processes. For example, close( ), free( ), delete( ), and release( ). • The kernel keeps track of resources, Application-managed resources can be controlled using mutexes or wait( ) and signal( ) calls.

  6. Deadlock Characterization • 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, …, Pn} 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 Pn is waiting for a resource that is held by P0. Deadlock can arise if four conditions hold simultaneously.

  7. Resource-Allocation Graph The graph consists of a set of vertices V and a set of edges E. • The set of Vertices V is partitioned into two different types of nodes: • P = {P1, P2, …, Pn}, the set consisting of all the processes in the system • R = {R1, R2, …, Rm}, the set consisting of all resource types in the system • request edge – directed edge Pi  Rj: the process Pi has requested an instance of resource type Rj and is currently waiting for that resource • assignment edge – directed edge Rj Pi :an instance of resource type Rj has been allocated to process Pi Deadlock can be described more precisely in term of a directed graph called a system resource-allocation graph

  8. Resource-Allocation Graph (Cont.) • Process • Resource Type with 4 instances • Pirequests instance of Rj • Pi is holding an instance of Rj Pi Rj Pi Rj

  9. Example of a Resource Allocation Graph

  10. Resource Allocation Graph With A Deadlock

  11. Graph With A Cycle But No Deadlock

  12. 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

  13. 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

  14. Deadlock Prevention Remove the possibility of deadlock occurring by denying one of the four necessary conditions: • Mutual Exclusion: • Shared resources such as read-only files do not lead to deadlocks. • Unfortunately some resources, such as printers and tape drives, require exclusive access by a single process. • Hold and Wait – To prevent this condition processes must be prevented from holding one or more resources while simultaneously waiting for one or more others. There are several possibilities for this: • Require process to request and be allocated all its resources before it begins execution, • Require that processes holding resources must release them before requesting new resources, and then re-acquire the released resources along with the new ones in a single new request. This can be a problem if a process has partially completed an operation using a resource and then fails to get it re-allocated after releasing it • Low resource utilization; starvation possible

  15. Deadlock Prevention (Cont.) • No Preemption –Preemption of process resource allocations can prevent this condition of deadlocks, when it is possible: • One approach is that if a process is forced to wait when requesting a new resource, then all other resources previously held by this process are implicitly released, ( preempted ), forcing this process to re-acquire the old resources along with the new resources in a single request. • Another approach is that when a resource is requested and not available, then the system looks to see what other processes currently have those resources and are themselves blocked waiting for some other resource. If such a process is found, then some of their resources may get preempted and added to the list of resources for which the process is waiting. • Either of these approaches may be applicable for resources whose states are easily saved and restored, such as registers and memory, but are generally not applicable to other devices such as printers and tape drives. • Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration

  16. Deadlock Prevention (Cont.) • Circular Wait –One way to avoid circular wait is to number all resources, and to require that processes request resources only in strictly increasing ( or decreasing ) order. • In other words, in order to request resource Rj, a process must first release all Ri such that i >= j. • One big challenge in this scheme is determining the relative ordering of the different resources

  17. Deadlock Avoidance • In some algorithms the scheduler only needs to know the maximum number of each resource that a process might potentially use. In more complex algorithms the scheduler can also take advantage of the schedule of exactly what resources may be needed in what order. • When a scheduler sees (examines the resource-allocation state ) that starting a process or granting resource requests may lead to future deadlocks, then that process is just not started or the request is not granted. • A resource allocation state is defined by the number of available and allocated resources, and the maximum requirements of all processes in the system. Requires that the system has some additional a priori information available

  18. Safe State • A state is safe if the system can allocate all resources requested by all processes ( up to their stated maximums ) without entering a deadlock state. • A state is safe if the system can allocate all resources requested by all processes ( up to their stated maximums ) without entering a deadlock state. • More formally, a state is safe if there exists a safe sequence of processes { P0, P1, P2, ..., PN } such that all of the resource requests for Pi can be granted using the resources currently allocated to Pi and all processes Pj where j < i. ( I.e. if all the processes prior to Pi finish and free up their resources, then Pi will be able to finish also, using the resources that they have freed up. ) • If a safe sequence does not exist, then the system is in an unsafe state, which MAY lead to deadlock. ( All safe states are deadlock free, but not all unsafe states lead to deadlocks. )

  19. 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.

  20. Example • A system with 12 tape drives and three processes • P0 requires 10 tape drives, P1 requires4 tape drives, P2 requires 9 Maximum Needs Current Needs P0 10 5 P1 4 2 P2 9 2 • At time To the system in a safe state with seq. <P1,P0,P2>. • At time T1 suppose P2 requested an additional tape drive, then the sequence <P1,P0,P2>. Will lead to deadlock.

  21. Avoidance algorithms • Single instance of a resource type • Use a resource-allocation graph • Multiple instances of a resource type • Use the banker’s algorithm

  22. Resource-Allocation Graph Scheme • Claim edge Pi Rj indicated that process Pi may request resource Rj; represented by a dashed line • Claim edge converts to request edge when a process requests a resource • Request edge converted to an assignment edge when the resource is allocated to the process • When a resource is released by a process, assignment edge reconverts to a claim edge • Resources must be claimed a priori in the system

  23. Resource-Allocation Graph

  24. Unsafe State In Resource-Allocation Graph

  25. Resource-Allocation Graph Algorithm • Suppose that process Pi requests a resource Rj • The request can be granted only if converting the request edge to an assignment edge does not result in the formation of a cycle in the resource allocation graph

  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

  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 x 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.

  28. Safety Algorithm 1. Let Work and Finishbe vectors of length m and n, respectively. Initialize: Work = Available Finish [i] = false for i = 0, 1, …, n- 1 2. Find an i such that both: (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

  29. Resource-Request Algorithm for Process Pi Requesti = request vector for process Pi. If Requesti[j] = k then process Pi wants k instances of resource type Rj • If Requesti Needigo to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim • If Requesti Available, go to step 3. Otherwise Pi must wait, since resources are not available • 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

  30. Example of Banker’s Algorithm • 5 processes P0 through P4; 3 resource types: A (10 instances), B (5instances), and C (7 instances) Snapshot at time T0: 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

  31. Example (Cont.) • The content of the matrix Need is defined to be Max – Allocation 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

  32. Example: P1 Request (1,0,2) • Check that Request  Available (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 • Can request for (3,3,0) by P4 be granted? • Can request for (0,2,0) by P0 be granted?

  33. 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?

  34. Recovery from Deadlock: Resource Preemption • Selecting a victim – minimize cost • Rollback – return to some safe state, restart process for that state • Starvation – same process may always be picked as victim, include number of rollback in cost factor

  35. End of Chapter 7

More Related