SE350: Operating Systems Lecture 5: Deadlock.

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

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.
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.
Deadlocks Andy Wang Operating Systems COP 4610 / CGS 5765.
Multi-Object Synchronization. Main Points Problems with synchronizing multiple objects Definition of deadlock – Circular waiting for resources Conditions.
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.
CPS110: Deadlock Landon Cox February 5, Concurrency so far  Mostly tried to constrain orderings  Locks, monitors, semaphores  It is possible.
CS 153 Design of Operating Systems Spring 2015 Lecture 11: Scheduling & Deadlock.
Deadlock Questions answered in this lecture: What are the four necessary conditions for deadlock? How can deadlock be prevented? How can deadlock be avoided?
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.
This Time - Deadlock Definition Conditions for deadlocks
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Chapter 7 Deadlocks Page 2 Chapter 7: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
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.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 7 Operating Systems.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 6 Deadlocks Slide 1 Chapter 6 Deadlocks.
Multi-Object Synchronization. Multi-Object Programs What happens when we try to synchronize across multiple objects in a large program? – Each object.
Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Deadlock.
Deadlocks CPE Operating Systems
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.
Deadlocks Lots of resources can only be used by one process at a time. Exclusive access is needed. Suppose process A needs R1 + R2 and B needs R1 + R2.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Process Management Deadlocks.
Synchronization Deadlocks and prevention
Chapter 7: Deadlocks.
CSE 120 Principles of Operating
CS703 - Advanced Operating Systems
Chapter 7: Deadlocks Source & Copyright: Operating System Concepts, Silberschatz, Galvin and Gagne.
Chapter 7 – Deadlock and Indefinite Postponement
Chapter 7: Deadlocks.
Operating System: DEADLOCKS
Introduction to Operating Systems
OPERATING SYSTEMS DEADLOCKS
ICS 143 Principles of Operating Systems
Chapter 7 Deadlocks.
CSCI 511 Operating Systems Chapter 7 Deadlock
Process Deadlocks.
Chapter 7: Deadlocks.
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks.
Andy Wang Operating Systems COP 4610 / CGS 5765
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Deadlocks Session - 13.
Chapter 7: Deadlocks.
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
OPERATING SYSTEMS DEADLOCKS.
CSE 153 Design of Operating Systems Winter 2019
OPERATING SYSTEMS DEADLOCKS.
DEADLOCKS.
Introduction to Deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
CENG334 Introduction to Operating Systems
Chapter 7: Deadlocks.
CSE 542: Operating Systems
Presentation transcript:

SE350: Operating Systems Lecture 5: Deadlock

Topics What is deadlock? What are the necessary conditions for deadlock? How can we prevent deadlock? How can we detect deadlock and recover from it?

Definitions Resource: any (passive) entity needed by a thread to do its job (CPU, disk space, memory, lock) Preemptable: can be taken away by OS Non-preemptable: must leave with thread Starvation: thread waits indefinitely Deadlock: circular waiting for resources Deadlock leads to starvation, but not vice versa (disk space -- what would you think if I took space for your files?) (mutual exclusion is a weird kind of resource!)

Deadlock won't always happen with this code, but it might Example: Two Locks // Thread A lock1.acquire(); lock2.acquire(); lock2.release(); lock1.release(); // Thread B lock2.acquire(); lock1.acquire(); lock1.release(); lock2.release(); Deadlock won't always happen with this code, but it might Owned by Waiting for Thread A lock1 lock2 Thread B Waiting for Owned by

Bidirectional Bounded Buffer // Thread A buffer1.put(data); buffer2.get(); // Thread B buffer2.put(data); buffer1.get(); Deadlock could happen if buffer1 and buffer2 both start almost full Owned by Waiting for Thread A Space on Buffer2 Space on Buffer1 Thread B Waiting for Owned by

Two Locks and a Condition Variable // Thread A lock1.acquire(); … lock2.acquire(); while (need to wait) condition.wait(lock2); lock2.release(); lock1.release(); // Thread B lock1.acquire(); … lock2.acquire(); condition.signal(lock2); lock2.release(); lock1.release(); What happens here?

Two Locks and a Condition Variable (cont.) Owned by Waiting for Thread A Lock1 CV Thread B Waiting for Owned by

Dining Philosophers Politicians! Each politician needs two chopsticks to eat Each grabs chopstick on the right first Deadlock if all grab chopstick at same time Deadlock depends on the order of execution No deadlock if one was left-handed

Necessary Conditions for Deadlock Limited resources Finite num. of threads can simultaneously use a resource No preemption Thread’s resource ownership cannot be revoked Multiple independent requests (wait while holding) Thread holds a resource while waiting for another First acquire one resources then try to acquire the other Circular chain of requests Each thread is waiting for a resource hold by another how many angels on the head of a pin

