Quiz Answers. CS 537 Lecture 9 Deadlock. What can go wrong? Readers and Writers Monitor Example

Similar documents
Deadlocks: Part I Prevention and Avoidance Yi Shi Fall 2017 Xi an Jiaotong University

Deadlocks Detection and Avoidance. Prof. Sirer CS 4410 Cornell University

Deadlocks: Detection & Avoidance

! What is a deadlock? ! What causes a deadlock? ! How do you deal with (potential) deadlocks? Maria Hybinette, UGA

Bridge Crossing Example

!! What is a deadlock? !! What causes a deadlock? !! How do you deal with (potential) deadlocks? Maria Hybinette, UGA

System Model. Deadlocks. Deadlocks. For example: Semaphores. Four Conditions for Deadlock. Resource Allocation Graph

System Model. Types of resources Reusable Resources Consumable Resources

Last Class: Monitors. Real-world Examples

The Deadlock Problem. A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set.

Deadlocks. Copyright : University of Illinois CS 241 Staff 1

Chapter 7: Deadlocks. Operating System Concepts with Java 8 th Edition

Deadlock. Concepts to discuss. A System Model. Deadlock Characterization. Deadlock: Dining-Philosophers Example. Deadlock: Bridge Crossing Example

Chapter 8: Deadlocks. The Deadlock Problem

The Deadlock Problem. Chapter 8: Deadlocks. Bridge Crossing Example. System Model. Deadlock Characterization. Resource-Allocation Graph

CS307 Operating Systems Deadlocks

Deadlocks. Bridge Crossing Example. The Problem of Deadlock. Deadlock Characterization. Resource-Allocation Graph. System Model

Deadlocks Characterization & Detection

Operating Systems ECE344. Ding Yuan

Chapter 8: Deadlocks. Bridge Crossing Example. The Deadlock Problem

Principles of Operating Systems

The Deadlock Problem

Operating Systems. Deadlock. Lecture 7 Michael O Boyle

CSE Opera+ng System Principles

Chapter 7: Deadlocks

More on Synchronization and Deadlock

Deadlock and Starvation

The Deadlock Lecture

Chapter 7: Deadlocks. Operating System Concepts 8 th Edition,

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

Principles of Operating Systems

The Deadlock Problem (1)

Resources. Lecture 4. Deadlocks. Resources (2) Resources (1) Four Conditions for Deadlock. Introduction to Deadlocks

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Chapter 8: Deadlocks

Chapter 8: Deadlocks. The Deadlock Problem. System Model. Bridge Crossing Example. Resource-Allocation Graph. Deadlock Characterization

Operating Systems. Lecture3.2 - Deadlock Management. Golestan University. Hossein Momeni

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Deadlocks. Mehdi Kargahi School of ECE University of Tehran Spring 2008

Last Class: Synchronization Problems!

CS 318 Principles of Operating Systems

Deadlocks. Today. Next Time. Resources & deadlocks Dealing with deadlocks Other issues. I/O and file systems

Chapter 7: Deadlocks. Operating System Concepts 8 th Edition,

Chapter 7: Deadlocks

Chapter 7: Deadlocks. Chapter 7: Deadlocks. The Deadlock Problem. Chapter Objectives. System Model. Bridge Crossing Example

COMP 3713 Operating Systems Slides Part 3. Jim Diamond CAR 409 Jodrey School of Computer Science Acadia University

Readings and References. Deadlock. Deadlock. Simple Traffic Gridlock Example. Reading. CSE Computer Systems November 30, 2001.

Chapter 6. Deadlocks

Module 7: Deadlocks. The Deadlock Problem. Bridge Crossing Example. System Model

Deadlock Risk Management

Operating Systems. Operating Systems Sina Meraji U of T

Chapter 3: Deadlocks

Chapter 7: Deadlocks. Operating System Concepts 8 th Edition,! Silberschatz, Galvin and Gagne 2009!

MODERN OPERATING SYSTEMS. Third Edition ANDREW S. TANENBAUM. Chapter 6 Deadlocks

CMPT 300 Introduction to Operating Systems

CMSC 412. Announcements

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

CISC 7310X. C10: Deadlocks. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 4/12/2018 CUNY Brooklyn College

Chapter 7: Deadlocks. Operating System Concepts 8th Edition, modified by Stewart Weiss

The Deadlock Problem

Deadlocks. Today. Next Time. ! Resources & deadlocks! Dealing with deadlocks! Other issues. ! Memory management

Chapter 7 : 7: Deadlocks Silberschatz, Galvin and Gagne 2009 Operating System Concepts 8th Edition, Chapter 7: Deadlocks

