Deadlocks. Prepared By: Kaushik Vaghani

Similar documents
Chapter 7: Deadlocks

The Deadlock Problem

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

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

Chapter 8: Deadlocks

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

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

Chapter 7: Deadlocks

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

Chapter 8: Deadlocks. The Deadlock Problem

Lecture 7 Deadlocks (chapter 7)

Chapter 7: Deadlocks

Deadlocks. Deadlock Overview

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

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

Module 7: Deadlocks. The Deadlock Problem

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

System Model. Types of resources Reusable Resources Consumable Resources

Chapter 7: Deadlocks

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

Chapter 8: Deadlocks. Operating System Concepts with Java

Principles of Operating Systems

CSE Opera+ng System Principles

CS307: Operating Systems

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

CS307 Operating Systems Deadlocks

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

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

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

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.

Chapter 8: Deadlocks. The Deadlock Problem

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

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

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

CMSC 412. Announcements

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

The Deadlock Problem (1)

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

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

Principles of Operating Systems

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

Deadlock Risk Management

The Deadlock Problem

Bridge Crossing Example

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

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

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 9 th Edition! Silberschatz, Galvin and Gagne 2013!

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

Deadlocks. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

CS420: Operating Systems. Deadlocks & Deadlock Prevention

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

The Deadlock Problem

Deadlocks. Dr. Yingwu Zhu

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Chapter 8: Deadlocks. The Deadlock Problem

Operating Systems 2015 Spring by Euiseong Seo DEAD LOCK

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

CHAPTER 7 - DEADLOCKS

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

Deadlock. Operating Systems. Autumn CS4023

UNIT-3 DEADLOCKS DEADLOCKS

Module 6: Deadlocks. Reading: Chapter 7

Deadlock. Chapter Objectives

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

OPERATING SYSTEMS. Deadlocks

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

Chapter 7: Deadlocks

Deadlocks. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Contents. Chapter 8 Deadlocks

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

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

Deadlock Risk Management

Operating systems. Lecture 5. Deadlock: System Model. Deadlock: System Model. Process synchronization Deadlocks. Deadlock: System Model

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

Potential Deadlock Example

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

Chapter 7: Deadlocks CS370 Operating Systems

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

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

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

Deadlocks. The Deadlock Problem. Bridge Crossing Example. Topics

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

Introduction to Deadlocks

CS370 Operating Systems

CSC 539: Operating Systems Structure and Design. Spring 2005

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

Chapter - 4. Deadlocks Important Questions

Fall 2015 COMP Operating Systems. Lab 06

Outlook. Deadlock Characterization Deadlock Prevention Deadlock Avoidance

CS370 Operating Systems

CS370 Operating Systems

Deadlocks. System Model

Operating Systems. Deadlocks. Stephan Sigg. November 30, Distributed and Ubiquitous Systems Technische Universität Braunschweig

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

Resource Allocation and Deadlock Handling

The deadlock problem

TDDB68 + TDDD82. Lecture: Deadlocks

Transcription:

Deadlocks Prepared By : Kaushik Vaghani

Outline System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection & Recovery

The Deadlock Problem A deadlock consists of a set of blocked processes, each holding a resource and waiting to acquire a resource held by another process in the set Example #1 A system has 2 disk drives P 1 and P 2 each hold one disk drive and each needs the other one Example #2 Semaphores A and B, initialized to 1 P 0 P 1 wait (A); ( wait(b wait (B); ( wait(a

