Operating Systems COMP 4850/CISG 5550 Deadlock Avoidance Dr. James Money.

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.
Deadlocks Today Resources & deadlocks Dealing with deadlocks Other issues Next Time Memory management.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
EEE 435 Principles of Operating Systems Deadlock Avoidance, Prevention, and Wrap-Up (Modern Operating Systems 3.5, 3.6, and 3.7)
Chapter 3 Deadlocks TOPICS Resource Deadlocks The ostrich algorithm
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Tanenbaum Ch 6 Silberschatz Ch 7
Chapter 3 Deadlocks - Αδιέξοδα 3.1. Resource
With slides from C. Griwodz, K. Li, A. Tanenbaum and M. van Steen
1 CS 333 Introduction to Operating Systems Class 7 - Deadlock Jonathan Walpole Computer Science Portland State University.
Deadlocks. 2 System Model There are non-shared computer resources –Maybe more than one instance –Printers, Semaphores, Tape drives, CPU Processes need.
The Banker’s Algorithm for A Single Resource
Deadlock CSCI 444/544 Operating Systems Fall 2008.
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Deadlock Detection with One Resource of Each Type (1)
02/19/2008CSCI 315 Operating Systems Design1 Deadlock Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
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.
Deadlock Characterization
Deadlocks.
Deadlock Chapter 3 Thursday, February 22, Today’s Schedule Assignment #4 from Chapter 3 posted Deadlock - Chapter 3  Skip multiple resources (3.4.2.
CH 3 Deadlock When 2 (or more) processes remain blocked forever!
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks Modified.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance 3.6. Deadlock prevention.
1 Deadlocks 2 Resources Examples of computer resources –printers –tape drives –tables Processes need access to resources in reasonable order Suppose.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
1 Deadlocks Chapter 3. 2 Resources Examples of computer resources –printers –tape drives –tables Processes need access to resources in reasonable order.
Operating Systems 软件学院 高海昌 Operating Systems Gao Haichang, Software School, Xidian University 22 Contents  1. Introduction** 
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
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.
1 MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
Resource Management Reusable  Disk blocks  File descriptors  Semaphores Consumable  Messages  Packets.
CS 346 – Chapter 7 Deadlock –Properties –Analysis: directed graph Handle –Prevent –Avoid Safe states and the Banker’s algorithm –Detect –Recover.
CSC 322 Operating Systems Concepts Lecture - 28: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Chapter 6 Concurrency: Deadlock and Starvation
CS333 Intro to Operating Systems Jonathan Walpole.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money.
CH 3 Deadlock When 2 (or more) processes remain blocked forever!
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Deadlock.
Deadlocks References –text: Tanenbaum ch.3. Deadly Embrace Deadlock definition –A set of process is dead locked if each process in the set is waiting.
Deadlock Operating Systems (CC2011NI) -Mr. Pranoy Man Pradhan.
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.
Sistem Operasi IKH311 Deadlock. 2 Resources Examples of computer resources printers tape drives tables Processes need access to resources in reasonable.
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.
Operating Systems, Summer Semester 2011
Operating Systems, Winter Semester 2011
MODERN OPERATING SYSTEMS Third Edition ANDREW S
ITEC 202 Operating Systems
Deadlocks References text: Tanenbaum ch.3.
Lecture 18: Deadlock: Conditions, Detection and Avoidance (cont.)
Lecture 19: Deadlock: Conditions, Detection and Avoidance
Process Deadlocks.
Deadlocks Definition A set of processes is in a Deadlock state when every process in the set is waiting for an event that can only be caused by another.
CS 333 Introduction to Operating Systems Class 7 - Deadlock
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Lecture 19: Deadlock: Conditions, Detection and Avoidance
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
OPERATING SYSTEMS DEADLOCKS.
OPERATING SYSTEMS DEADLOCKS.
Introduction to Deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Chapter 3 Deadlocks -For MCA, PU
Presentation transcript:

Operating Systems COMP 4850/CISG 5550 Deadlock Avoidance Dr. James Money

Introduction to Deadlocks Deadlocks are formally defined by Deadlocks are formally defined by –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 Since they are all waiting, none of them will wake up Since they are all waiting, none of them will wake up Assumption of no interrupts and single threads Assumption of no interrupts and single threads

