Deadlock CS 537 - Introduction to Operating Systems.

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Deadlock and Starvation
Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Chapter 7: Deadlocks.
02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying.
Deadlock CS Introduction to Operating Systems.
Ceng Operating Systems Chapter 2.4 : Deadlocks Process concept  Process scheduling  Interprocess communication  Deadlocks Threads.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 30 Handling Deadlock.
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.
Deadlock CSCI 444/544 Operating Systems Fall 2008.
CS 450 OPERATING SYSTEMS DEADLOCKS Manju Muralidharan Priya.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
Deadlocks Gordon College Stephen Brinton. Deadlock Overview The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 7: Deadlock Dr. Mohamed Hefeeda.
What we will cover…  The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock.
Monitors High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes. monitor monitor-name {
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Deadlocks.
System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention, Avoidance, and Detection Recovering from Deadlock Combined Approach.
Chapter 7 – Deadlock (Pgs 283 – 306). Overview  When a set of processes is prevented from completing because each is preventing the other from accessing.
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.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-6 Deadlocks Department of Computer Science and Software Engineering.
CHAPTER 8: DEADLOCKS System Model Deadlock Characterization
 The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock Avoidance  Deadlock.
Operating Systems (CS 340 D) Dr. Abeer Mahmoud Princess Nora University Faculty of Computer & Information Systems Computer science Department.
CS 346 – Chapter 7 Deadlock –Properties –Analysis: directed graph Handle –Prevent –Avoid Safe states and the Banker’s algorithm –Detect –Recover.
Lecture 12 Handling Deadlock – Prevention, avoidance and detection.
Chapter 8 Deadlocks. Objective System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
CS307 Operating Systems Deadlocks Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2012.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 6 Deadlocks Slide 1 Chapter 6 Deadlocks.
Deadlocks Introduction to Operating Systems: Module 7.
Deadlocks Copyright ©: University of Illinois CS 241 Staff1.
Operating Systems Unit 4: – Dining Philosophers – Deadlock – Indefinite postponement Operating Systems.
Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
7.1 CSE Department MAITSandeep Tayal 7: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
NETW 3005 Monitors and Deadlocks. Reading For this lecture, you should have read Chapter 7. NETW3005 (Operating Systems) Lecture 06 - Deadlocks2.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The 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);
Deadlock. Examples You can't get a job without experience; you can't get experience without a job. A set of blocked processes each holding a resource.
Chapter 7: Deadlocks. The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Chapter 7: Deadlocks.
OPERATING SYSTEM CONCEPTS AND PRACTISE
Process Management Deadlocks.
Chapter 7: Deadlocks.
Deadlock Detection & recovery
Chapter 6 : Deadlocks What is a Deadlock?
Operating Systems (CS 340 D)
Chapter 7: Deadlocks.
Operating System: DEADLOCKS
Chapter 7 Deadlocks.
Chapter 7: Deadlocks.
Deadlock B.Ramamurthy CSE421 1/11/2019 B.Ramamurthy.
Outline Deadlocks, dead lock prevention, avoidance.
Deadlock Prevention Restrain the ways request can be made.
CSc 552 Advanced Unix Process deadlock deadlock prevention
Deadlock B.Ramamurthy CSE421 2/23/2019 B.Ramamurthy.
Chapter 7: Deadlocks.
Lecture 26 Syed Mansoor Sarwar
Deadlock B.Ramamurthy CSE421 4/23/2019 B.Ramamurthy.
Deadlock B.Ramamurthy CSE421 5/1/2019 B.Ramamurthy.
OPERATING SYSTEMS DEADLOCKS.
Chapter 7: Deadlocks.
Deadlock B.Ramamurthy CSE421 8/28/2019 B.Ramamurthy.
Deadlock B.Ramamurthy CSE421 9/3/2019 B.Ramamurthy.
Presentation transcript:

Deadlock CS Introduction to Operating Systems

Defining Deadlock Deadlock is a situation where 2 or more processes are unable to proceed because they are waiting for shared resources. Three necessary conditions for deadlock –able to hold more than one resource at a time –unwilling to give up resources –cycle Break any one of these three conditions and deadlock is avoided

Example Imagine 4 cars at an intersection

Example Lanes are resources. Deadlock has occurred because –each car is holding 2 resources (lanes) –none of the cars is willing to backup –car 0 waits for car 1 which waits for car 2 which waits for car 3 which waits for car 0 this is a cycle If any ONE of the above conditions can be broken, deadlock would be broken

System Resource-Allocation Graph Tool for detecting deadlock Shows which process is waiting for which resource –called a request edge Shows which process is holding which resource –called an assignment edge

System Resource-Allocation Graph P0P0 P1P1 P2P2 R0R0 R1R1 R2R2 Assignment Edges Request Edges R 0 --> P 0 R 1 --> P 1 R 2 --> P 0 R 2 --> P 1 P 1 --> R 0 P 2 --> R 1

Dealing with Deadlock Three ways to deal with deadlock –never allow it to occur –allow it to occur, detect it, and break it –ignore it this is the most common solution requires programmers to write programs that don’t allow deadlock to occur

Not Allowing Deadlock to Occur Prevent processes from holding multiple resources –not very realistic hard (or impossible) to get anything done if not allowed to hold more than one resource –could provide multiple copies of resources not very economical some resources can’t be duplicated (semaphore)

Not Allowing Deadlock to Occur Preempt Processes –if process can’t get a resource, give up what it has either all at once OR as other processes request what it has Who gets preempted? Can lead to severe starvation If resource is easily saved and restored, this is may be okay –CPU, memory image