Bridge Crossing Example Traffic only in one direction The resource is a one-lane bridge If a deadlock occurs, it can be resolved if one car backs up ( rollback (preempt resources and Several cars may have to be backed up if a deadlock occurs Starvation is possible

System Model Resource types R 1, R 2,..., R m CPU cycles, memory space, I/O devices Each resource type R i has 1 or more instances Each process utilizes a resource as follows: request use release

Deadlock Characterization Necessary Conditions Deadlock can arise if four conditions hold simultaneously. Mutual exclusion: At least one resource must be held in a nonsharable mode; that is, only one process at a time can use the resource. If another process requests that resource, the requesting process must be delayed until the resource has been released. Hold and wait: A process holding at least one resource is waiting to acquire additional resources held by other processes. No preemption: Resource cannot be preempted; that is, a resource can be released only voluntarily by the process holding it after that process has completed its task. Circular wait: there exists a set {P0, P1,, Pn} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2,, Pn 1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P0.

Resource-Allocation Graph Deadlocks can be described more precisely in terms of a directed graph called a system 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 consisting of all the processes in the system R = {R 1, R 2,, R m }, the set consisting of all resource types in the system request edge directed edge P 1 R j assignment edge directed edge R j P i

(. Cont ) Resource-Allocation Graph Process Resource Type with 4 instances P i requests instance of R j P i R j P i is holding an instance of R j P i R j

Resource Allocation Graph With A Deadlock Before P 3 requested an instance of R 2 After P 3 requested an instance of R 2 Given the definition of a resource-allocation graph, it can be shown that, if the graph contains no cycles, then no process in the system is deadlocked. If the graph does contain a cycle, then a deadlock may exist.

Graph With A Cycle But No Deadlock Process P 4 may release its instance of resource type R 2. That resource can then be allocated to P3, thereby breaking the cycle.

Relationship of cycles to deadlocks If a resource allocation graph contains no cycles no deadlock If a resource allocation graph contains a cycle and if only one instance exists per resource type deadlock If a resource allocation graph contains a cycle and and if several instances exists per resource type possibility of deadlock

Methods for Handling Deadlocks

Methods for Handling Deadlocks Prevention Ensure that the system will never enter a deadlock state. Avoidance Ensure that the system will never enter an unsafe state. Detection & Recovery Allow the system to enter a deadlock state, detect it, and then recover. Do Nothing Ignore the problem and pretend that deadlocks never occur in the system. Used by most OS, including Unix and Windows.

Methods for Handling Deadlocks (Cont.) To ensure that deadlocks never occur, the system may use either a deadlock-prevention or a deadlock-avoidance scheme. Deadlock Prevention provides a set of methods for ensuring that at least one of the necessary conditions cannot hold. Deadlock Avoidance requires that the operating system be given in advance additional information concerning which resources a process will request and use during its lifetime. With this additional knowledge, it can decide for each request whether or not the process should wait.

Deadlock Prevention

Deadlock Prevention As we noted, for a deadlock to occur, each of the four necessary conditions must hold. By ensuring that at least one of these conditions cannot hold, we can prevent the occurrence of a deadlock. Mutual Exclusion The mutual-exclusion condition must hold for non-sharable resources. Hold and Wait we must guarantee that, whenever a process requests a resource, it does not hold any other resources. Require a process to request and be allocated all its resources before it begins execution, or allow a process to request resources only when the process has none. Result: Low resource utilization; Prepared By: Kaushik starvation Vaghani possible.

Deadlock Prevention To illustrate the difference between these two protocols, we consider a process that copies data from a DVD drive to a file on disk, sorts the file, and then prints the results to a printer. If all resources must be requested at the beginning of the process, then the process must initially request the DVD drive, disk file, and printer. It will hold the printer for its entire execution, even though it needs the printer only at the end. The second method allows the process to request initially only the DVD drive and disk file. It copies from the DVD drive to the disk and then releases both the DVD drive and the disk file. The process must then again request the disk file and the printer. After copying the disk file to the printer, it releases these two resources and terminates.

(. Cont ) Deadlock Prevention No Preemption If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released Preempted resources are added to the list of resources for which the process is waiting A process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting Circular Wait impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. For example: F(tape drive) = 1 F(disk drive) = 5 F(printer) = 12 We can now consider the following protocol to prevent deadlocks: Each process can request resources only in an increasing order of enumeration. That is, a process can initially request any number of instances of a resource type -say, Ri. After that, the process can request instances of resource Prepared type By: Kaushik Rj if and Vaghani only if F(Rj) > F(Ri).

Deadlock Avoidance

Deadlock Avoidance It Requires that the system has some additional a priori information available about how resources are to be requested. Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need. Given this a priori information, it is possible to construct an algorithm that ensures that the system will never enter a deadlocked state. The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition. A resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes

Safe State When a process requests an available resource, the system must decide if immediate allocation leaves the system in a safe state. A system is in a safe state only if there exists a safe sequence. A sequence of processes <P 1, P 2,, P n > is a safe sequence for the current allocation state if, for each P i, the resource requests that P i can still make, can be satisfied by currently available resources plus resources held by all P j, with j < i. That is: If the P i resource needs are not immediately available, then P i can wait until all P j have finished When P j is finished, P i can obtain needed resources, execute, return allocated resources, and terminate When P i terminates, P i +1 can obtain its needed resources, and so on

( continued ) Safe State If a system is in safe state no deadlocks If a system is in unsafe state possibility of deadlock Avoidance ensure that a system will never enter an unsafe state

Safe, Unsafe, Deadlock State

Avoidance algorithms For a single instance of a resource type, use a resource-allocation graph For multiple instances of a resource type, use the banker s algorithm

Resource-Allocation Graph Scheme For this, we use a variant of the resource allocation graph and Introduce a new kind of edge called a claim edge in addition to the request and assignment edges. Claim edge P i - - - - -> R j indicates that process P j may request resource R j ; which is represented by a dashed line A claim edge converts to a request edge when a process requests a resource A request edge converts to an assignment edge when the resource is allocated to the process When a resource is released by a process, an assignment edge reconverts to a claim edge

Resource-Allocation Graph Scheme Resources must be claimed a priori in the system. That is, before process Pi starts executing, all its claim edges must already appear in the resource-allocation graph. Now Suppose that process P i requests a resource Rj. The request can be granted only if converting the request edge to an assignment edge does not result in the formation of a cycle in the resource allocation graph. If no cycle exists, then the allocation of the resource will leave the system in a safe state. If cycle exists, then the allocation will put the system in an unsafe state.

Resource-Allocation Graph with Claim Edges Assignment edge Request edge Claim edge Claim edge

Unsafe State In Resource-Allocation Graph Assignment edge Request edge Claim edge Assignment edge

Banker s Algorithm Used when there exists multiple instances of a resource type. Banker s algorithm is less efficient than the resourceallocation graph scheme. The name was chosen because the algorithm could be used in a banking system to ensure that the bank never allocated its available cash in such a way that it could no longer satisfy the needs of all its customers. Each process must declare the maximum number of instances of each resource type that it may need when it enters the system.

Banker s Algorithm When a user requests a set of resources, the system must determine whether the allocation of these resources will leave the system in a safe state. If it will, the resources are allocated; otherwise, the process must wait until some other process releases enough resources. When a process gets all its resources, it must return them in a finite amount of time

Data Structures for the Banker s Algorithm Let n = number of processes in the system, m = number of resources types. Available: A vector of length m indicates the number of available resources of each type. If available [j] = k, there are k instances of resource type R j available. Max: (n x m matrix). If Max [i][j] = k, then process P i may request at most k instances of resource type R j. Allocation: (n x m matrix). If Allocation[i][j] = k then P i is currently allocated k instances of R j. Need: (n x m matrix). If Need[i][j] = k, then P i may need k more instances of R j to complete its task. Need [i,j] = Max[i][j] Allocation [i][j]

Data Structures for the Banker s Algorithm We can treat each row in the matrices Allocation and Need as vectors and refer to them as Allocation i and Need i. The vector Allocation i specifies the resources currently allocated to process P i ; the vector Need i specifies the additional resources that process P i may still request to complete its task.

Banker s Algorithm (Cont.) Safety Algorithm 1. Let Work and Finish be vectors of length m and n, respectively. Initially, Work = Available Finish[i] = false for i = 0, 1,..., n - 1. This means, initially, no process has finished and the number of available resources is represented by the Available array. 2. Find an index i such that both Finish[i] == false Need i <= Work If there is no such i present, then proceed to step 4. It means, we need to find an unfinished process whose need can be satisfied by the available resources. If no such process exists, just go to step 4.

Banker s Algorithm (Cont.) 3. Perform the following: Go to step 2. Safety Algorithm Work = Work + Allocation i Finish[i] = true; When an unfinished process is found, then the resources are allocated and the process is marked finished. And then, the loop is repeated to check the same for all other processes. 4. If Finish[i] == true for all i, then the system is in a safe state.

Banker s Algorithm (Cont.) Resource-Request Algorithm This is the algorithm for determining whether requests can be safely granted. Let Request i be the request vector for process P i. If Request i [j] == k, then process P i wants k instances of resource type R j. When a request for resources is made by process P i, the following actions are taken: 1. If Request i <= Need i, go to step 2. Otherwise, raise an error condition, since the process has exceeded its maximum claim. 2. If Request i <= Available, go to step 3. Otherwise, P i must wait, since the resources are not available. 3. Have the system pretend to have allocated the requested resources to process P i by modifying the state as follows:

Banker s Algorithm (Cont.) Resource-Request Algorithm Available = Available - Request i ; Allocation i = Allocation i + Request i ; Need i = Need i - Request i ; If the resulting resource-allocation state is safe, the transaction is completed, and process P i is allocated its resources. However, if the new state is unsafe, then P i must wait for Request i, and the old resource-allocation state is restored.

Deadlock Detection

Deadlock Detection For deadlock detection, the system must provide An algorithm that examines the state of the system to detect whether a deadlock has occurred And an algorithm to recover from the deadlock A detection-and-recovery scheme requires various kinds of overhead Run-time costs of maintaining necessary information and executing the detection algorithm Potential losses inherent in recovering from a deadlock

Single Instance of Each Resource Type Requires the creation and maintenance of a wait-for graph Consists of a variant of the resource-allocation graph The graph is obtained by removing the resource nodes from a resource-allocation graph and collapsing the appropriate edges Consequently; all 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

Resource-Allocation Graph and Wait-for Graph Resource-Allocation Graph Corresponding wait-for graph

Multiple Instances of a Resource Type Required 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.

Multiple Instances of a Resource Type we again treat the rows in the matrices Allocation and Request as vectors; we refer to them as Allocation i and Request i. 1. Let Work and Finish be vectors of length m and n, respectively. Initialize Work= Available. For i= 0, 1,..., n-1, 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.

Multiple Instances of a Resource Type 3. Work= Work+ Allocation i Finish[i] = true Go to step 2. 4. If Finish[i] == false for some i, 0 <= i < n, then the system is in a deadlocked state. Moreover, if Finish[i] == false, then process P i is deadlocked.

Detection-Algorithm Usage When, and how often, to invoke the detection algorithm depends on: How often is a deadlock likely to occur? How many processes will be affected by deadlock when it happens? If the detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we would not be able to tell which one of the many deadlocked processes caused the deadlock If the detection algorithm is invoked for every resource request, such an action will incur a considerable overhead in computation time A less expensive alternative is to invoke the algorithm at defined interval- for example, once per hour or whenever CPU utilization drops below 40%.

Recovery From Deadlock

Recovery from Deadlock Two Approaches Process Termination Resource Preemption

Recovery from Deadlock: Process Termination Abort all deadlocked processes This approach will break the deadlock, but at great expense; deadlocked process running from long time will have to be recomputed later. Abort one process at a time until the deadlock cycle is eliminated This approach incurs considerable overhead, since, after each process is aborted, a deadlock-detection algorithm must be re-invoked to determine whether any processes are still deadlocked. Many factors may affect which process is chosen for termination What is the priority of the process? How long has the process run so far and how much longer will the process need to run before completing its task? How many and what type of resources has the process used? How many more resources does the process need in order to finish its task? How many processes will need to be terminated? Is the process interactive or batch?

Recovery from Deadlock: Resource Preemption With this approach, we successively preempt some resources from processes and give these resources to other processes until the deadlock cycle is broken When preemption is required to deal with deadlocks, then three issues need to be addressed: Selecting a victim Which resources and which processes are to be preempted? Rollback If we preempt a resource from a process, what should be done with that process? Clearly it cannot continue with its normal execution; It is missing some needed resources. We must roll back the process to some safe state and restart it from that state. Starvation How do we ensure that starvation will not occur? That is, how can we guarantee that resources will not always be preempted from the same process?

Summary Four necessary conditions must hold in the system for a deadlock to occur Mutual exclusion Hold and wait No preemption Circular wait Four principal methods for dealing with deadlocks Use some protocol to (1) prevent or (2) avoid deadlocks, ensuring that the system will never enter a deadlock state Allow the system to enter a deadlock state, (3) detect it, and then recover Recover by process termination or resource preemption (4) Do nothing; ignore the problem altogether and pretend that deadlocks ( Unix never occur in the system (used by Windows and To prevent deadlocks, we can ensure that at least one of the four necessary conditions never holds