Lecture 9 Page 1 CS 111 Online Deadlock What is a deadlock? A situation where two entities have each locked some resource Each needs the other’s locked.

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Chapter 5 Deadlocks. Contents What is deadlock? What is deadlock? Characterization Characterization Resource allocation graph Resource allocation graph.
ICS Principles of Operating Systems Lectures 8 and 9 - Deadlocks Prof. Dmitri V. Kalashnikov dvk ics.uci.edu Slides © Prof. Nalini Venkatasubramanian.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
1 Deadlock Solutions: Avoidance, Detection, and Recovery CS 241 March 30, 2012 University of Illinois.
Chapter 7: Deadlocks.
Deadlocks Andy Wang Operating Systems COP 4610 / CGS 5765.
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:
Multi-Object Synchronization. Main Points Problems with synchronizing multiple objects Definition of deadlock – Circular waiting for resources Conditions.
Chapter 6 Concurrency: Deadlock and Starvation
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.
1 Wednesday, June 28, 2006 Command, n.: Statement presented by a human and accepted by a computer in such a manner as to make the human feel that he is.
Chapter 7: Deadlocks. 7.2 Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks.
Deadlock CSCI 444/544 Operating Systems Fall 2008.
1 Lecture 8: Deadlocks Operating System Spring 2008.
CPSC 4650 Operating Systems Chapter 6 Deadlock and Starvation
1 Concurrency: Deadlock and Starvation Chapter 6.
7/2/2015cse deadlock © Perkins, DW Johnson and University of Washington1 Deadlock CSE 410, Spring 2008 Computer Systems
Silberschatz, Galvin and Gagne  Operating System Concepts Deadlock and Starvation Deadlock – two or more processes are waiting indefinitely for.
What we will cover…  The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock.
CPS110: Deadlock Landon Cox February 5, Concurrency so far  Mostly tried to constrain orderings  Locks, monitors, semaphores  It is possible.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Deadlocks.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
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.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Operating Systems Part III: Process Management (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.
Deadlocks Silberschatz Ch. 7 and Priority Inversion Problems.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Lecture 6 Page 1 CS 111 Summer 2014 Concurrency Solutions and Deadlock CS 111 Operating Systems Peter Reiher.
1 Deadlock. 2 Concurrency Issues Past lectures:  Problem: Safely coordinate access to shared resource  Solutions:  Use semaphores, monitors, locks,
CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.
 The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock Avoidance  Deadlock.
CS 346 – Chapter 7 Deadlock –Properties –Analysis: directed graph Handle –Prevent –Avoid Safe states and the Banker’s algorithm –Detect –Recover.
Operating Systems Lecture 4 Deadlock Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software Engineering.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
Lecture 9 Page 1 CS 111 Online Deadlock Prevention Deadlock avoidance tries to ensure no lock ever causes deadlock Deadlock prevention tries to assure.
CS333 Intro to Operating Systems Jonathan Walpole.
Styresystemer og Multiprogrammering Block 3, 2005 Deadlocks Robert Glück.
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.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 7 Operating Systems.
Chapter 7: Deadlocks. 7.2CSCI 380 – Operating Systems Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling.
Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
Lecture 9 Page 1 CS 111 Spring 2015 Synchronization and Deadlock CS 111 Operating Systems Peter Reiher.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 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);
Lecture 9 Page 1 CS 111 Fall 2015 Synchronization and Deadlock CS 111 Operating Systems Peter Reiher.
Process Management Deadlocks.
Operating System Principles: Deadlocks – Problems and Solutions CS 111 Operating Systems Peter Reiher.
CSE 120 Principles of Operating
Concurrency: Deadlock and Starvation
ITEC 202 Operating Systems
CS703 - Advanced Operating Systems
Advanced Operating System Fall 2009
Introduction to Operating Systems
ICS 143 Principles of Operating Systems
Chapter 7 Deadlocks.
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
CSc 552 Advanced Unix Process deadlock deadlock prevention
CS 333 Introduction to Operating Systems Class 7 - Deadlock
Andy Wang Operating Systems COP 4610 / CGS 5765
SE350: Operating Systems Lecture 5: Deadlock.
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

