Computer Operating System - Lecture 8: Deadlocks - Nguyen Thanh Son

System Model
 Deadlock Characterization
 Methods for Handling Deadlocks
 Deadlock Prevention
 Deadlock Avoidance
 Deadlock Detection
 Recovery from Deadlock
 Combined Approach to Deadlock
Handling 
pdf 42 trang xuanthi 30/12/2022 2140
Bạn đang xem 20 trang mẫu của tài liệu "Computer Operating System - Lecture 8: Deadlocks - Nguyen Thanh Son", để tải tài liệu gốc về máy hãy click vào nút Download ở trên.

File đính kèm:

  • pdfcomputer_operating_system_lecture_08_deadlocks_nguyen_thanh.pdf

Nội dung text: Computer Operating System - Lecture 8: Deadlocks - Nguyen Thanh Son

  1. Chapter’s Content  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock Avoidance  Deadlock Detection  Recovery from Deadlock  Combined Approach to Deadlock BK Handling TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 2
  2. 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).  Several cars may have to be backed up if a deadlock occurs. BK  Starvation is possible. TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering
  3. Deadlock Characterization 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. BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 6
  4. Resource-Allocation Graph (Cont.)  Process  Resource Type with 4 instances P  Pi requests instance of Rj i R j Pi  Pi is holding an instance of Rj Rj BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 8
  5. Resource Allocation Graph With A Deadlock BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 10
  6. 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. BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 12
  7. Deadlock Prevention Restrain the ways request can be made  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. BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 14
  8. 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. BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 16
  9. 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. BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 18
  10. Resource-Allocation Graph Algorithm  Claim edge Pi Rj indicated that process Pj may request resource Rj; represented by a dashed line.  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. BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 20
  11. Unsafe State In Resource-Allocation Graph BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 22
  12. Data Structures for the Banker’s Algorithm Let n = number of processes, and m = number of esources types.  Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj available.  Max: n x 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]. BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 24
  13. Resource-Request Algorithm for Process Pi Request = 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 BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 26
  14. 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 satisfies safety criteria. BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 28
  15. Deadlock Detection  Allow system to enter deadlock state  Detection algorithm  Recovery scheme BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 30
  16. Resource-Allocation Graph and Wait-for Graph Resource-Allocation Graph Corresponding wait-for graph BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 32
  17. Detection Algorithm 1. Let Work and Finish be vectors of length m and n, respectively Initialize: (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: (a) Finish[i] == false (b) Requesti Work If no such i exists, go to step 4. BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 34
  18. Example of Detection Algorithm  Five processes P0 through P4; three resource types A (7 instances), B (2 instances), and C (6 instances).  Snapshot at time T0: 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 will result in Finish[i] = true for all i. BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 36
  19. 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. BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 38
  20. 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. BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 40
  21. Traffic Deadlock for Exercise 8.4 BK TP.HCM 07-Feb-17 Faculty of Computer Science & Engineering 42