CS 31: Intro to Systems Deadlock. Kevin Webb Swarthmore College April 21, 2015

Similar documents
CS 31: Intro to Systems Deadlock. Martin Gagne Swarthmore College April 25, 2017

Operating Systems ECE344. Ding Yuan

CS 318 Principles of Operating Systems

CS153: Deadlock. Chengyu Song. Slides modified from Harsha Madhyvasta, Nael Abu-Ghazaleh, and Zhiyun Qian

CS 153 Design of Operating Systems Winter 2016

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.

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

The Deadlock Problem

System Model. Types of resources Reusable Resources Consumable Resources

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

CS420: Operating Systems. Deadlocks & Deadlock Prevention

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

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

Deadlocks. Copyright : University of Illinois CS 241 Staff 1

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

CSE 120. Fall Lecture 8: Scheduling and Deadlock. Keith Marzullo

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

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

Chapter 7: Deadlocks

Deadlocks. Dr. Yingwu Zhu

Principles of Operating Systems

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

UNIT-5 Q1. What is deadlock problem? Explain the system model of deadlock.

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

The Deadlock Problem

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

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

Chapter 8: Deadlocks. The Deadlock Problem

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

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

Deadlock. Before We Begin. What is Deadlock? Traffic Jam as Example of Deadlock. CSE 120: Principles of Operating Systems. Lecture 6.

Principles of Operating Systems

The Deadlock Problem (1)

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

CS307 Operating Systems Deadlocks

Lecture 7 Deadlocks (chapter 7)

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

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

Lecture 9: Midterm Review

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

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

Deadlocks. Prepared By: Kaushik Vaghani

CMSC 412. Announcements

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

CSE 306/506 Operating Systems Deadlock. YoungMin Kwon

Chapter 7: Deadlocks

Concurrency: Deadlock and Starvation

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

Operating Systems: Quiz2 December 15, Class: No. Name:

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

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

Chapter 8: Deadlocks

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

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

Deadlocks. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

UNIT:2. Process Management

Bridge Crossing Example

Deadlocks. Operating System Concepts - 7 th Edition, Feb 14, 2005

Operating Systems. Operating Systems Sina Meraji U of T

CSE 120 Principles of Operating Systems

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

ICS Principles of Operating Systems. Lectures Set 5- Deadlocks Prof. Nalini Venkatasubramanian

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

VI. Deadlocks Operating Systems Prof. Dr. Marc H. Scholl DBIS U KN Summer Term

CS450/550 Operating Systems

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

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

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

CSE 380 Computer Operating Systems

Last Class: Monitors. Real-world Examples

ECE519 Advanced Operating Systems

Deadlock 1 Chapter 6

Chapter 7: Deadlocks

Shared and Preemptable Resources. A few resources may be sharable. Anyone can write the system log.

Deadlocks. Deadlock Overview

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

Chapter 8: Deadlocks. The Deadlock Problem

UNIT 4 DEADLOCKS 4.0 INTRODUCTION

Deadlock and Starvation

Chapter 7: Deadlocks

Chapter 7: Deadlocks. Operating System Concepts 9th Edition DM510-14

CS307: Operating Systems

Concurrency: Deadlock and Starvation. Chapter 6

Deadlocks. The Deadlock Problem. Bridge Crossing Example. Topics

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Silberschatz, Galvin and Gagne 2013! CPU cycles, memory space, I/O devices! " Silberschatz, Galvin and Gagne 2013!

OPERATING SYSTEMS. Deadlocks

Deadlock and Starvation

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

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

More on Synchronization and Deadlock

Deadlock. Operating Systems. Autumn CS4023

CHAPTER 7: DEADLOCKS. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

Deadlock. CS 450 : Operating Systems Michael Lee

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

Contents. Chapter 8 Deadlocks

CSE Operating Systems

Module 7: Deadlocks. The Deadlock Problem

Transcription:

CS 31: Intro to Systems Deadlock Kevin Webb Swarthmore College April 21, 2015

Reading Quiz

Deadly Embrace The Structure of the THE-Multiprogramming System (Edsger Dijkstra, 1968) Also introduced semaphores Deadlock is as old as synchronization

What is Deadlock? Set of threads are permanently blocked Unblocking of one relies on progress of another But none can make progress! Example Threads A and B Resources X and Y A holding X, waiting for Y B holding Y, waiting for X holding Each is waiting for the other; will wait forever A waiting for X Y waiting for B holding

What is Deadlock? Deadlock is a problem that can arise: When processes compete for access to limited resources When threads are incorrectly synchronized Definition: Deadlock exists among a set of threads if every thread is waiting for an event that can be caused only by another thread in the set.

B D Traffic Jam as Example of Deadlock Cars A, B, C, D W X C Road W, X, Y, Z A Y Z Car A holds road space Y, waiting for space Z Cars deadlocked in an intersection Gridlock

B D Traffic Jam as Example of Deadlock Z A B A W Y X Z C Y X D C W Cars deadlocked in an intersection Resource Allocation Graph

Four Conditions for Deadlock 1. Mutual Exclusion Only one thread may use a resource at a time. 2. Hold-and-Wait Thread holds resource while waiting for another. 3. No Preemption Can t take a resource away from a thread. 4. Circular Wait The waiting threads form a cycle.

Four Conditions for Deadlock 1. Mutual Exclusion Only one thread may use a resource at a time. 2. Hold-and-Wait Thread holds resource while waiting for another. 3. No Preemption Can t take a resource away from a thread. 4. Circular Wait The waiting threads form a cycle.

Examples of Deadlock Memory (a reusable resource) total memory = 200KB T 1 requests 80KB T 2 requests 70KB T 1 requests 60KB (wait) T 2 requests 80KB (wait) Messages (a consumable resource) T 1 T 2 T 1 : receive M 2 from P 2 T 2 : receive M 1 from P 1 T 1 M 1 M 2 T 2

Banking, Revisited struct account { mutex lock; int balance; } Transfer(from_acct, to_acct, amt) { lock(from_acct.lock); lock(to_acct.lock) from_acct.balance -= amt; to_acct.balance += amt; } unlock(to_acct.lock); unlock(from_acct.lock);

If multiple threads are executing this code, is there a race? Could a deadlock occur? struct account { mutex lock; int balance; } If there s potential for a race/deadlock, what execution ordering will trigger it? Transfer(from_acct, to_acct, amt) { lock(from_acct.lock); lock(to_acct.lock) } from_acct.balance -= amt; to_acct.balance += amt; unlock(to_acct.lock); unlock(from_acct.lock); Clicker Choice Potential Race? A No No B Yes No C No Yes D Yes Yes Potential Deadlock?