Not Allowing Deadlock to Occur Don’t allow cycles to happen Force requests in specific order –for example, must requests resources in ascending order –Process A may have to wait for B, but B will never have to wait for A Must know in advance what resources are going to be used

Not Allowing Deadlock to Occur Force processes to declare needed resources –process must do this before starting OS examines each processes requests and decides if a deadlock could occur –don’t allocate resource –conservative approach Again, process must know what it wants before hand

Detecting Deadlock There are available resources –avail[r] = # of resources not allocated r is number of different resources in system There exists a set of requests for resources –req[p][r] = # of units of resource r requested by process p There exists a set of resources allocated to a specific process –alloc[p][r] = # of units of resource r currently held by process p

Detecting Deadlock 2 Systems - one deadlocked and one not R0R0 R1R1 R2R2 P0P0 P1P1 P2P2 1) Deadlock Free System R0R0 R1R1 R2R2 P0P0 P1P1 P2P2 2) Deadlocked System

Detecting Deadlock Basic idea –examine the system for cycles –find any job that can satisfy all of its requests –assume it finishes and give its resources back to the system –remove the process’s node from the graph –repeat the process until no nodes left - no deadlock can’t remove any more nodes - deadlocked

Detecting Deadlock Algorithm boolean deadlock(int [][] req) { boolean [] done = new boolean[N];// N = # of processes initialize done to all false for(int p=0; p<N; p++) { find p such that (!done[p] && req[p] <= avail) if (p is not found) return true; // DEADLOCK else { avail += alloc[p]; done[p] = true; } return false;// NO deadlock }

Example of Algorithm Examine the following system R0R0 R1R1 R2R2 P0P0 P1P1 P2P2 r = 3; N = 5 avail[r] = {0, 0, 1 } req[n][r] = { (0, 0, 1), (1, 0, 0), (0, 0, 0) } alloc[n][r] = { (2, 0, 0), (0, 0, 2), (1, 1, 3) } Now trace through algorithm. Final solution: No deadlock.

Bankers Algorithm This is a method for detecting if allocating a could lead to deadlock Don’t allow allocation if deadlock could occur –conservative approach –requires processes declare up front what they need this is a processes credit

Bankers Algorithm boolean bankers (int p, int [] breq) { // do some error checking - make sure not exceeding credit, etc. avail -= breq; credit[p] -= breq; alloc[p] += breq; if( safe() ) return true; else restore avail, credit, and alloc to original values return false; } boolean safe() { return !deadlock(credit); }

Bankers Algorithm Consider the following set of credits, allocations, and availabilities Initial Credit Current Allocation units of resources = initial avail[r] = { 8, 7, 7 } avail[r] = { 4, 2, 3 } current credits for each process

Bankers Algorithm Consider the following requests: –P 3 requests: (2, 2, 2) overdrawn - denied –P 5 requests: (5, 0, 0) only 4 available - must wait –P 3 requests: (3, 1, 0) run algorithm (assume adjusted credits become requests for deadlock detection) notice this is conservative in this case, request can be granted

Deadlock Recover first, must be able to detect deadlock –use deadlock method described earlier running time is O(n 2 m) this can lead to poor performance if done on every request - processes requests resources frequently run deadlock algorithm at set intervals –how often is deadlock likely to occur? –how many processes will be affected?

Deadlock Recovery So what to do if deadlock is discovered? –OS can start killing processes –OS can revoke resources from processes Both of the above solutions will eventually end a deadlock –which processes to kill? –which resources to revoke?

Process Termination Two solutions –kill all deadlocked processes very costly (work is lost) –kill one process at a time until deadlock is broken high overhead (must re-run deadlock algorithm after each killing) still have cost of lost work which process to kill?

Process Termination Which process to kill? –must have some type of cost analysis kill lowest priority process kill youngest process (least amount of work done) kill process with most resources (gives greatest chance of ending deadlock) kill process with most outstanding resource requests (removes the most request edges from graph) Will have to restart killed processes later –avoid starvation

Resource Preemption Let processes stay alive but revoke some of their resources Again, must select process to preempt –take resources from biggest “hog” –take resources from youngest

Resource Preemption What to do with preempted process? –rollback to safe state difficult to determine what was safe state must keep extra state around example: semaphore –if preempt the semaphore from some process, it must be rolled back to way it was before entering critical section otherwise there is an inconsistant state Beware of starvation

Dining Philosophers Philosophers sitting around a dining table Philosophers only eat and think Need two forks to eat Exactly as many forks as philosophers Before eating, a philosopher must pick up the fork to his right and left When done eating, each philosopher sets down both forks and goes back to thinking

Dining Philosophers P 5 P 4P 3 P 2 P 1 P 0 F 0 F 1 F 2 F 3 F 4 F 5

Dining Philosophers Only one philosopher can hold a fork at a time One major problem what if all philosophers decide to eat at once? –if they all pick up the right fork first, none of them can get the second fork to eat –deadlock

Philosopher Deadlock Solutions Make every even numbered philosopher pick up the right fork first and every odd numbered philosopher pick up the left fork first Don’t let them all eat at once –a philosopher has to enter a monitor to eat –can only get into the monitor if no one else in it –only one philosopher is allowed to eat at a time

Philosopher Deadlock Solution monitor diningPhilosopher { int [] state = new int[5]; static final int THINKING = 0; static final int HUNGRY = 1; static final int EATING = 2; condition [] self = new condition[5]; public diningPhilosphers { for(int i=0; i<5; i++) state[i] = THINKING; } public pickup(int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self[i].wait; } public putDown(int i) { state[i] = THINKING; test((i+5) % 6); test((i+1) % 6); } private test(int i) { if( (state[(i + 5) % 6] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 6] != EATING) ) { state[i] = EATING; self[i].signal; }