Deadlocks: Part II Detection and Recovery. 2 Dining Philophers Problem Five chopsticks/Five philosophers (really cheap restaurant) –Free-for all: Philosopher.

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Chapter 7: Deadlocks.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 7: Deadlocks.
1 CSC 539: Operating Systems Structure and Design Spring 2005 Process deadlock  deadlock prevention  deadlock avoidance  deadlock detection  recovery.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Deadlocks  (How to Detect Them and Avoid Them) A:
CS 162 Week 3 Discussion (2/13 – 2/14). Today’s Section Project Administrivia (5 min) Quiz (5 min) Review Lectures 6 and 7 (15 min) Worksheet and Discussion.
Deadlocks Detection and Avoidance Prof. Sirer CS 4410 Cornell University.
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.
Deadlock April 19, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Deadlocks. 2 Deadlock Avoidance If we have future information –Max resource requirement of each process before they execute Can we guarantee that deadlocks.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 7: Deadlock Dr. Mohamed Hefeeda.
Deadlocks: Part II Detection and Recovery. Announcements Prelim coming up in two and a half weeks: –In class, Thursday, October 16 th, 10:10—11:25pm,
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
Deadlocks. System Model There are non-shared computer resources –Maybe more than one instance –Printers, Semaphores, Tape drives, CPU Processes need access.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Deadlocks.
System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention, Avoidance, and Detection Recovering from Deadlock Combined Approach.
CSC139 Operating Systems Lecture 9 Deadlock Adapted from Prof. John Kubiatowicz's lecture notes for CS162 Copyright.
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.
CHAPTER 8: DEADLOCKS System Model Deadlock Characterization
Deadlock zDeadlock is a problem that can exist when a group of processes compete for access to fixed resources. zDefinition: deadlock exists among a set.
Chapter 8: Deadlocks Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock.
 The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock Avoidance  Deadlock.
Deadlocks System Model RAG Deadlock Characterization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
Deadlocks: Part I Prevention and Avoidance. 2 Review: Dining Philosopher’s Dijkstra –A problem that was invented to illustrate a different aspect of communication.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 7 Operating Systems.
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.
CS307 Operating Systems Deadlocks Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2012.
Deadlocks - System Model - Deadlock characterization - Methods for handling deadlocks - Deadlock prevention,avoidance - Deadlock detection and recovery.
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 - 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.
Lecture 6 Deadlock 1. Deadlock and Starvation Let S and Q be two semaphores initialized to 1 P 0 P 1 wait (S); wait (Q); wait (Q); wait (S);. signal (S);
Chapter 7: Deadlocks. The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Chapter 7: Deadlocks.
OPERATING SYSTEM CONCEPTS AND PRACTISE
Chapter 7: Deadlocks.
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks.
Operating System: DEADLOCKS
Chapter 7 Deadlocks.
CSCI 511 Operating Systems Chapter 7 Deadlock
Process Deadlocks.
Deadlock B.Ramamurthy CSE421 1/11/2019 B.Ramamurthy.
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks.
CSc 552 Advanced Unix Process deadlock deadlock prevention
Deadlock B.Ramamurthy CSE421 2/23/2019 B.Ramamurthy.
Chapter 7: Deadlocks.
Deadlock Hank Levy 1.
Deadlock B.Ramamurthy CSE421 4/23/2019 B.Ramamurthy.
Deadlock B.Ramamurthy CSE421 5/1/2019 B.Ramamurthy.
CSCI 315 Operating Systems Design
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Deadlock B.Ramamurthy CSE421 8/28/2019 B.Ramamurthy.
Deadlock B.Ramamurthy CSE421 9/3/2019 B.Ramamurthy.
Presentation transcript:

Deadlocks: Part II Detection and Recovery

2 Dining Philophers Problem Five chopsticks/Five philosophers (really cheap restaurant) –Free-for all: Philosopher will grab any one they can –Need two chopsticks to eat What if all grab at same time? –Deadlock! How to prevent deadlock? –Make one of them give up a chopstick (Hah!) –Eventually everyone will get chance to eat How to avoid deadlock? –Never let philosopher take last chopstick if no philosopher has two chopsticks afterwards

3 Four requirements for Deadlock Mutual exclusion –Only one thread at a time can use a resource. Hold and wait –Thread holding at least one resource is waiting to acquire additional resources held by other threads No preemption –Resources are released only voluntarily by the thread holding the resource, after thread is finished with it Circular wait –There exists a set {T 1, …, T n } of waiting threads T 1 is waiting for a resource that is held by T 2 T 2 is waiting for a resource that is held by T 3 … T n is waiting for a resource that is held by T 1

4 We saw that you can prevent deadlocks. –By negating one of the four necessary conditions. (which are..?) We saw that the OS can schedule processes in a careful way so as to avoid deadlocks. –Using a resource allocation graph. –Banker’s algorithm. –What are the downsides to these? The story so far..

