Computer Operating System - Chapter 8: Deadlocks

System Model
! Deadlock in Multithreaded Applications
! Deadlock Characterization
! Methods for Handling Deadlocks
! Deadlock Prevention
! Deadlock Avoidance
! Deadlock Detection
! Recovery from Deadlock 
pdf 47 trang xuanthi 30/12/2022 3140
Bạn đang xem 20 trang mẫu của tài liệu "Computer Operating System - Chapter 8: Deadlocks", để 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_chapter_8_deadlocks.pdf

Nội dung text: Computer Operating System - Chapter 8: Deadlocks

  1. Chapter 8: Outline ! System Model ! Deadlock in Multithreaded Applications ! Deadlock Characterization ! Methods for Handling Deadlocks ! Deadlock Prevention ! Deadlock Avoidance ! Deadlock Detection ! Recovery from Deadlock Operating System Concepts 2 Silberschatz, Galvin and Gagne ©2018
  2. System Model ! System consists of resources ! Resource types R1, R2, . . ., Rm CPU cycles, memory space, I/O devices ! Each resource type Ri has Wi instances. ! Each process utilizes a resource as follows: " request " use " release Operating System Concepts 4 Silberschatz, Galvin and Gagne ©2018
  3. Deadlock in Multithreaded Application Operating System Concepts 6 Silberschatz, Galvin and Gagne ©2018
  4. 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, , 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. Operating System Concepts 8 Silberschatz, Galvin and Gagne ©2018
  5. Resource Allocation Graph Example ! One instance of R1 ! Two instances of R2 ! One instance of R3 ! Three instance of R4 ! T1 holds one instance of R2 and is waiting for an instance of R1 ! T2 holds one instance of R1, one instance of R2, and is waiting for an instance of R3 ! T3 is holds one instance of R3 Operating System Concepts 10 Silberschatz, Galvin and Gagne ©2018
  6. Graph With A Cycle But No Deadlock Operating System Concepts 12 Silberschatz, Galvin and Gagne ©2018
  7. Methods for Handling Deadlocks ! Ensure that the system will never enter a deadlock state: " Deadlock prevention " Deadlock avoidance ! Allow the system to enter a deadlock state and then recover ! Ignore the problem and pretend that deadlocks never occur in the system. Operating System Concepts 14 Silberschatz, Galvin and Gagne ©2018
  8. 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 Operating System Concepts 16 Silberschatz, Galvin and Gagne ©2018
  9. 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 Operating System Concepts 18 Silberschatz, Galvin and Gagne ©2018
  10. 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. Operating System Concepts 20 Silberschatz, Galvin and Gagne ©2018
  11. Avoidance Algorithms ! Single instance of a resource type " Use a resource-allocation graph ! Multiple instances of a resource type " Use the Banker’s Algorithm Operating System Concepts 22 Silberschatz, Galvin and Gagne ©2018
  12. Resource-Allocation Graph Operating System Concepts 24 Silberschatz, Galvin and Gagne ©2018
  13. 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 Operating System Concepts 26 Silberschatz, Galvin and Gagne ©2018
  14. Data Structures for the Banker’s Algorithm ! Let n = number of processes, and m = number of resources 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] Operating System Concepts 28 Silberschatz, Galvin and Gagne ©2018
  15. 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 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 Operating System Concepts 30 Silberschatz, Galvin and Gagne ©2018
  16. 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 Operating System Concepts 32 Silberschatz, Galvin and Gagne ©2018
  17. Deadlock Detection ! Allow system to enter deadlock state ! Detection algorithm ! Recovery scheme Operating System Concepts 34 Silberschatz, Galvin and Gagne ©2018
  18. Resource-Allocation Graph and Wait-for Graph Resource-Allocation Graph Corresponding wait-for graph Operating System Concepts 36 Silberschatz, Galvin and Gagne ©2018
  19. 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 Operating System Concepts 38 Silberschatz, Galvin and Gagne ©2018
  20. 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 Operating System Concepts 40 Silberschatz, Galvin and Gagne ©2018
  21. 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? 4 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. Operating System Concepts 42 Silberschatz, Galvin and Gagne ©2018
  22. 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 Operating System Concepts 44 Silberschatz, Galvin and Gagne ©2018
  23. Summary (Cont.) ! Deadlock can be avoided by using the banker’s algorithm, which does not grant resources if doing so would lead the system into an unsafe state where deadlock would be possible. ! Adeadlock-detection algorithm can evaluate processes and resources on a running system to determine if a set of processes is in a deadlocked state. ! If deadlock does occur, a system can attempt to recover from the deadlock by either aborting one of the processes in the circular wait or preempting resources that have been assigned to a deadlocked process. Operating System Concepts 46 Silberschatz, Galvin and Gagne ©2018