The Deadlock Problem

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

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

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

Chapter 7: Deadlocks

Chapter 8: Deadlocks. The Deadlock Problem

Chapter 7: Deadlocks

Chapter 7: Deadlocks

The Deadlock Problem

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

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

Chapter 8: Deadlocks

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

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Module 7: Deadlocks. The Deadlock Problem

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

Chapter 8: Deadlocks. Operating System Concepts with Java

Deadlocks. Deadlock Overview

Lecture 7 Deadlocks (chapter 7)

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

CMSC 412. Announcements

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

CS307: Operating Systems

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

CS307 Operating Systems Deadlocks

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

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

Deadlock Prevention. Restrain the ways request can be made. Mutual Exclusion not required for sharable resources; must hold for nonsharable resources.

Principles of Operating Systems

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

System Model. Types of resources Reusable Resources Consumable Resources

Chapter 7: Deadlocks

CSE Opera+ng System Principles

Principles of Operating Systems

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

Deadlocks. Prepared By: Kaushik Vaghani

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.

The Deadlock Problem (1)

Deadlocks. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Bridge Crossing Example

Module 6: Deadlocks. Reading: Chapter 7

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

CSC Operating Systems Fall Lecture - XII Deadlocks - III. Tevfik Ko!ar. Louisiana State University. October 6 th, 2009

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

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

Roadmap. Safe State. Deadlock Avoidance. Basic Facts. Safe, Unsafe, Deadlock State. Tevfik Koşar. CSC Operating Systems Spring 2007

Deadlock Risk Management

CS420: Operating Systems. Deadlocks & Deadlock Prevention

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

The Deadlock Problem

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

Deadlocks. Dr. Yingwu Zhu

Roadmap. Deadlock Prevention. Deadlock Prevention (Cont.) Deadlock Detection. Exercise. Tevfik Koşar. CSE 421/521 - Operating Systems Fall 2012

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

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

Operating Systems 2015 Spring by Euiseong Seo DEAD LOCK

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

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

Chapter 8: Deadlocks. The Deadlock Problem

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

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

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

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

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

Chapter 8: Deadlocks. The Deadlock Problem

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

Potential Deadlock Example

Chapter 7: Deadlocks CS370 Operating Systems

Deadlock. A Bit More on Synchronization. The Deadlock Problem. Deadlock Characterization. Operating Systems 2/7/2005. CSC 256/456 - Spring

Chapter 7: Deadlocks. Chapter 7: Deadlocks. System Model. Chapter Objectives

Chapter - 4. Deadlocks Important Questions

CS370 Operating Systems

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

Chapter 7: Deadlocks

CS370 Operating Systems

Chapter 7: Deadlocks. Chapter 7: Deadlocks. Deadlock Example. Chapter Objectives

UNIT-3 DEADLOCKS DEADLOCKS

Unit-03 Deadlock and Memory Management Unit-03/Lecture-01

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

University of Babylon / College of Information Technology / Network Department. Operating System / Dr. Mahdi S. Almhanna & Dr. Rafah M.

Deadlock. Operating Systems. Autumn CS4023

CS370 Operating Systems

Deadlock Risk Management

CS370 Operating Systems

The deadlock problem

Operating System: Chap7 Deadlocks. National Tsing-Hua University 2016, Fall Semester

Deadlocks. The Deadlock Problem. Bridge Crossing Example. Topics

OPERATING SYSTEMS. Deadlocks

Fall 2015 COMP Operating Systems. Lab 06

CS370 Operating Systems

Deadlock. Chapter Objectives

TDDB68 + TDDD82. Lecture: Deadlocks

Introduction to Deadlocks

CSC 539: Operating Systems Structure and Design. Spring 2005

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

Deadlocks. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Contents. Chapter 8 Deadlocks

Outlook. Deadlock Characterization Deadlock Prevention Deadlock Avoidance

csci 3411: Operating Systems

When Deadlock Happens

Transcription:

Deadlocks

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. Example System has 2 disk drives. P1 and P2 each hold one disk drive and each needs another one. Example semaphores A and B, initialized to 1 P0 P1 wait (A); wait(b) wait (B); wait(a)