5 Techniques for Preventing Deadlock Infinite resources –Include enough resources so that no one ever runs out of resources. Doesn’t have to be infinite, just large –Give illusion of infinite resources (e.g. virtual memory) –Examples: Brooklyn bridge with 12,000 lanes. Never wait! Infinite disk space (not realistic yet?) No Sharing of resources (totally independent threads) –Not very realistic Don’t allow waiting –How the phone company avoids deadlock Call to your Mom in Toledo, works its way through the phone lines, but if blocked get busy signal. –Technique used in Ethernet/some multiprocessor nets Everyone speaks at once. On collision, back off and retry –Inefficient, since have to keep retrying Consider: driving to New York City; when hit traffic jam, suddenly you’re transported back home and told to retry!

6 Techniques for Preventing Deadlock (con’t) Make all threads request everything they’ll need at the beginning. –Problem: Predicting future is hard, tend to over-estimate resources –Example: If need 2 chopsticks, request both at same time Don’t leave home until we know no one is using any intersection between here and where you want to go; only one car on the Bay Bridge at a time Force all threads to request resources in a particular order preventing any cyclic use of resources –Thus, preventing deadlock –Example (x.P, y.P, z.P,…) Make tasks request disk, then memory, then… Keep from deadlock on freeways around SF by requiring everyone to go clockwise