Common Deadlock Thread 0 Transfer(acctA, acctb, 20); Thread 1 Transfer(acctB, accta, 40); Transfer( ) { lock(accta.lock); lock(acctb.lock); Transfer( ) { lock(acctb.lock); lock(accta.lock);

Common Deadlock Thread 0 Transfer(acctA, acctb, 20); Thread 1 Transfer(acctA, acctb, 40); Transfer( ) { lock(accta.lock); T 0 gets to here lock(acctb.lock); Transfer( ) { lock(acctb.lock); T 1 gets to here lock(accta.lock); T 0 holds A s lock, will make no progress until it can get B s. T 1 holds B s lock, will make no progress until it can get A s.

How to Attack the Deadlock Problem Deadlock Prevention Make deadlock impossible by removing a condition Deadlock Avoidance Avoid getting into situations that lead to deadlock Deadlock Detection Don t try to stop deadlocks Rather, if they happen, detect and resolve

How to Attack the Deadlock Problem Deadlock Prevention Make deadlock impossible by removing a condition Deadlock Avoidance Avoid getting into situations that lead to deadlock Deadlock Detection Don t try to stop deadlocks Rather, if they happen, detect and resolve

B D How Can We Prevent a Traffic Jam? Do intersections usually look like this one? A W Y X Z C We have road infrastructure (mechanisms) Cars deadlocked in an intersection We have road rules (policies)

B D Suppose we add north/south stop signs. Which condition would that eliminate? A. Mutual exclusion W X C B. Hold and wait A Y Z C. No preemption D. Circular wait E. More than one

B D Suppose we add a stop light. Which condition would that eliminate? A. Mutual exclusion W X C B. Hold and wait A Y Z C. No preemption D. Circular wait E. More than one

Deadlock Prevention Simply prevent any single condition for deadlock 1. Mutual exclusion Make all resources sharable 2. Hold-and-wait Get all resources simultaneously (wait until all free) Only request resources when it has none

Deadlock Prevention Simply prevent any single condition for deadlock 3. No preemption Allow resources to be taken away (at any time) 4. Circular wait Order all the resources, force ordered acquisition

Which of these conditions is easiest to give up to prevent deadlocks? A. Mutual exclusion (make everything sharable) B. Hold and wait (must get all resources at once) C. No preemption (resources can be taken away) D. Circular wait (total order on resource requests) E. I m not willing to give up any of these!

How to Attack the Deadlock Problem Deadlock Prevention Make deadlock impossible by removing a condition Deadlock Avoidance Avoid getting into situations that lead to deadlock Deadlock Detection Don t try to stop deadlocks Rather, if they happen, detect and resolve

Deadlock Avoidance Avoid situations that lead to deadlock Selective prevention Remove condition only when deadlock a possibility Works with incremental resource requests Resources are asked for in increments Do not grant request that can lead to a deadlock Requires knowledge of maximum resource requirements

Banker s Algorithm Fixed number of threads and resources Each thread has zero or more resources allocated System state: either safe or unsafe Depends on allocation of resources to threads Safe: deadlock is absolutely avoidable Can avoid deadlock by certain order of execution Unsafe: deadlock is possible (but not certain) May not be able to avoid deadlock

Safe, Unsafe, and Deadlock States Safe Deadlock Unsafe

Banker s Algorithm Given thread/resource claim matrix thread/resource allocation matrix resource availability vector Is there a thread ordering such that a thread can run to completion, return resources resources can then be used by another thread eventually, all the threads complete

Banker s Algorithm for Avoidance The Banker s Algorithm is the classic approach to deadlock avoidance for resources with multiple units 1. Assign a credit limit to each customer (thread) Maximum credit claim must be stated in advance 2. Reject any request that leads to a dangerous state A dangerous state is one where a sudden request by any customer for the full credit limit could lead to deadlock A recursive reduction procedure recognizes dangerous states 3. In practice, the system must keep resource usage well below capacity to maintain a resource surplus

B D How Can We Avoid a Traffic Jam? What are the incremental resources? Safe* state: No possibility of deadlock <= 3 cars in intersection A C Unsafe state: Deadlock possible, don t allow *Don t try this while driving

B D How Can We Avoid a Traffic Jam? Which condition is being prevented? A. Mutual exclusion B. Hold and wait C. No preemption D. Circular wait A C

Deadlock Avoidance Eliminates deadlock Must know max resource usage in advance Do we always know resources at compile time? Do we specify resources at run time? Could we?

How to Attack the Deadlock Problem Deadlock Prevention Make deadlock impossible by removing a condition Deadlock Avoidance Avoid getting into situations that lead to deadlock Deadlock Detection Don t try to stop deadlocks Rather, if they happen, detect and resolve

Deadlock Detection and Recovery Do nothing special to prevent/avoid deadlocks If they happen, they happen Periodically, try to detect if a deadlock occurred Do something to resolve it Reasoning Deadlocks rarely happen (hopefully) Cost of prevention or avoidance not worth it Deal with them in special way (may be very costly)

Detecting a Deadlock Construct resource graph Z Requires A B Identifying all resources Y X Tracking their use Periodically running detection algorithm D W C

Resource Allocation Graph Deadlock can be described using a resource allocation graph (RAG) The RAG consists of sets of vertices P = {T1, T2,..., Tn} of threads and R = {R1, R2,..., Rm} resources A directed edge from a thread to a resource, Pi Ri, implies that Pi has requested Rj A directed edge from a resource to a thread, Ri Pi, implies that Rj has been acquired by Pi Each resource has a fixed number of units If the graph has no cycles, deadlock cannot exist If the graph has a cycle, deadlock may exist

Recovery from Deadlock Abort all deadlocked threads / processes Will remove deadlock, but drastic and costly

Recovery from Deadlock Abort all deadlocked threads / processes Will remove deadlock, but drastic and costly Abort deadlocked threads one-at-at-time Do until deadlock goes away (need to detect) What order should threads be aborted?

Recovery from Deadlock Preempt resources (force their release) Need to select thread and resource to preempt Need to rollback thread to previous state Need to prevent starvation What about resources in inconsistent states Such as files that are partially written? Or interrupted message (e.g., file) transfers?

Which type of deadlock-handling scheme would you expect to see in a modern OS (Linux/Windows/OS X)? A. Deadlock prevention B. Deadlock avoidance C. Deadlock detection/recovery D. Something else

Other Thread Complications Deadlock is not the only problem Performance: too much locking? Priority inversion

Priority Inversion Problem: Low priority thread holds lock, high priority thread waiting for lock. What needs to happen: boost low priority thread so that it can finish, release the lock What sometimes happens in practice: low priority thread not scheduled, can t release lock Example: Mars Pathfinder

Sojourner Rover on Mars

Mars Rover Three periodic tasks: 1. Low priority: collect meteorological data 2. Medium priority: communicate with NASA 3. High priority: data storage/movement Tasks 1 and 3 require exclusive access to a hardware bus to move data. Bus protected by a mutex.

Mars Rover Failsafe timer (watchdog): if high priority task doesn t complete in time, reboot system Observation: uh-oh, this thing seems to be rebooting a lot, we re losing data JPL engineers later confessed that one or two system resets had occurred in their months of pre-flight testing. They had never been reproducible or explainable, and so the engineers, in a very human-nature response of denial, decided that they probably weren't important, using the rationale "it was probably caused by a hardware glitch".

What Happened: Priority Inversion H M L Low priority task, running happily. Time

What Happened: Priority Inversion H M Low priority task acquires mutex lock. L Time

What Happened: Priority Inversion H M Medium task starts up, takes CPU. L Blocked Time

What Happened: Priority Inversion H High priority task tries to acquire mutex, can t because it s already held. Blocked M L Blocked Time

What Happened: Priority Inversion H High priority task tries to acquire mutex, can t because it s already held. Blocked M L Blocked Low priority task can t give up the lock because it can t run - medium task trumps it. Time

What Happened: Priority Inversion H Blocked M L Blocked High priority is taking too long. Reboot! Time

Solution: Priority Inheritance H High priority task tries to acquire mutex, can t because it s already held. Blocked M L -> H Blocked Give to blue red s (higher) priority! Time

Solution: Priority Inheritance High priority finishes in time. H Blocked M L Blocked Release lock, revert to low priority. Blocked Time

Deadlock Summary Deadlock occurs when threads are waiting on each other and cannot make progress. Deadlock requires four conditions: Mutual exclusion, hold and wait, no resource preemption, circular wait Approaches to dealing with deadlock: Ignore it Living life on the edge (most common!) Prevention Make one of the four conditions impossible Avoidance Banker s Algorithm (control allocation) Detection and Recovery Look for a cycle, preempt/abort