Questions How does Dining Politicians meet the necessary conditions for deadlock? Limited resources No preemption Multiple independent requests (wait while holding) Circular chain of requests How can we modify this to prevent deadlock? Have an infinite pool of chopsticks Take chopstick away from politicians if in deadlock Grab both chopsticks at once or neither Put one left handed politician

Preventing Deadlock Exploit or limit program behavior Limit program from anything that might lead to deadlock Predict the future If we know what program will do, we can tell if granting a resource might lead to deadlock Detect and recover If we can rollback threads, we can fix deadlock if it occurs

Exploit or Limit Program Behavior Provide enough resources Preempt resources Eliminate “wait while holding” Eliminate circular waiting

Provide Enough Resources Question: How many chopsticks are enough? One additional chopstick anywhere on the table! Acquire both chopsticks at once?

Eliminate Wait While Holding Release lock when calling out of module Module::foo() { lock.acquire(); doSomeStuff();    otherModule->bar();   lock.release(); }   Module::doSomeStuff() { x = x + 1; Module::foo() { doSomeStuff(); otherModule->bar(); } Module::doSomeStuff() { lock.acquire(); x = x + 1; lock.release();

Eliminate Circular Waiting Lock ordering: always acquire locks in a fixed order Question: Can we use resource ordering to eliminate deadlock in dining politicians?  Number chopsticks from 1 to N Pick lower-numbered chopstick before higher-numbered

Example // Thread 1 Acquire A Acquire C Wait for B // Thread 2 Acquire B Wait for A Have to stall here – otherwise, we’re doomed! Preventing deadlock means waiting even when the resource you are asking for is available, if some of the resources you will (or may) need are not available.

Deadlock Dynamics Safe state: Unsafe state: Doomed state: For any possible sequence of resource requests, there is at least one processing order that eventually succeeds May require waiting even when resources are available! Unsafe state: At least one sequence of future resource requests leads to deadlock no matter what processing order is tried Doomed state: All possible processing orders lead to deadlock

Possible System States Process can be in a safe, unsafe, or deadlocked state

Question What are the doomed states for Dining Politicians? Each politicians holds one chopstick What are the safe states? All the other states What are the unsafe states? Only the doomed states

Preventing Deadlock Exploit or limit program behavior Limit program from anything that might lead to deadlock Predict the future If we know what program will do, we can tell if granting a resource might lead to deadlock Detect and recover If we can rollback threads, we can fix deadlock if it occurs

Communal Dining Politicians N chopsticks in middle of table; N politicians, each needs 2 chopsticks and can grab 1 at a time When does grabbing a chopstick lead to deadlock? It's the last one, and no one would have 2 chopsticks  

Communal Mutant Dining Politicians N chopsticks in the middle of the table N politicians, each needs k > 2 chopsticks and can grab 1 at a time When does grabbing a chopstick lead to deadlock? It's the last one, and no one would have k It's the next to the last, and no one would have k-1, ... Deadlock free if when try to grab fork: take it unless it's the last one, and no one would have k it's the next to the last, and no one would have k-1, ...  

Banker’s Algorithm Grant request iff result is a safe state Sum of maximum resource needs of current threads can be greater than the total resources Provided there is some way for all the threads to finish without getting into deadlock Example: proceed iff Total available resources - # allocated >= max remaining that might be needed by this thread in order to finish Guarantees this thread can finish Gas crisis -- sum of gas tanks in cars bigger than storage capacity of tanks. Usually ok!

Detect and Repair Scan wait graph, detect cycles, fix cycles Terminate a thread to free up resources Not always possible, e.g., could lead to inconsistent state Proceed without the resource Requires robust exception handling code E.g., Amazon will say you can buy a book, if the inventory subsystem doesn’t reply quickly enough (wrong answer quickly is better than the right answer slowly) Roll back actions of deadlocked threads Common technique in databases Transaction allow rollbacks, restart to beginning of transaction a) Shoot thread, force it to give up resources.   This isn't always possible -- for instance, with a mutex, can't shoot a thread and leave world in a consistent state, unless the exception handling code is very carefully written. b) Continue even though you don’t have the resource you need --- e.g., Amazon will say you can have a book, if the inventory subsystem doesn’t reply quickly enough (wrong answer quickly is better than the right answer slowly) c) Roll back actions of deadlocked threads (transactions) Common technique in databases. Transactions -- allow you to do this rollback, to beginning of transaction.   Common technique in databases. Of course, if restart in exactly the same way, might still get deadlock. So roll only one car back.

Acknowledgment This lecture is a slightly modified version of the one prepared by Tom Anderson