7 Toward right idea: –State maximum resource needs in advance –Allow particular thread to proceed if: (available resources - #requested)  max remaining that might be needed by any thread Banker’s algorithm (less conservative): –Allocate resources dynamically Evaluate each request and grant if some ordering of threads is still deadlock free afterward Technique: pretend each request is granted, then run safety algorithm, - Check for ([Max node ]-[Alloc node ] ≤ [Avail]) - Grant request if result is deadlock free (conservative!) Keeps system in a “SAFE” state, i.e. there exists a sequence {P 1, P 2, … P n } with P 1 requesting all remaining resources, finishing, then T 2 requesting all remaining resources, etc.. –Algorithm allows the sum of maximum resource needs of all current threads to be greater than total resources Banker’s Algorithm for Avoiding Deadlock

8 Banker’s Algorithm Decides whether to grant a resource request. Data structures: n: integer # of processes m: integer # of resources available[1..m]available[i] is # of avail resources of type i max[1..n,1..m]max demand of each Pi for each Ri allocation[1..n,1..m]current allocation of resource Rj to Pi need[1..n,1..m]max # resource Rj that Pi may still request need i = max i - allocation i let request[i] be vector of # of resource Rj Process Pi wants

9 Basic Algorithm 1.If request[i] > need[i] then error (asked for too much) 2.If request[i] > available[i] then wait (can’t supply it now) 3.Resources are available to satisfy the request Let’s assume that we satisfy the request. Then we would have: available = available - request[i] allocation[i] = allocation [i] + request[i] need[i] = need [i] - request [i] Now, check if this would leave us in a safe state: if yes, grant the request, if no, then leave the state as is and cause process to wait.

10 Safety Check free[1..m] = available /* how many resources are available */ finish[1..n] = false (for all i) /* none finished yet */ Step 1: Find an i such that finish[i]=false and need[i] <= work /* find a proc that can complete its request now */ if no such i exists, go to step 3 /* we’re done */ Step 2: Found an i: finish [i] = true /* done with this process */ free = free + allocation [i] /* assume this process were to finish, and its allocation back to the available list */ go to step 1 Step 3: If finish[i] = true for all i, the system is safe. Else Not

11 Banker’s Algorithm: Example Allocation Max Available A B C A B C A B C P P P P P this is a safe state: safe sequence Suppose that P1 requests (1,0,2) - add it to P1’s allocation and subtract it from Available

12 Banker’s Algorithm: Example Allocation Max Available A B C A B C A B C P P P P P This is still safe: safe seq In this new state, P4 requests (3,3,0) not enough available resources P0 requests (0,2,0) let’s check resulting state

13 Banker’s Algorithm: Example Allocation Max Available A B C A B C A B C P P P P P This is unsafe state (why?) So P0’s request will be denied Problems with Banker’s Algorithm?

14 Banker’s Algorithm Example Banker’s algorithm with dining philosophers –“Safe” (won’t cause deadlock) if when try to grab chopstick either: Not last chopstick Is last chopstick but someone will have two afterwards –What if k-handed lawyers? Don’t allow if: It’s the last one, no one would have k It’s 2 nd to last, and no one would have k-1 It’s 3 rd to last, and no one would have k-2 …

15 Remaining deadlock discussion Solutions for deadlock detection and recovery

16 Deadlock Detection & Recovery If neither avoidance or prevention is implemented, deadlocks can (and will) occur. Coping with this requires: –Detection: finding out if deadlock has occurred Keep track of resource allocation (who has what) Keep track of pending requests (who is waiting for what) –Recovery: untangle the mess. Expensive to detect, as well as recover

17 Using the RAG Algorithm to detect deadlocks Suppose there is only one instance of each resource Example 1: Is this a deadlock? –P1 has R2 and R3, and is requesting R1 –P2 has R4 and is requesting R3 –P3 has R1 and is requesting R4 Example 2: Is this a deadlock? –P1 has R2, and is requesting R1 and R3 –P2 has R4 and is requesting R3 –P3 has R1 and is requesting R4 Use a wait-for graph: –Collapse resources –An edge P i  P k exists only if RAG has P i  R j & R j  P k –Cycle in wait-for graph  deadlock!

18 Resource-Allocation Graph and Wait-for Graph Resource-Allocation GraphCorresponding wait-for graph

19 2 nd Detection Algorithm What if there are multiple resource instances? Data structures: n: integer# of processes m: integer# of resources available[1..m]available[i] is # of avail resources of type i request[1..n,1..m]max demand of each Pi for each Ri allocation[1..n,1..m]current allocation of resource Rj to Pi finish[1..n] true if Pi’s request can be satisfied let request[i] be vector of # instances of each resource Pi wants

20 2 nd Detection Algorithm 1. work[]=available[] for all i < n, if allocation[i]  0 then finish[i]=false else finish[i]=true 2.find an index i such that: finish[i]=false; request[i]<=work if no such i exists, go to 4. 3.work=work+allocation[i] finish[i] = true, go to 2 4.if finish[i] = false for some i, then system is deadlocked with Pi in deadlock

21 Example Finished = {F, F, F, F}; Work = Available = (0, 0, 1); R1R1 R2R2 R3R3 P1P1 111 P2P2 212 P3P3 110 P4P4 111 R1R1 R2R2 R3R3 P1P1 321 P2P2 221 P3P3 001 P4P4 111 Allocation Request

22 Example Finished = {F, F, T, F}; Work = (1, 1, 1); R1R1 R2R2 R3R3 P1P1 111 P2P2 212 P3P3 110 P4P4 111 R1R1 R2R2 R3R3 P1P1 321 P2P2 221 P3P3 P4P4 111 Allocation Request

23 Example Finished = {F, F, T, T}; Work = (2, 2, 2); R1R1 R2R2 R3R3 P1P1 111 P2P2 212 P3P3 110 P4P4 111 R1R1 R2R2 R3R3 P1P1 321 P2P2 221 P3P3 P4P4 Allocation Request

24 Example Finished = {F, T, T, T}; Work = (4, 3, 4); R1R1 R2R2 R3R3 P1P1 111 P2P2 212 P3P3 110 P4P4 111 R1R1 R2R2 R3R3 P1P1 321 P2P2 P3P3 P4P4 Allocation Request

25 Example Finished = {T, T, T, T}; Work = (4, 3, 4); R1R1 R2R2 R3R3 P1P1 P2P2 P3P3 P4P4 R1R1 R2R2 R3R3 P1P1 P2P2 P3P3 P4P4 Allocation Request

26 When to run Detection Algorithm? For every resource request? For every request that cannot be immediately satisfied? Once every hour? When CPU utilization drops below 40%?

27 Deadlock Recovery Killing one/all deadlocked processes –Crude, but effective –Keep killing processes, until deadlock broken –Repeat the entire computation Preempt resource/processes until deadlock broken –Selecting a victim (# resources held, how long executed) –Rollback (partial or total) –Starvation (prevent a process from being executed)

28 FYI: Java 1.5 Concurrency Tools java.lang.management.ThreadMXBean.findDeadlockedThreads() –Part of the JVM management API –Can be used to detect deadlocks: returns you the threadIDs of threads currently blocking waiting to enter an object (and “ownable synchronizers”) –“It might be an expensive operation” - so when would you run it? java.util.concurrent.Semaphore (and thus mutex).CountDownLatch &.CountDownLatch.Exchanger (Nice - similar in flavor to co-routines from e.g. Scheme/Lisp) java.util.concurrent.atomic –A toolkit of classes that support lock-free programming (given H/W support): AtomicInteger aint = new AtomicInteger(42); aint.compareAndSet(whatIThinkItIs, whatIWantItToBe); //No lock needed....and more. Check out the following if interested: – – …but of course, you can’t use this in :o)

29 Summary Dining Philosophers Problem –Highlights need to multiplex resources –Context to discuss starvation, deadlock, livelock Four conditions for deadlocks –Mutual exclusion Only one thread at a time can use a resource –Hold and wait Thread holding at least one resource is waiting to acquire additional resources held by other threads –No preemption Resources are released only voluntarily by the threads –Circular wait  set {T 1, …, T n } of threads with a cyclic waiting pattern

30 Summary (2) Techniques for addressing Deadlock –Allow system to enter deadlock and then recover –Ensure that system will never enter a deadlock –Ignore the problem and pretend that deadlocks never occur in the system Deadlock prevention –Prevent one of four necessary conditions for deadlock Deadlock avoidance –Assesses, for each allocation, whether it has the potential to lead to deadlock –Banker’s algorithm gives one way to assess this Deadlock detection and recover –Attempts to assess whether waiting graph can ever make progress –Recover it not