Deadlocks. Copyright : University of Illinois CS 241 Staff 1

Similar documents
Deadlock CS 241. March 19, University of Illinois

CSCE Operating Systems Deadlock. Qiang Zeng, Ph.D. Fall 2018

Deadlocks. Copyright : University of Illinois CS 241 Staff 1

OPERATING SYSTEMS. Deadlocks

Bridge Crossing Example

Deadlock and Starvation

Classical Synchronization Problems. Copyright : University of Illinois CS 241 Staff 1

The Deadlock Problem (1)

Chapter 3: Deadlocks

Principles of Operating Systems

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

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

Operating Systems ECE344. Ding Yuan

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

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

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

Concurrency: Deadlock and Starvation. Chapter 6

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

Principles of Operating Systems

6. Concurrency: Deadlock

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.

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

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

Chapter 3. Deadlocks

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

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

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

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

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

CMPT 300 Introduction to Operating Systems

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

Process Management. Deadlock. Process Synchronization. Management Management. Starvation

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

System Model. Types of resources Reusable Resources Consumable Resources

Chapter 8: Deadlocks. The Deadlock Problem

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

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

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

Deadlock Risk Management

Chapter 7: Deadlocks

Deadlocks. Prepared By: Kaushik Vaghani

Chapter 6. Deadlocks

Operating Systems. Deadlocks

CS450/550 Operating Systems

Chapter 6 Concurrency: Deadlock and Starvation

Last Class: Synchronization Problems!

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

The Deadlock Problem

Last Class: Monitors. Real-world Examples

Deadlock Risk Management

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

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

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

The Slide does not contain all the information and cannot be treated as a study material for Operating System. Please refer the text book for exams.

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

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

CSC 539: Operating Systems Structure and Design. Spring 2005

The Deadlock Problem

Deadlocks: Detection & Avoidance

Deadlock 1 Chapter 6

CS307 Operating Systems Deadlocks

The deadlock problem

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

Operating Systems. Deadlock. User OS. Kernel & Device Drivers. Interface Programs. Brian Mitchell - Operating Systems

Back to synchronization

CS420: Operating Systems. Deadlocks & Deadlock Prevention

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

Deadlock. Rab Nawaz Jadoon DCS. Assistant Professor COMSATS Lahore Pakistan. Department of Computer Science

Deadlocks. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Operating Systems. Operating Systems Sina Meraji U of T

Yet another synchronization problem

Concurrency: Deadlock and Starvation. Chapter 6

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

Final Exam Review. CPSC 457, Spring 2016 June 29-30, M. Reza Zakerinasab Department of Computer Science, University of Calgary

Deadlocks. System Model

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

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

Chapter 7: Deadlocks

Chapter 8: Deadlocks

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

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

Chapter 7: Deadlocks CS370 Operating Systems

Chapter 3. Deadlocks

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

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

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

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

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

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

CSE 306/506 Operating Systems Deadlock. YoungMin Kwon

Learning Outcomes. Chapter 3. Deadlocks. Resources. Resources. Resources

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

More on Synchronization and Deadlock

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

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

Resources. Chapter 3. Deadlocks. Resources. Resources

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

CS 153 Design of Operating Systems Winter 2016

Transcription:

Deadlocks 1

Deadlock Which way should I go? 2

Deadlock I Oh can no! almost I m get stuck! across GRIDLOCK! 3

Deadlock Definition Deadlocked process Waiting for an event that will never occur Typically, but not necessarily, involves more than one process A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause How can a single process deadlock itself? 4

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

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 6

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 Note Most OSes do not prevent or deal with deadlocks 7

Deadlock: One-lane Bridge I always have to back up! 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 8

Addressing Deadlock Prevention Design the system so that deadlock is impossible Avoidance Construct a model of system states, then choose a strategy that, when resources are assigned to processes, will not allow the system to go to a deadlock state Detection & Recovery Check for deadlock (periodically or sporadically) and identify which processes and resources are involved Recover by killing one of the deadlocked processes and releasing its resources Manual intervention Have the operator reboot the machine if it seems too slow 9

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 10

Dining Philosophers had it all Mutual exclusion Exclusive use of forks Hold and wait condition Hold 1 fork, wait for next No preemption condition Cannot force another to undo their hold Circular wait condition Each waits for next neighbor to put down fork DEADLOCK! This is the best one to tackle 11

Formalizing circular wait: Resource allocation graphs Nodes Circle: Processes Square: Resources Arcs P1 is using R1 From resource to process = resource assigned to process From process to resource = process requests (and is waiting for) resource P1 R1 R2 P2 P2 requested R2 12

Resource allocation graphs Nodes Circle: Processes Square: Resources P1 acquires R1 R1 P1 Circular wait P1 requests R2 R2 Deadlock P2 requests R1 Processes P1 and P2 are in deadlock over resources R1 and r2 P2 P2 acquires R2 13

Dining Philosophers resource allocation graph If we use 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); } } 14

Dining Philosophers resource allocation graph If we use the trivial broken solution... P1 One node per philosopher One node per fork Everyone tries to pick up left fork P5 R1 R2 P2 Request edges R5 R3 R4 P4 P3 15

Dining Philosophers resource allocation graph If we use the trivial broken solution... One node per philosopher One node per fork Everyone tries to pick up left fork Everyone succeeds 16

Dining Philosophers resource allocation graph If we use the trivial broken solution... One node per philosopher One node per fork Everyone tries to pick up left fork Everyone succeeds Assignment edges 17

Dining Philosophers resource allocation graph If we use the trivial broken solution... One node per philosopher One node per fork Everyone tries to pick up left fork Everyone succeeds Everyone tries to pick up right fork Request edges 18

