Styresystemer og Multiprogrammering Block 3, 2005 Deadlocks Robert Glück.

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Chapter 7: Deadlocks.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 7: Deadlocks.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 7: Deadlocks.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Lecture 7: Deadlocks, Deadlock Risk Management. Lecture 7 / Page 2AE4B33OSS Silberschatz, Galvin and Gagne ©2005 Contents The Concept of Deadlock Resource-Allocation.
Deadlocks CS 3100 Deadlocks1. The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held by another.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks.
Chapter 7. Deadlocks.
Chapter 7: Deadlocks. 7.2 Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks.
1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Chapter 8: Deadlocks System Model Deadlock Characterization
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
Deadlocks Gordon College Stephen Brinton. Deadlock Overview The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 7: Deadlock Dr. Mohamed Hefeeda.
What we will cover…  The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Objectives Understand the Deadlock.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Deadlocks.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks.
Chapter 7: Deadlocks Adapted to COP4610 by Robert van Engelen.
System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention, Avoidance, and Detection Recovering from Deadlock Combined Approach.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Chapter 7 Deadlocks. 7.2 Modified By Dr. Khaled Wassif Operating System Concepts – 7 th Edition Silberschatz, Galvin and Gagne ©2005 Chapter 7: Deadlocks.
Cosc 4740 Chapter 6, Part 4 Deadlocks. The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-6 Deadlocks Department of Computer Science and Software Engineering.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 AE4B33OSS Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization.
CHAPTER 8: DEADLOCKS System Model Deadlock Characterization
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
 The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock Avoidance  Deadlock.
Dr. Kalpakis CMSC 421, Operating Systems Deadlocks.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks System Model Deadlock Characterization Methods.
Mi-Jung Choi Dept. of Computer and Science Silberschatz, Galvin and Gagne ©2006 Operating System Principles Chapter 7: Deadlocks.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 7 Operating Systems.
Operating Systems Unit VI Deadlocks and Protection Department of Computer Science Engineering and Information Technology.
Chapter 8 Deadlocks. Objective System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection.
Chapter 7: Deadlocks. 7.2CSCI 380 – Operating Systems Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
CS307 Operating Systems Deadlocks Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2012.
國立台灣大學 資訊工程學系 Chapter 7: Deadlocks. 資工系網媒所 NEWS 實驗室 Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes.
Chap 7 Deadlocks. Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks To.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 6 Deadlocks Slide 1 Chapter 6 Deadlocks.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks.
Deadlocks Introduction to Operating Systems: Module 7.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 7: Deadlocks.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
7.1 CSE Department MAITSandeep Tayal 7: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
CSE Operating System Principles Deadlocks. CSE – Operating System Principles2 Overview System Model Deadlock Characterization Methods for.
Chapter 7: Deadlocks. The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Process Deadlocks.
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks.
Outline Deadlocks, dead lock prevention, avoidance.
Deadlock Prevention Restrain the ways request can be made.
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Presentation transcript:

Styresystemer og Multiprogrammering Block 3, 2005 Deadlocks Robert Glück

2 Today’s Plan Deadlocks: –System Model –Deadlock Characterization –Resource-Allocation Graph –Methods for Handling Deadlocks –Detect, Avoid, Prevent Deadlocks

3 What is a Deadlock? A set of processes is blocked because each is holding a resource and waiting to acquire a resource held by another process in the set.

4 Example: Traffic Deadlock

5 Deadlock Characterization Can arise if four conditions hold simultaneously: 1.Mutual exclusion: only one process at a time can use a resource; resource nonsharable. 2.No preemption: a resource can be released only voluntarily by the process holding it. 3.Hold and wait: a process holding at least one resource is waiting to acquire more resources. 4.Circular wait: a circular dependence exists: P 1  P 2  …  P n

6 System Model Finite number of resources to be distributed among a number of competing processes. Resource types R 1, …, R m –Each resource type has one or more instances –Files, memory space, I/O devices, CPU cycles Each process utilizes a resource: Request  Use  Release resource

7 Resource-Allocation Graph deadlock request R1 R3 R2 resources P1 P2P3 processes

8 Graph with Cycle, but no Deadlock

9 Basic Facts If graph contains no cycles  no deadlock If graph contains a cycle  if one instance per resource type, then deadlock. if several instances per resource type, then possibility of deadlock.

10 Deadlock Example threadA lock(X) lock(Y) threadB lock(Y) lock(X) Deadlock possible: 1. threadA locks X 2. threadB locks Y 3. threadA waits for lock Y 4. threadB waits for lock X No deadlock will occur if threadA is able to acquire and release the locks for X and Y before threadB attempts to lock the two objects. Difficult to identify and test for deadlocks that may occur only under certain circumstances.