Conditions for Deadlock 1. Mutual Exclusion – each resource is either currently assigned to one process or is available 2. Hold and Wait – processes currently holding resources can request new resources 3. No preemption – Resources previously granted cannot forcibly be taken away from the process. They must be released by the process 4. Circular Wait – there must be a circular chain of 2+ processes, each whom is waiting for a resource held by the next member of the chain

Conditions for Deadlock All four conditions must exist for a deadlock to occur All four conditions must exist for a deadlock to occur If one is absent, deadlock cannot occur If one is absent, deadlock cannot occur Many of these are related to system policy choices Many of these are related to system policy choices

Dealing with Deadlocks Ignore the problem, maybe it will ignore you? Ignore the problem, maybe it will ignore you? –Used by UNIX and Windows Detection and Recovery Detection and Recovery Dynamic avoidance by careful resource allocation Dynamic avoidance by careful resource allocation Prevention by structurally negating one of the four conditions for deadlocks Prevention by structurally negating one of the four conditions for deadlocks

Deadlock Avoidance In deadlock detection, we assumed all resources were requested simultaneously In deadlock detection, we assumed all resources were requested simultaneously However, in reality, we request them one at a time However, in reality, we request them one at a time The system must decide if granting the resource is safe or not The system must decide if granting the resource is safe or not We consider careful resource allocation now We consider careful resource allocation now

Resource Trajectories The main algorithm is based on the idea of safe states The main algorithm is based on the idea of safe states We first consider a graphic version of this model first We first consider a graphic version of this model first The does not immediately turn into an algorithm, but provide a good intuition into the problem The does not immediately turn into an algorithm, but provide a good intuition into the problem

Resource Trajectories The following slide shows a model for dealing with two processes and two resources The following slide shows a model for dealing with two processes and two resources The horizontal axis represents number of instructions executed for process A The horizontal axis represents number of instructions executed for process A The vertical axis represents number of instructions executed for process B The vertical axis represents number of instructions executed for process B

Resource Trajectories At I 1, A requests a printer and at I 2, A requests a plotter At I 1, A requests a printer and at I 2, A requests a plotter The printer and plotter are released at I 3 and I 4, respectively The printer and plotter are released at I 3 and I 4, respectively Process B needs the plotter from I 5 to I 7 and the printer from I 6 to I 8 Process B needs the plotter from I 5 to I 7 and the printer from I 6 to I 8

Resource Trajectories

Every point in the picture represents a joint state of the two processes Every point in the picture represents a joint state of the two processes Initially, the state is p, with nothing having been executed Initially, the state is p, with nothing having been executed If the scheduler runs A first, then we get to point q If the scheduler runs A first, then we get to point q Then process B runs, and we get to r Then process B runs, and we get to r

Resource Trajectories When A cross the line for I 1, it requests and it granted the printer When A cross the line for I 1, it requests and it granted the printer When B reaches t, it requests the plotter When B reaches t, it requests the plotter The shaded regions are of particular interest for deadlocks The shaded regions are of particular interest for deadlocks

Resource Trajectories The slanted lines from southwest to northeast is when both processes have the printer The slanted lines from southwest to northeast is when both processes have the printer The slanted lines from northwest to southeast is when both processes have the plotter The slanted lines from northwest to southeast is when both processes have the plotter Both of these are deadlock regions because of mutual exclusion Both of these are deadlock regions because of mutual exclusion

Resource Trajectories If the system enters the box bounded by I 1, I 2, I 5, and I 6, it will eventually deadlock when it reaches the intersection of I 2 and I 6 If the system enters the box bounded by I 1, I 2, I 5, and I 6, it will eventually deadlock when it reaches the intersection of I 2 and I 6 The entire box is unsafe The entire box is unsafe At point t, the only safe course of action is to run process A until it gets to I 4 At point t, the only safe course of action is to run process A until it gets to I 4 Any trajectory outside of this box to u will do Any trajectory outside of this box to u will do

Resource Trajectories The important thing to notice at point t, is that process B is requesting a resource The important thing to notice at point t, is that process B is requesting a resource The system must decide to grant it or not The system must decide to grant it or not If it is granted, it enters an unsafe region and a possible deadlock If it is granted, it enters an unsafe region and a possible deadlock To avoid this, we should suspect process A until is requests and releases the plotter To avoid this, we should suspect process A until is requests and releases the plotter