Dining Philosophers resource allocation graph If we use the trivial broken solution... One node per philosopher One node per fork Everyone tries to pick up left fork DEADLOCK! Everyone succeeds Everyone tries to pick up right fork Cycle = deadlock 19

Default Solution: Be an Ostrich Approach Do nothing! Deadlocked processes stay stuck Rationale Keeps the common path faster and more reliable Deadlock prevention, avoidance and detection/recovery are expensive If deadlock is rare, is it worth the overhead? 21

Deadlock Prevention Goal 1: devise resource allocation rules that 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 22

Deadlock Prevention Prevent any one of the 4 conditions Mutual exclusion Hold-and-wait No preemption Circular wait 23

Mutual Exclusion Processes claim exclusive control of the resources they require How to break it? 24

Mutual Exclusion Processes claim exclusive control of the resources they require How to break it? Non-exclusive access only Read-only access Probably not an option for most scenarios But be smart and try to use shared resources wisely Battle won! War lost Very bad at Goal #2 25

Hold and Wait Condition Processes hold resources already allocated to them while waiting for additional resources How to break it? 26

Hold and Wait Condition Processes hold resources already allocated to them while waiting for additional resources How to break it? All at once Force a process to request all resources it needs at one time Get all or nothing Release and try again If a process needs to acquire a new resource, it must first release all resources it holds, then reacquire all it needs Both Inefficient Potential of starvation 27

Hold and Wait Condition Processes hold resources already allocated to them while waiting for additional resources How to break it? Only one Result Process can only have one resource locked No circular wait! 28

Hold and Wait Condition Processes hold resources already allocated to them while waiting for additional resources Result No circular wait! Very constraining (mediocre job on Goal #2) Better than Rules #1 and #2, but... Often need more than one resource Hard to predict resource needs at the beginning Releasing and re-requesting is inefficient, complicates programming, might lead to starvation 29

No Preemption Condition Resources cannot be taken from processes holding them until used to completion How to break it? 30

No Preemption Condition Resources cannot be taken from processes holding them until used to completion How to break it? Let it all go If a process holding some resources is denied a further request, that process must release its original resources Inefficient! Take it all away int pthread_mutex_trylock(pthread_mutex_t *mutex); On success, pthread_mutex_trylock() returns 0. On error, one of the following values is returned: EBUSY: The mutex is already locked. If a process requests a resource that is held by another process, the OS may preempt the second process and force it to release its resources Waste of CPU and other resources! 31

No Preemption Condition Resources cannot be taken from processes holding them until used to completion Result 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! 32

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 How to break it? 33

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 How to break it? Guarantee no cycles Allow processes to access resources only in increasing order of resource id Not really fair or necessarily efficient 34

Dining Philosophers solution with numbered 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); } } 35

Dining Philosophers solution with numbered 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); } } 36

Dining Philosophers solution with numbered 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); } } 37

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)); } } 2 1 3 5 4 38

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)); } } 2 1 3 5 4 39

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)); } } 2 1 3 5 4 40

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)); } } 2 1 3 5 4 41

Ordered resource requests prevent deadlock Without numbering Cycle! 42

Ordered resource requests prevent deadlock With numbering 3 4 7 8 Contradiction: Must have requested 3 first! 43

Proof by M.C. Escher 44

45

Are we always in trouble without ordering resources? Not always 4 3 7 8 Ordered resource requests are sufficient to avoid deadlock, but not necessary Convenient, but may be conservative 46

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

Deadlock Detection Check to see if a deadlock has occurred! Single resource per type Can use wait-for graph Check for cycles How? 48

Wait for Graphs Easier to find cycles on this graph Resource Allocation Graph Corresponding Wait For Graph 49

Deadlock Recovery Get rid of the cycles in the wait for graph How many cycles are there? 50

Deadlock Recovery Options Kill all deadlocked processes and release resources Kill one deadlocked process at a time and release its resources Steal one resource at a time Rollback all or one of the processes to a checkpoint that occurred before they requested any resources Difficult to prevent indefinite postponement 51

Deadlock Recovery Have to kill one more Resource Allocation Graph Corresponding Wait For Graph 52

Deadlock Recovery Only have to kill one Resource Allocation Graph Corresponding Wait For Graph 53

Deadlock Recovery: Process Termination How should the aborted process be chosen? Process priority Current computation time and time to completion Amount of resources used by the process Amount of resources needed by the process to complete If this process is terminated, how many other processes will need to be terminated? Is process interactive or batch? 54

Deadlock Recovery: Resource Preemption Selecting a victim Minimize cost Rollback Return to some safe state Restart process for that state Challenge: Starvation Same process may always be picked as victim Fix: Include number of rollbacks in cost factor 55

Deadlock Avoidance Basic idea Resource manager tries to see the worst case that could happen It does not grant an incremental resource request to a process if this allocation might lead to deadlock 56

Deadlock Avoidance Approach Define a model of system states (SAFE, UNSAFE) Choose a strategy that guarantees that the system will not go to a deadlock state Multiple instance of each Resources Requires the maximum number of each resource needed for each process For each resource i, p.max[i] = maximum number of instances of i that p can request 57

Safe vs. Unsafe Safe Guarantee There is some scheduling order in which every process can run to completion even if all of them suddenly and simultaneously request their maximum number of resources From a safe state The system can guarantee that all processes will finish Unsafe state: no such guarantee A deadlock state is an unsafe state An unsafe state may not be a deadlock state Some process may be able to complete Overall a conservative/pessimistic approach 58

How to Compute Safety Banker s Algorithm (Dijkstra, 1965) Each customer tells banker the maximum number of resources it needs, before it starts Customer borrows resources from banker Customer returns resources to banker Banker only lends resources if the system will stay in a safe state after the loan 59