1 Deadlock Solutions CS 241 March 28, 2012 University of Illinois.

Slides:



Advertisements
Similar presentations
ICS Principles of Operating Systems Lectures 8 and 9 - Deadlocks Prof. Dmitri V. Kalashnikov dvk ics.uci.edu Slides © Prof. Nalini Venkatasubramanian.
Advertisements

Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
1 Deadlock Solutions: Avoidance, Detection, and Recovery CS 241 March 30, 2012 University of Illinois.
Lecture 7: Deadlock     Necessary Conditions for Deadlock     Deadlock Prevention - Havender's Linear Ordering Deadlock Avoidance Deadlock Detection &
Deadlocks Today Resources & deadlocks Dealing with deadlocks Other issues Next Time Memory management.
6. Deadlocks 6.1 Deadlocks with Reusable and Consumable Resources
Deadlock CS Introduction to Operating Systems.
Deadlocks Andy Wang Operating Systems COP 4610 / CGS 5765.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Starvation and Deadlock
Avishai Wool lecture Introduction to Systems Programming Lecture 5 Deadlocks.
Ken Birman. Dining Philosophers A problem that was invented to illustrate some issues related to synchronization with objects Our focus here is on the.
10 Deadlock Example Process 1 Process 2 Resource 1 Resource 2 Process holds the resource Process requests the resource.
Deadlocks. 2 System Model There are non-shared computer resources –Maybe more than one instance –Printers, Semaphores, Tape drives, CPU Processes need.
Deadlock CSCI 444/544 Operating Systems Fall 2008.
The ‘deadlock’ conditions Reviewing some key points concerning the potential for ‘deadlock’ in an operating system.
1 Lecture 8: Deadlocks Operating System Spring 2008.
OS Spring 2004 Concurrency: Principles of Deadlock Operating Systems Spring 2004.
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Deadlock Prevention CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
OS Fall’02 Concurrency: Principles of Deadlock Operating Systems Fall 2002.
Chapter 6 Concurrency: Deadlock and Starvation
7/2/2015cse deadlock © Perkins, DW Johnson and University of Washington1 Deadlock CSE 410, Spring 2008 Computer Systems
Deadlock Chapter 3 Thursday, February 22, Today’s Schedule Assignment #4 from Chapter 3 posted Deadlock - Chapter 3  Skip multiple resources (3.4.2.
Deadlock Questions answered in this lecture: What are the four necessary conditions for deadlock? How can deadlock be prevented? How can deadlock be avoided?
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Chapter 7 – Deadlock (Pgs 283 – 306). Overview  When a set of processes is prevented from completing because each is preventing the other from accessing.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance 3.6. Deadlock prevention.
1 Deadlocks 2 Resources Examples of computer resources –printers –tape drives –tables Processes need access to resources in reasonable order Suppose.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Operating Systems 软件学院 高海昌 Operating Systems Gao Haichang, Software School, Xidian University 22 Contents  1. Introduction** 
Operating Systems Part III: Process Management (Deadlocks)
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
CIS Operating Systems Deadlock Professor Qiang Zeng Fall 2015.
This Time - Deadlock Definition Conditions for deadlocks
Resource Management Reusable  Disk blocks  File descriptors  Semaphores Consumable  Messages  Packets.
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Deadlock cs550 Operating Systems David Monismith.
Chapter 7 Deadlocks. 7.1 Introduction Deadlock Common approaches to deal with deadlock – Prevention – Avoidance – Detection and recovery.
Lecture 9 Page 1 CS 111 Online Deadlock Prevention Deadlock avoidance tries to ensure no lock ever causes deadlock Deadlock prevention tries to assure.
Chapter 6 Concurrency: Deadlock and Starvation
Deadlocks System Model RAG Deadlock Characterization
CS333 Intro to Operating Systems Jonathan Walpole.
Deadlocks Mark Stanovich Operating Systems COP 4610.
Deadlocks.  Deadlocks: Occurs when threads are waiting for resources with circular dependencies Often involve nonpreemptable resources, which cannot.
Starvation and Deadlock. Another EventBarrier Example EventBarrier channel; void OutputThread { while (TRUE) { ComputeDataToSend(); channel.Wait(); SendData();
CS3771 Today: Distributed Coordination  Previous class: Distributed File Systems Issues: Naming Strategies: Absolute Names, Mount Points (logical connection.
Deadlocks Copyright ©: University of Illinois CS 241 Staff1.
Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.
Operating Systems Unit 4: – Dining Philosophers – Deadlock – Indefinite postponement Operating Systems.
Deadlock CS Introduction to Operating Systems.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Deadlock.
Deadlock and Starvation
Process Management Deadlocks.
Concurrency: Deadlock and Starvation
Deadlock and Starvation
Chapter 7 – Deadlock and Indefinite Postponement
Chapter 12: Concurrency, Deadlock and Starvation
Introduction to Operating Systems
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
CS 333 Introduction to Operating Systems Class 7 - Deadlock
Andy Wang Operating Systems COP 4610 / CGS 5765
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
SE350: Operating Systems Lecture 5: Deadlock.
Introduction to Deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Presentation transcript:

1 Deadlock Solutions CS 241 March 28, 2012 University of Illinois

2 Announcements Office hours today: 3-4 and 5-6 In between: Talk by Tom Wenisch Energy efficiency in warehouse-scale computers 4pm, in 3405 SC Midterm exams: you may look at them through the end of this week An extension of our one-week policy Drop by office hours today (3-4 and 5-6) or schedule appointment

3 Deadlock: definition There exists a cycle of processes such that each process cannot proceed until the next process takes some specific action. Result: all processes in the cycle are stuck!

4 Deadlock in the real world Which way should I go?

5 Deadlock in the real world I can almost get across Drat! GRIDLOCK!

6 Deadlock: One-lane Bridge Traffic only in one direction Each section of a bridge can be viewed as a resource What can happen?

7 Deadlock: One-lane Bridge Traffic only in one direction Each section of a bridge can be viewed as a resource Deadlock  Resolved if cars back up (preempt resources and rollback)  Several cars may have to be backed up

8 Deadlock: One-lane Bridge Traffic only in one direction Each section of a bridge can be viewed as a resource Deadlock  Resolved if cars back up (preempt resources and rollback)  Several cars may have to be backed up But, starvation is possible e.g., if the rule is that Westbound cars always go first when present

9 Deadlock: One-lane Bridge Deadlock vs. Starvation Starvation = Indefinitely postponed  Delayed repeatedly over a long period of time while the attention of the system is given to other processes  Logically, the process may proceed but the system never gives it the CPU (unfortunate scheduling) Deadlock = no hope  All processes blocked; scheduling change won’t help I always have to back up!

10 Deadlock solutions Prevention Design system so that deadlock is impossible Avoidance Steer around deadlock with smart scheduling Detection & recovery Check for deadlock periodically Recover by killing a deadlocked processes and releasing its resources Do nothing Prevention, avoidance and detection/recovery are expensive If deadlock is rare, is it worth the overhead? Manual intervention (kill processes, reboot) if needed

11 Deadlock Prevention

12 Aside: Necessary Conditions for Deadlock Mutual exclusion  Processes claim exclusive control of the resources they require Hold-and-wait (a.k.a. wait-for) condition  Processes hold resources already allocated to them while waiting for additional resources No preemption condition  Resources cannot be removed from the processes holding them until used to completion Circular wait condition  A circular chain of processes exists in which each process holds one or more resources that are requested by the next process in the chain

13 Deadlock prevention Goal 1: devise resource allocation rules which make circular wait impossible Resources include mutex locks, semaphores, pages of memory,......but you can think about just mutex locks for now Goal 2: make sure useful behavior is still possible! The rules will necessarily be conservative  Rule out some behavior that would not cause deadlock But they shouldn’t be to be too conservative  We still need to get useful work done

14 Rule #1: No Mutual Exclusion For deadlock to happen: processes must claim exclusive control of the resources they require How to break it?

15 Rule #1: No Mutual Exclusion For deadlock to happen: processes must claim exclusive control of the resources they require How to break it? Non-exclusive access only  Read-only access Battle won!  War lost  Very bad at Goal #2

16 Rule #2: Allow preemption A lock can be taken away from current owner Let it go: If a process holding some resources is denied a further request, that process must release its original resources Or take it all away: OS preempts current resource owner, gives resource to new process/thread requesting it Breaks circular wait...because we don’t have to wait Reasonable strategy sometimes e.g. if resource is memory: “preempt” = page to disk Not so convenient for synchronization resources e.g., locks in multithreaded application What if current owner is in the middle of a critical section updating pointers? Data structures might be left in inconsistent state!

17 Rule #3: No hold and wait When waiting for a resource, must not hold others So, process can only have one resource locked Or, it must request all resources at the beginning Or, before asking for more: give up everything you have and request it all at one time Breaks circular wait In resource allocation diagram: process with an outgoing link must have no incoming links Therefore, cannot have a loop!

18 Rule #3: No hold and wait Breaks circular wait In resource allocation diagram: process with an outgoing link must have no incoming links Therefore, cannot have a loop! Q: Which of these request links would be disallowed? Process Resource request ownership

19 Rule #3: No hold and wait Breaks circular wait In resource allocation diagram: process with an outgoing link must have no incoming links Therefore, cannot have a loop! A: Legal links are... Process Resource request ownership

20 Rule #3: No hold and wait Very constraining (bad job on Goal #2) Better than Rules #1 and #2, but... Often need more than one resource Hard to predict at the begining what resources you’ll need Releasing and re-requesting is inefficient, complicates programming, might lead to starvation

21 Rule #4: request resources in order Must request resources in increasing order Impose ordering on resources (any ordering will do) If holding resource i, can only request resources > i Much less constraining (decent job on Goal #2) Strictly easier to satisfy than “No hold and wait”: If we can request all resources at once, then we can request them in increasing order But now, we don’t need to request them all at once Can pick the arbitrary ordering for convenience to the application Still might be inconvenient at times But why is it guaranteed to preclude circular wait?

22 Back to the trivial broken “solution”... # define N 5 void philosopher (int i) { while (TRUE) { think(); take_fork(i); take_fork((i+1)%N); eat(); /* yummy */ put_fork(i); put_fork((i+1)%N); } Dining Philosophers solution with unnumbered resources

23 Back to the trivial broken “solution”... # define N 5 void philosopher (int i) { while (TRUE) { think(); take_fork(i); take_fork((i+1)%N); eat(); /* yummy */ put_fork(i); put_fork((i+1)%N); } Dining Philosophers solution with unnumbered resources

24 Dining Philosophers solution with unnumbered resources Back to the trivial broken “solution”... # define N 5 void philosopher (int i) { while (TRUE) { think(); take_fork(i); take_fork((i+1)%N); eat(); /* yummy */ put_fork(i); put_fork((i+1)%N); }

25 Dining Philosophers solution with numbered resources Instead, number resources First request lower numbered fork # define N 5 void philosopher (int i) { while (TRUE) { think(); take_fork(LOWER(i)); take_fork(HIGHER(i)); eat(); /* yummy */ put_fork(LOWER(i)); put_fork(HIGHER(i)); }

26 Dining Philosophers solution with numbered resources Instead, number resources... Then request higher numbered fork # define N 5 void philosopher (int i) { while (TRUE) { think(); take_fork(LOWER(i)); take_fork(HIGHER(i)); eat(); /* yummy */ put_fork(LOWER(i)); put_fork(HIGHER(i)); }

27 Dining Philosophers solution with numbered resources Instead, number resources... Then request higher numbered fork # define N 5 void philosopher (int i) { while (TRUE) { think(); take_fork(LOWER(i)); take_fork(HIGHER(i)); eat(); /* yummy */ put_fork(LOWER(i)); put_fork(HIGHER(i)); }

28 Dining Philosophers solution with numbered resources Instead, number resources... One philosopher can eat! # define N 5 void philosopher (int i) { while (TRUE) { think(); take_fork(LOWER(i)); take_fork(HIGHER(i)); eat(); /* yummy */ put_fork(LOWER(i)); put_fork(HIGHER(i)); }

29 Without numbering Ordered resource requests prevent deadlock Cycle!

30 With numbering Ordered resource requests prevent deadlock Contradiction: Must have requested 3 first!

31 Proof by M.C. Escher

32

33 Are we always in trouble without ordering resources? No, not always: Ordered resource requests are sufficient to avoid deadlock, but not necessary Convenient, but may be conservative

34 Q: What’s the rule of the road? What’s the law? Does it resemble one of the rules we saw? I can almost get across Drat!

35 Summary Deadlock prevention Imposes rules on what system can do These rules are conservative Most useful technique: ordered resources Application can do it; no special OS support Next: dealing with deadlocks other ways Avoidance Detection & recovery