Lecture 9 Page 1 CS 111 Online Deadlock What is a deadlock? A situation where two entities have each locked some resource Each needs the other’s locked resource to continue Neither will unlock till they lock both resources Hence, neither can ever make progress

Lecture 9 Page 2 CS 111 Online Why Are Deadlocks Important? A major peril in cooperating parallel processes – They are relatively common in complex applications – They result in catastrophic system failures Finding them through debugging is very difficult – They happen intermittently and are hard to diagnose – They are much easier to prevent at design time Once you understand them, you can avoid them – Most deadlocks result from careless/ignorant design – An ounce of prevention is worth a pound of cure

Lecture 9 Page 3 CS 111 Online Deadlocks and Different Resource Types Commodity Resources – Clients need an amount of it (e.g. memory) – Deadlocks result from over-commitment – Avoidance can be done in resource manager General Resources – Clients need a specific instance of something A particular file or semaphore A particular message or request completion – Deadlocks result from specific dependency relationships – Prevention is usually done at design time

Lecture 9 Page 4 CS 111 Online Types of Deadlocks Commodity resource deadlocks – E.g., memory, queue space General resource deadlocks – E.g., files, critical sections Heterogeneous multi-resource deadlocks – E.g., P1 needs a file P2 holds, P2 needs memory which P1 is using Producer-consumer deadlocks – E.g., P1 needs a file P2 is creating, P2 needs a message from P1 to properly create the file

Lecture 9 Page 5 CS 111 Online Four Basic Conditions For Deadlocks For a deadlock to occur, these conditions must hold: 1.Mutual exclusion 2.Incremental allocation 3.No pre-emption 4.Circular waiting

Lecture 9 Page 6 CS 111 Online Deadlock Conditions: 1. Mutual Exclusion The resources in question can each only be used by one entity at a time If multiple entities can use a resource, then just give it to all of them If only one can use it, once you’ve given it to one, no one else gets it – Until the resource holder releases it

Lecture 9 Page 7 CS 111 Online Deadlock Condition 2: Incremental Allocation Processes/threads are allowed to ask for resources whenever they want – As opposed to getting everything they need before they start If they must pre-allocate all resources, either: – They get all they need and run to completion – They don’t get all they need and abort In either case, no deadlock Should the pre- allocation step be a critical section itself? Is there another option?

Lecture 9 Page 8 CS 111 Online Deadlock Condition 3: No Pre-emption When an entity has reserved a resource, you can’t take it away from him – Not even temporarily If you can, deadlocks are simply resolved by taking someone’s resource away – To give to someone else But if you can’t take it away from anyone, you’re stuck

Lecture 9 Page 9 CS 111 Online Deadlock Condition 4: Circular Waiting A waits on B which waits on A In graph terms, there’s a cycle in a graph of resource requests Could involve a lot more than two entities But if there is no such cycle, someone can complete without anyone releasing a resource – Allowing even a long chain of dependencies to eventually unwind – Maybe not very fast, though...

Lecture 9 Page 10 CS 111 Online A Wait-For Graph Thread 1Thread 2 Critical Section A Critical Section B Thread 1 acquires a lock for Critical Section A Thread 2 acquires a lock for Critical Section B Thread 1 requests a lock for Critical Section B Thread 2 requests a lock for Critical Section A No problem! Deadlock! We can’t give him the lock right now, but... Hmmmm...

Lecture 9 Page 11 CS 111 Online Deadlock Avoidance Use methods that guarantee that no deadlock can occur, by their nature Advance reservations – The problems of under/over-booking – The Bankers’ Algorithm Practical commodity resource management Dealing with rejection Reserving critical resources