Safe and Unsafe States We will use the vectors and matrices from deadlock detection We will use the vectors and matrices from deadlock detection A state is said to be safe if it is not deadlocked and there is some scheduling order so that each process can run to completion even if they requests their maximum number of resources immediately A state is said to be safe if it is not deadlocked and there is some scheduling order so that each process can run to completion even if they requests their maximum number of resources immediately

Safe and Unsafe States

The prior example is safe since there is a sequence of allocations that allows the processes to complete The prior example is safe since there is a sequence of allocations that allows the processes to complete Now, let us consider an unsafe example Now, let us consider an unsafe example

Safe and Unsafe States

So, the decision to go from (a) to (b) in the prior slide moves us from a safe state to an unsafe state So, the decision to go from (a) to (b) in the prior slide moves us from a safe state to an unsafe state We should have not granted process A’s request to prevent a possible deadlock We should have not granted process A’s request to prevent a possible deadlock Note: An unsafe state is not necessarily a deadlock! Note: An unsafe state is not necessarily a deadlock! Only a safe state guarantees all processes will finish Only a safe state guarantees all processes will finish In an unsafe state, it may or may not finish In an unsafe state, it may or may not finish

Banker’s Algorithm for Single Resource The scheduling algorithm for handling single resources is due to Dijkstra(1965) and is known as the banker’s algorithm The scheduling algorithm for handling single resources is due to Dijkstra(1965) and is known as the banker’s algorithm It is an extension of the deadlock detection algorithm It is an extension of the deadlock detection algorithm It is modeled similar to the way a small town banker deals with customers whom he has given a line of credit It is modeled similar to the way a small town banker deals with customers whom he has given a line of credit

Banker’s Algorithm for Single Resource The algorithm checks to see if granting a resource leads to a safe or unsafe state The algorithm checks to see if granting a resource leads to a safe or unsafe state The banker gives out the various credit limits, which add up to 22 The banker gives out the various credit limits, which add up to 22 However, s/he can only lend out 10 units at a time However, s/he can only lend out 10 units at a time The units can be tape drives, the customers are processes and the banker is the OS The units can be tape drives, the customers are processes and the banker is the OS

Banker’s Algorithm for Single Resource

In (b), the state is safe In (b), the state is safe In (c) is unsafe In (c) is unsafe In (b), if anyone but C requests a resource, it can be delayed until C is finished In (b), if anyone but C requests a resource, it can be delayed until C is finished (c) does not have to result in a deadlock, but we want to avoid this state (c) does not have to result in a deadlock, but we want to avoid this state

Banker’s Algorithm for Single Resource The algorithm considers each request as it occurs and checks to see if it leads to a safe state The algorithm considers each request as it occurs and checks to see if it leads to a safe state If it does, the request is granted If it does, the request is granted If it does not, the request is postponed If it does not, the request is postponed To check safety, we see if we have enough resources to satisfy some process To check safety, we see if we have enough resources to satisfy some process The resources are released, and the next closest customer of the limit is checked, and so on The resources are released, and the next closest customer of the limit is checked, and so on All processes must be able to finish to be safe All processes must be able to finish to be safe

Banker’s Algorithm for Multiple Resources We can now generalize the banker’s algorithm for multiple resources We can now generalize the banker’s algorithm for multiple resources This time we use a matrix of assigned and request resources similar to before This time we use a matrix of assigned and request resources similar to before

Banker’s Algorithm for Multiple Resources

1. Look at a row, R, whose unmet resource needs are smaller than or equal to A(R<=A). If no row exists, the system will eventually deadlock 2. Assume the process of the chosen row requests its resources and finishes. Mark the process as terminated and add its resources to vector A 3. Repeat 1 and 2 until either all the processes are marked as terminated, which means the state is safe, or until a deadlock occurs, which means the state is unsafe

Banker’s Algorithm for Multiple Resources The current state is safe in the figure The current state is safe in the figure Suppose process B requests a scanner Suppose process B requests a scanner –This is granted since the resulting state is safe –Process D, then process A or E finishes, followed by the rest

Banker’s Algorithm for Multiple Resources After B is granted one of the two remaining scanners, suppose E wants the last printer After B is granted one of the two remaining scanners, suppose E wants the last printer –This reduces A=( ) –This leads to a potential deadlock –This request must be deferred

Houston, we have a problem! This has been highly studied This has been highly studied However, it suffers from a major flaw: However, it suffers from a major flaw: –It is useless –It needs to know the total resource needs of a program in advance –In addition, the number of processes is dynamic