System Model Resource types R 1, R 2,..., R m. CPU cycles, memory space, I/O devices. Each resource type R i has W i instances. Each process utilizes a resource as follows: Request. Use. Release.

Necessary Conditions for Deadlock Mutual exclusion: only one process at a time can use a resource. Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. No preemption: a resource can be released only voluntarily by the process holding it. Circular wait: there exists a set {P 0, P 1,, P n } of waiting processes such that P 0 is waiting for a resource that is held by P 1, P 1 is waiting for a resource that is held by P 2,, P n 1 is waiting for a resource that is held by P n, and P n is waiting for a resource that is held by P 0.

Resource Allocation Graph A set of vertices V and a set of edges E. V is partitioned into two types: P = {P 1, P 2,, P n }, the set of processes. R = {R 1, R 2,, R m }, a set consisting of all resource types. Request edge directed edge Pi R j. Assignment edge directed edge Rj P i.

Resource Allocation Graph Process. Resource Type with 4 instances. Pi requests instance of R j. Pi is holding an instance of R j. P i R j P i R j

Example Graph

Deadlock Rules If graph contains no cycles then no deadlock. If graph contains a cycle then, if only one instance per resource type, deadlock. if several instances per resource type, possibility of deadlock.

Resource Allocation Graph (Deadlock?)

Graph With Cycle (Deadlock?)

Handling Deadlocks Ensure that the system will never enter a deadlock state. Deadlock prevention. Allow the system to enter a deadlock state and then recover. Deadlock detection. Ignore the problem and pretend that deadlocks never occur in the system. Used by most operating systems, including Linux. The Ostrich Algorithm. Why might this make sense?

Deadlock Prevention Recall our preconditions for deadlock: Mutual exclusion. Hold and wait. No preemption. Circular wait. If we can exclude any one, we can prevent deadlock. Mutual Exclusion?

Hold and Wait Require processes to request all resources at startup (atomically). Require processes to release all resources before requesting new ones. Drawbacks?

No Preemption 1 If a process that is holding some resources requests another resource that cannot be allocated to it, then all resources held are released. Preempted resources are added to the list of resources for which the process is waiting. Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting.

No Preemption 2 If a process requests resources that are held, check to see if the holding process is waiting. If it is waiting, preempt its resources. If resources are held by a non-waiting process, requesting process must wait. Any drawbacks to the two preemption schemes?

Circular Wait Impose a total ordering of all resource types, and require that each process requests resources in increasing order of enumeration. Why this works: It is not possible to have a set of processes such that every process is waiting for a resource with a higher number than the number of a currently held resource. Any drawbacks to this algorithm?

Deadlock Avoidance Our deadlock prevention methods put restrictions on how processes could request resources. Deadlock avoidance mechanisms require the OS to keep track of requests and resources to make sure that no deadlock can occur. This will require the system to have some a- priori information about the requests that a process could make.

Deadlock Avoidance Each process declares the max number of resources of each type it may need. System examines the resource-allocation state to ensure that there can never be a circularwait condition. If a request could lead to a circular wait, the process must wait until the state changes. Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes.

Safe States System is in safe state if there exists a sequence <P 1, P 2,, P n > of ALL the processes is the systems such that for each P i, the resources that P i can still request can be satisfied by currently available resources + resources held by all the P j, with j < i. I.e. There remains some way to grant everyone's maximum request. (Assumption is that all processes terminate eventually.)

Safe State Example Example from the book: Assume that a system has 12 tape drives. This is a safe state: MAX ALLOC P0 10 5 P1 4 2 P2 9 2 The sequence (P1, P0, P2) works. MAX ALLOC P0 10 5 P1 4 2 P2 9 2

Safe State Example This is not a safe state: MAX ALLOC P0 10 5 P1 4 2 P2 9 3 P1 is the only process who's max request could still be granted. When it exits, only 4 free drives. If both P0 and P2 then request their max drives, deadlock occurs. An unsafe state does not guarantee deadlock.