11 Methods for Handling Deadlocks Detect and recover: allow the system to enter a deadlock, detect it, and recover Avoid: ensure that system never enters a deadlock Prevent: ensure that at least one of the necessary conditions cannot hold. Ignore: pretend that deadlocks never occur; used by most OS, including UNIX.

12 Deadlock Detection 1.Allow system to enter deadlock state 2.Detection algorithm 3.Recovery scheme

13 Several Instances of a Resource Type Available: Vector of length m indicates the number of available resources of each type R j Allocation: An n x m matrix defines the number of resources of each type R j currently allocated to each process P i Request: An n x m matrix indicates the current request of each process. If Request [i][j] = k, then process P i is requesting k more instances of resource type R j

14 Detection Algorithm Given: Free vector of length m (free resources) Finishvector of length n (finished processes) Initialize: (a) Free = Available (b)For i = 1,2, …, n do If Allocation i = 0 then Finish[i] = true. else Finish[i] = false. Algorithm requires an order of O(m x n 2) operations to detect whether the system is in deadlocked state.

15 Detection Algorithm (cont.) While an index i exists such that both Finish[i] == false(active process) Request i  Free(satisfiable request) Do Finish[i] = true(assume termination) Free = Free + Allocation i (release resources) End If Finish[i] == false, for some i, 1  i  n, then deadlock. (A process P i is deadlocked if Finish[i] == false.)

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

17 Recovery: Preemption Each section of a bridge: 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. Starvation is possible.

18 Recovery: Process 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.

19 Recovery: 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 (simple to preempt) –Resources process needs to complete –How many processes will need to be terminated. –Is process interactive or batch?

20 Deadlock Avoidance Ensure that a system will never enter an unsafe state. Requires additional a priori information. Simplest and most useful model: each process declares the maximum number of resources of each type that it may need.

21 Safe State System is in safe state if there exists a safe sequence of all processes. Sequence 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. –If P i resource needs are not immediately available, then P i can wait until all P j have finished. –When P j is finished, P i can obtain needed resources, execute, return allocated resources, and terminate. –When P i terminates, P i+1 can obtain its needed resources, and so on.

22 System States unsafe state  possibility of deadlock safe state  no deadlocks

23 Banker’s Algorithm Multiple instances of some resource types. Each process must a priori claim maximum use. When a process gets all its resources it must return them in a finite amount of time. When a process requests a resource it may have to wait.

24 Data Structures: Banker’s Algorithm Available: Vector of length m indicates the number of available resources of each type R j Allocation: n x m matrix: number of resources of each type R j currently allocated to each process P i Max: n x m matrix: If Max [i,j] = k, then process Pi may request at most k instances of resource type Rj. Need: n x m matrix: If Request [i][j] = k, then process P i is requesting k more instances of resource type R j Need [i,j] = Max[i,j] – Allocation [i,j]

25 Banker’s Algorithm Given: Free vector of length m (free resources) Finishvector of length n (finished processes) Initialize: (a) Free = Available (b)For i = 1,2, …, n do Finish[i] = false Algorithm requires an order of O(m x n 2 ) operations to detect whether the system is in deadlocked state.

26 Banker’s Algorithm (cont.) While an index i exists such that both Finish[i] == false(process active) Need i  Free(max. request satisfiable) Do Finish[i] = true(assume termination) Free = Free + Allocation i (release all resources) End If Finish[i] == true, for all i, 1  i  n, then safe state.

27 Resource-Request for Process Pi 1.If Request i  Need i goto step 2. Else, raise error (P i exceeded max.claim) 2.If Request i  Available, goto step 3. Else, P i must wait, since resources are not available. 3.Pretend to allocate requested resources to P i by modifying the state as follows: Available = Available = Request i ; Allocation i = Allocation i + Request i ; Need i = Need i – Request i;; If unsafe  P i must wait, restore old resource-allocation If safe  the resources are allocated to P i

28 Deadlock Prevention Break one of the four conditions: 1.Mutual Exclusion 2.No Preemption 3.Hold and Wait 4.Circular Wait  Deadlock is impossible!

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

30 Prevention (cont.) No Preemption – –If a process that is holding some resources requests another resource that cannot be 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 and 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.

31 Combined Approaches 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.

32 Summary Deadlocks –Necessary conditions –Detection: algorithm for several rsc instances –Recovery: strategy –Avoidance: banker’s algorithm –Prevention: break a necessary condition –Combination: select best approach for each resource class

33 Source These slides are based on SGG04 and the slides provided by the authors.