DEADLOCKS M O D E R N O P E R A T I N G S Y S T E M S C H A P T E R 6 S P R I N G

Deadlocks Prof. James L. Frankel Harvard University. Version of 7:05 PM 7-Mar-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

Chapter 7: Deadlocks CS370 Operating Systems

OPERATING SYSTEMS. Prescribed Text Book. Operating System Principles, Seventh Edition. Abraham Silberschatz, Peter Baer Galvin and Greg Gagne

CSE120 Principles of Operating Systems. Prof Yuanyuan (YY) Zhou Deadlock

Chapter 7: Deadlocks

Lecture 7 Deadlocks (chapter 7)

Module 7: Deadlocks. The Deadlock Problem

CS4410, Fall 2016 Homework 2 Deadline: 09/28/2016, 11:59AM

Module 7: Deadlocks. System Model. Deadlock Characterization. Methods for Handling Deadlocks. Deadlock Prevention. Deadlock Avoidance

Deadlocks. Minsoo Ryu. Real-Time Computing and Communications Lab. Hanyang University.

Chapter 8: Deadlocks. The Deadlock Problem

Deadlocks. Deadlock in Resource Sharing Environment. CIT 595 Spring Recap Example. Representing Deadlock

CS 153 Design of Operating Systems Winter 2016

Chapter 7: Deadlocks

Deadlock Prevention, Avoidance, and Detection

Deadlocks. Prepared By: Kaushik Vaghani

Deadlock. Readings and References. Simple Traffic Gridlock Example. Deadlock. Reading. Other References. CSE 410, Spring 2006 Computer Systems

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

The deadlock problem

Deadlock Risk Management

CS307: Operating Systems

Semaphores. Blocking in semaphores. Two types of semaphores. Example: Bounded buffer problem. Binary semaphore usage

Deadlocks. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Introduction to OS. Deadlock. MOS Ch. 6. Mahmoud El-Gayyar. Mahmoud El-Gayyar / Introduction to OS 1

Operating Systems. Deadlocks

Chapter 8: Deadlocks. Operating System Concepts with Java

COP 4610: Introduction to Operating Systems (Spring 2016) Chapter 7 Deadlocks. Zhi Wang Florida State University

CSCC 69H3 2/1/13. Today: Deadlock. Remember example from last week? Example of Deadlock. Example: dining philosophers: Not just an OS Problem!

Deadlocks. Dr. Yingwu Zhu

Deadlocks. System Model

Deadlocks. Deadlock Overview

Chapter 3. Deadlocks

CSC 539: Operating Systems Structure and Design. Spring 2005

What is Deadlock? Two or more entities need a resource to make progress, but will never get that resource. Examples from everyday life:

Deadlock and Starvation

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

VI. Deadlocks. What is a Deadlock? Intended Schedule. Deadlock Problem (1) ???

Transcription:

Quiz Answers CS 537 Lecture 9 Deadlock Use of disabling interrupts Not allowed by processor --> requires system call Not safe is usermode code buggy and allowed by processor Locking Just lock manipulation of list, nothing else Double-checked locking Is safe here - assuming fine never gets closed and CPU doesn t reorder things or leave values in registers Remzi Arpaci-Dussea, Michael Swift 1 Remzi Arpaci-Dussea, Michael Swift 2 Readers and Writers Monitor Example What can go wrong? Monitor ReadersNWriters { int WaitingWriters, WaitingReaders, NReaders, NWriters; Condition CanRead, CanWrite; Void BeginWrite() { if(nwriters == 1 NReaders > 0) { ++WaitingWriters; wait(canwrite); --WaitingWriters; NWriters = 1; Void EndWrite() { NWriters = 0; if(waitingreaders) Signal(CanRead); else Signal(CanWrite); Void BeginRead() { if(nwriters == 1 WaitingWriters > 0) { ++WaitingReaders; Wait(CanRead); --WaitingReaders; ++NReaders; Signal(CanRead); Void EndRead() { if(--nreaders == 0) Signal(CanWrite); For example: dining philosphers Primarily, we worry about: Starvation: A policy that can leave some philosopher hungry in some situation (even one where the others collaborate) Deadlock: A policy that leaves all the philosophers stuck, so that nobody can do anything at all Livelock: A policy that makes them all do something endlessly without ever eating! Remzi Arpaci-Dussea, Michael Swift 3 Remzi Arpaci-Dussea, Michael Swift 4 1

Starvation vs Deadlock Starvation vs. Deadlock Starvation: thread waits indefinitely Example, low-priority thread waiting for resources constantly in use by high-priority threads Deadlock: circular waiting for resources Thread A owns Res 1 and is waiting for Res 2 Thread B owns Res 2 and is waiting for Res 1 Gridlock Real World Deadlocks? Owned By Wait For Res 1 Thread A Thread B Res 2 Wait For Owned By Deadlock Starvation but not vice versa Starvation can end (but doesn t have to) Deadlock can t end without external intervention Remzi Arpaci-Dussea, Michael Swift 5 Remzi Arpaci-Dussea, Michael Swift 6 Testing for deadlock How do cars do it? Never block an intersection Must back up if you find yourself doing so Why does this work? Breaks a wait-for relationship Illustrates a sense in which intransigent waiting (refusing to release a resource) is one key element of true deadlock! Testing for deadlock Steps Collect process state and use it to build a graph Ask each process are you waiting for anything? Put an edge in the graph if so We need to do this in a single instant of time, not while things might be changing Now need a way to test for cycles in our graph Remzi Arpaci-Dussea, Michael Swift 7 Remzi Arpaci-Dussea, Michael Swift 8 2

Testing for deadlock Graph reduction example One way to find cycles Look for a node with no outgoing edges Erase this node, and also erase any edges coming into it Idea: This was a process people might have been waiting for, but it wasn t waiting for anything else If (and only if) the graph has no cycles, we ll eventually be able to erase the whole graph! This is called a graph reduction algorithm 8 0 1 2 3 4 11 7 5 9 6 This graph can be fully reduced, hence there was no deadlock at the time the graph was drawn. 10 12 Remzi Arpaci-Dussea, Michael Swift 9 Remzi Arpaci-Dussea, Michael Swift 10 Graph reduction example This is an example of an irreducible graph It contains a cycle and represents a deadlock, although only some processes are in the cycle Some questions you might ask If a system is deadlocked, could this go away? No, unless someone kills one of the threads or something causes a process to release a resource Many real systems put time limits on waiting precisely for this reason. When a process gets a timeout exception, it gives up waiting and this also can eliminate the deadlock But that process may be forced to terminate itself because often, if a process can t get what it needs, there are no other options available! Remzi Arpaci-Dussea, Michael Swift 11 Remzi Arpaci-Dussea, Michael Swift 12 3

Some questions you might ask Suppose a system isn t deadlocked at time T. Can we assume it will still be free of deadlock at time T+1? No, because the very next thing it might do is to run some process that will request a resource establishing a cyclic wait and causing deadlock Deadlocks Definition: Deadlock exists among a set of processes if Every process is waiting for an event This event can be caused only by another process in the set Event is the acquire or release of another resource One-lane bridge Remzi Arpaci-Dussea, Michael Swift 13 Remzi Arpaci-Dussea, Michael Swift 14 Four Conditions for Deadlock Coffman et. al. 1971 Necessary conditions for deadlock to exist: Mutual Exclusion At least one resource must be held is in non-sharable mode Hold and wait There exists a process holding a resource, and waiting for another No preemption Resources cannot be preempted Circular wait There exists a set of processes {P 1, P 2, P N, such that P 1 is waiting for P 2, P 2 for P 3,. and P N for P 1 Dealing with Deadlocks Reactive Approaches: detect and recover Periodically check for evidence of deadlock For example, using a graph reduction algorithm Then need a way to recover Could blue screen and reboot the computer Could pick a victim and terminate that thread But this is only possible in certain kinds of applications Basically, thread needs a way to clean up if it gets terminated and has to exit in a hurry! Often thread would then retry from scratch Despite drawbacks, database systems do this All four conditions must hold for deadlock to occur Remzi Arpaci-Dussea, Michael Swift 15 Remzi Arpaci-Dussea, Michael Swift 16 4

Dealing with Deadlocks Proactive Approaches: Deadlock Prevention Prevent one of the 4 necessary conditions from arising. This will prevent deadlock from occurring Deadlock Avoidance Carefully allocate resources based on future knowledge Deadlocks are prevented Ignore the problem Pretend deadlocks will never occur Ostrich approach but surprisingly common! Representing Deadlock Two common ways of representing deadlock Vertices: Threads (or processes) in system Resources (anything of value, including locks and semaphores) Edges: Indicate thread is waiting for the other WFG: good for locks, RAG: good for buffers, devices Wait-For Graph waiting for T1 waiting for T2 Resource-Allocation Graph T1 wants held by y x held by wants T2 Remzi Arpaci-Dussea, Michael Swift 17 Remzi Arpaci-Dussea, Michael Swift 18 Deadlock Prevention #1 Approach Ensure 1 of 4 conditions cannot occur Negate each of the 4 conditions No single approach is appropriate (or possible) for all circumstances No mutual exclusion --> Make resource sharable Example: Read-only files Deadlock Prevention #2 No Hold-and-wait --> Two possibilities 1) Only request resources when have none Release resource before requesting next one Thread 1 lock(x); A += 10; unlock(x); lock(y); B += 20; unlock(y); lock(x); A += 30; unlock(x); Thread 2 lock(y); B += 10; unlock(y); lock(x); A += 20; unlock(x); lock(y); B += 30; unlock(y); Remzi Arpaci-Dussea, Michael Swift 19 Remzi Arpaci-Dussea, Michael Swift 20 5

Deadlock Prevention #2 No Hold-and-wait 2) Atomically acquire all resources at once Example #1: Single lock to protect all Deadlock Prevention #2 No Hold-and-wait 2) Atomically acquire all resources at once Example #2: New primitive to acquire two locks Thread 1 lock(z); A += 10; B += 20; A += B; A += 30; unlock(z); Thread 2 lock(z); B += 10; A += 20; A += B; B += 30; unlock(z); Thread 1 lock(x,y); A += 10; B += 20; A += B; unlock(y); A += 30; unlock(x); Thread 2 lock(x,y); B += 10; A += 20; A += B; unlock(x); B += 30; unlock(y); Remzi Arpaci-Dussea, Michael Swift 21 Remzi Arpaci-Dussea, Michael Swift 22 Deadlock Prevention #2 Problems w/ acquiring many resources atomically Low resource utilization Must make pessimistic assumptions about resource usage if (cond1) { lock(x); if (cond2) { lock(y); Starvation If need many resources, others might keep getting one of them Deadlock Prevention #3 No no preemption --> Preempt resources Example: A waiting for something held by B, then take resource away from B and give to A Only works for some resources (e.g., CPU and memory) Not possible if resource cannot be saved and restored Can t take away a lock without causing problems Remzi Arpaci-Dussea, Michael Swift 23 Remzi Arpaci-Dussea, Michael Swift 24 6

Deadlock Prevention #4 No circular wait --> Impose ordering on resources Give all resources a ranking; must acquire highest ranked first How to change Example? Problems? Deadlock Avoidance Dijkstra s Banker s Algorithm Avoid unsafe states of processes holding resources Unsafe states might lead to deadlock if processes make certain future requests When process requests resource, only give if doesn t cause unsafe state Problem: Requires processes to specify all possible future resource demands Remzi Arpaci-Dussea, Michael Swift 25 Remzi Arpaci-Dussea, Michael Swift 26 Banker s Algorithm example 1. I request a pot When a request is made pretend you granted it pretend all other legal requests were made can the graph be reduced? if so, allocate the requested resource if not, block the thread Remzi Arpaci-Dussea, Michael Swift 27 Remzi Arpaci-Dussea, Michael Swift 28 7

Allocation is OK; there is a way for me to complete, and then you can complete 2. request a pot Remzi Arpaci-Dussea, Michael Swift 29 Remzi Arpaci-Dussea, Michael Swift 30 Allocation is OK; there is a way for me to complete, and then you can complete 3a. request a pan Remzi Arpaci-Dussea, Michael Swift 31 Remzi Arpaci-Dussea, Michael Swift 32 8

NO! Both of us might be unable to complete! 3b. I request a pan Remzi Arpaci-Dussea, Michael Swift 33 Remzi Arpaci-Dussea, Michael Swift 34 Allocation is OK; there is a way for me to complete, and then you can complete Deadlock Detection and Recovery Detection Maintain wait-for graph of requests Run algorithm looking for cycles When should algorithm be run? Recovery: Terminate deadlock Reboot system (Abort all processes) Abort all deadlocked processes Abort one process in cycle Challenges How to take resource away from process? Undo effects of process (e.g., removing money from account) Must roll-back state to safe state (checkpoint memory of job) Could starve process if repeatedly abort it Remzi Arpaci-Dussea, Michael Swift 35 Remzi Arpaci-Dussea, Michael Swift 36 9

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%? 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) Remzi Arpaci-Dussea, Michael Swift 37 Remzi Arpaci-Dussea, Michael Swift 38 Summary: Handing Deadlock Deadlock prevention Ensure deadlock does not happen Ensure at least one of 4 conditions does not occur Deadlock avoidance Ensure deadlock does not happen Use information about resource requests to dynamically avoid unsafe situations Deadlock detection and recovery Allow deadlocks, but detect when occur Recover and continue Ignore Easiest and most common approach Remzi Arpaci-Dussea, Michael Swift 39 10