Resource Allocation Graph Algorithm Only works if there is at most one resource of each type. Claim edge P i -> R j indicates that process P j may request resource R j ; represented by a dashed line. Claim edge converts to request edge when a process requests a resource. Request edge converted to an assignment edge when the resource is allocated to the process. When a resource is released by a process, assignment edge reconverts to a claim edge. Resources must be claimed a priori in the system.

Resource Allocation Graph Algorithm Suppose that process Pi requests a resource R j. Grant request only if converting the request edge to an assignment edge does not result in the formation of a cycle.

Banker's Algorithm Multiple resource instances. Each process must declare maximum use. Any process that makes a request that would lead to an unsafe state must wait. When a process gets all its resources it must return them in a finite amount of time.

Banker's Algorithm Data Structures n processes, m resource types. Available: Vector of length m. Available[j] = number of available resources of type R j. Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of resource type R j. Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of R j. Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of R j to complete its task. Need [i,j] = Max[i,j] Allocation [i,j].

Banker's Algorithm: Checking For Safety 1.Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish [i] = false for i = 0, 1,, n- 1. 2.Find an i such that both: (a) Finish[i] = false (b) Need i Work If no such i exists, go to step 4. 3. Work = Work + Allocation i Finish[i] = true go to step 2. 4.If Finish[i] == true for all i, then the system is in a safe state.

Banker's Algorithm: Requesting Resources Request i = request vector for process P i. 1.If Request i Need i go to step 2. Otherwise, raise error condition. 2.If Request i Available, go to step 3. Otherwise P i must wait, since resources are not available. 3.Pretend to allocate requested resources to P i by modifying the state as follows: Available = Available Request; Allocation i = Allocation i + Request i ; Need i = Need i Request i ; If safe the resources are allocated to P i. If unsafe P i must wait, and the old resource-allocation state is restored.

Banker's Algorithm Analysis Thoughts on running time of safety check? An drawbacks to the bankers algorithm?

Deadlock Detection: Single Instance of Each Resource Maintain wait-for graph Nodes are processes. P i P j if P i is waiting for P j. Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a deadlock. An algorithm to detect a cycle in a graph requires an order of n 2 operations, where n is the number of vertices in the graph.

Wait-for Graph Why bother collapsing the graph?

Deadlock Detection With Multiple Resource Instances Big picture: Periodically we check the system for deadlocks. The deadlock check algorithm is very similar to the safety check algorithm we saw before. Instead of looking for unsafe states, we look for deadlocked states.

Deadlock Detection Data Structures Available: A vector of length m indicates the number of available resources of each type. Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process. Request: An n x m matrix indicates the current request of each process. If Request [i j ] = k, then process P i is requesting k more instances of resource type. R j.

Deadlock Detection Algorithm 1.Let Work and Finish be vectors of length m and n, respectively Initialize: (a) Work = Available (b)for i = 1,2,, n, if Allocation i 0, then Finish[i] = false;otherwise, Finish[i] = true. 2.Find an index i such that both: (a) Finish[i] == false (b)request i Work If no such i exists, go to step 4.

Deadlock Detection Algorithm 3.Work = Work + Allocation i Finish[i] = true go to step 2. 4.If Finish[i] == false, for some i, 1 i n, then the system is in deadlock state. Moreover, if Finish[i] == false, then P i is deadlocked.

Deadlock Recovery: Process Termination Abort all deadlocked processes. Abort one process at a time until the deadlock cycle is eliminated. In which order should we choose to abort? Priority of the process. How long process has computed, and how much longer to completion. Resources the process has used. Resources process needs to complete. How many processes will need to be terminated. Is process interactive or batch?

Deadlock Recovery: Resource Preemption Selecting a victim minimize some cost measure. Rollback return to some safe state, restart process for that state. Can be tricky. Starvation same process may always be picked as victim Include number of rollbacks in cost factor.

Acknowledgments Portions of these slides are taken from Power Point presentations made available along with: Silberschatz, Galvin, and Gagne. Operating System Concepts, Seventh Edition. Original versions of those presentations can be found at: http://os-book.com/