Lecture 9 Page 12 CS 111 Online Avoiding Deadlock Using Reservations Advance reservations for commodity resources – Resource manager tracks outstanding reservations – Only grants reservations if resources are available Over-subscriptions are detected early – Before processes ever get the resources Client must be prepared to deal with failures – But these do not result in deadlocks Dilemma: over-booking vs. under-utilization

Lecture 9 Page 13 CS 111 Online Overbooking Vs. Under Utilization Processes generally cannot perfectly predict their resource needs To ensure they have enough, they tend to ask for more than they will ever need Either the OS: – Grants requests till everything’s reserved In which case most of it won’t be used – Or grants requests beyond the available amount In which case sometimes someone won’t get a resource he reserved

Lecture 9 Page 14 CS 111 Online Handling Reservation Problems Clients seldom need all resources all the time All clients won't need max allocation at the same time Question: can one safely over-book resources? – For example, seats on an airplane What is a “safe” resource allocation? – One where everyone will be able to complete – Some people may have to wait for others to complete – We must be sure there are no deadlocks

Lecture 9 Page 15 CS 111 Online The Banker’s Algorithm One algorithm for resource reservations Assumptions: 1.All critical resources are known and quantifiable – E.g., money or memory – No other resources can cause deadlocks 2.All clients reserve for their maximum requirement – They will never need more than this amount 3.If a client gets his maximum, he will complete – Upon completion, he frees all his resources – Those resources then become available for others

Lecture 9 Page 16 CS 111 Online The Rules of the Banker’s Algorithm Given a resource “state” characterized by: – Total size of each pool of resources – Reservations granted to each client for each resource – Current allocations of each resource to each client A state is “safe” if... – Enough resources allocated to at least one client to allow him to finish – After any client frees its resources, resulting state is safe – And so on, until all clients have completed A proposed allocation can be granted if the resulting state would still be “safe”

Lecture 9 Page 17 CS 111 Online Why Isn’t the Banker’s Algorithm Used? Quantified resources assumption – Not all resources are measurable in units – Other resource types can introduce circular dependencies Eventual completion assumption – All resources are released when client completes – In modern systems many tasks run for months Likelihood of resource “convoy” formation – Reduced parallelism, reduced throughput Many systems choose simpler “don't overbook” policy

Lecture 9 Page 18 CS 111 Online Commodity Resource Management in Real Systems Advanced reservation mechanisms are common – Unix brk() and sbrk() system calls – Disk quotas, Quality of Service contracts Once granted, system must guarantee reservations – Allocation failures only happen at reservation time – Hopefully before the new computation has begun – Failures will not happen at request time – System behavior more predictable, easier to handle But clients must deal with reservation failures

Lecture 9 Page 19 CS 111 Online Dealing With Reservation Failures Resource reservation eliminates deadlock Apps must still deal with reservation failures – Application design should handle failures gracefully E.g., refuse to perform new request, but continue running – App must have a way of reporting failure to requester E.g., error messages or return codes – App must be able to continue running All critical resources must be reserved at start-up time

Lecture 9 Page 20 CS 111 Online Isn’t Rejecting App Requests Bad? It’s not great, but it’s better than failing later With advance notice, app may be able to adjust service not to need the unavailable resource If app is in the middle of servicing a request, we may have other resources allocated and the request half-performed – If we fail then, all of this will have to be unwound – Could be complex, or even impossible

Lecture 9 Page 21 CS 111 Online Why Not Just Wait? If reservation fails, why not hold on to what I’ve got and ask again later? What would happen in our deadlock example? – Nobody would ever make progress – That’s what would generally happen in deadlock if you just wait Making your clients wait indefinitely is a bad idea

Lecture 9 Page 22 CS 111 Online System Services and Reservations System services must never deadlock for memory Potential deadlock: swap manager – Invoked to swap out processes to free up memory – May need to allocate memory to build I/O request – If no memory available, unable to swap out processes – So it can’t free up memory, and system wedges Solution: – Pre-allocate and hoard a few request buffers – Keep reusing the same ones over and over again – Little bit of hoarded memory is a small price to pay to avoid deadlock That’s just one example system service, of course