Deadlocks. Dr. Yingwu Zhu

Similar documents
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

The Deadlock Problem

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

System Model. Types of resources Reusable Resources Consumable Resources

Chapter 8: Deadlocks. The Deadlock Problem

Chapter 7: Deadlocks

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

CMSC 412. Announcements

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. Operating System Concepts 8 th Edition,! Silberschatz, Galvin and Gagne 2009!

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Chapter 7: Deadlocks

Deadlocks. Deadlock Overview

Module 7: Deadlocks. The Deadlock Problem

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

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

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

Chapter 8: Deadlocks. Operating System Concepts with Java

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

Module 6: Deadlocks. Reading: Chapter 7

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

The Deadlock Problem (1)

CS307: Operating Systems

CSE Opera+ng System Principles

Principles of Operating Systems

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

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 7: DEADLOCKS. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

Deadlock Risk Management

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

CS307 Operating Systems Deadlocks

Deadlocks. Prepared By: Kaushik Vaghani

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

Lecture 7 Deadlocks (chapter 7)

The Deadlock Problem

Bridge Crossing Example

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

The Deadlock Problem

Principles of Operating Systems

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

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! CPU cycles, memory space, I/O devices! " Silberschatz, Galvin and Gagne 2013!

Operating Systems 2015 Spring by Euiseong Seo DEAD LOCK

Chapter 8: Deadlocks. The Deadlock Problem

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

Deadlocks. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

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

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

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

CS420: Operating Systems. Deadlocks & Deadlock Prevention

Chapter 8: Deadlocks. The Deadlock Problem

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

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

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

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

Potential Deadlock Example

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

Operating Systems ECE344. Ding Yuan

CHAPTER 7 - DEADLOCKS

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

Chapter 7: Deadlocks

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

Chapter - 4. Deadlocks Important Questions

Chapter 7: Deadlocks CS370 Operating Systems

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

Deadlock Risk Management

CS370 Operating Systems

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

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

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

Deadlock. Operating Systems. Autumn CS4023

OPERATING SYSTEMS. Deadlocks

CS 318 Principles of Operating Systems

Deadlocks. The Deadlock Problem. Bridge Crossing Example. Topics

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

Deadlock. Chapter Objectives

The deadlock problem

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

Fall 2015 COMP Operating Systems. Lab 06

CS370 Operating Systems

CSC 539: Operating Systems Structure and Design. Spring 2005

CS370 Operating Systems

Introduction to Deadlocks

CS370 Operating Systems

Deadlocks. System Model

UNIT-3 DEADLOCKS DEADLOCKS

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

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

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

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

More on Synchronization and Deadlock

CS153: Deadlock. Chengyu Song. Slides modified from Harsha Madhyvasta, Nael Abu-Ghazaleh, and Zhiyun Qian

Operating Systems. Deadlock. Lecture 7 Michael O Boyle

Transcription:

Deadlocks Dr. Yingwu Zhu

Deadlocks Synchronization is a live gun we can easily shoot ourselves in the foot Incorrect use of synchronization can block all processes You have likely been intuitively avoiding this situation already More generally, processes that allocate multiple resources generate dependencies on those resources Locks, semaphores, monitors, etc., just represent the resources that they protect If one process tries to allocate a resource that a second process holds, and vice-versa, they can never make progress We call this situation deadlock, and we ll look at: Definition and conditions necessary for deadlock Representation of deadlock conditions Approaches to dealing with deadlock

Deadlock Definition 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 tape drives. P 1 and P 2 each hold one tape drive and each needs another one. Example semaphores A and B, initialized to 1 P 0 P 1 wait (A); wait(b) wait (B); wait(a)

Deadlock Characterization Deadlock can arise if four conditions hold simultaneously. 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, after that process has completed its task. Circular wait: there exists a set {P 0, P 1,, P 0 } 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. More precisely describe deadlocks 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 i R j assignment edge directed edge R j P i If the graph has no cycles, deadlock cannot exist If the graph has a cycle, deadlock may exist

Resource-Allocation Graph (Cont.) 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

Example of a Resource Allocation Graph

Example of a Resource Allocation Graph If the graph contains no cycles, then no process is deadlocked

Resource Allocation Graph

Resource Allocation Graph If the graph contains a cycle, then a deadlock MAY exist! [1] If each resource type has exactly one instance, then a cycle implies that a deadlock has occurred. [2] If each resource type has several instances, then a cycle does not necessarily imply a deadlock. The cycle is just a necessary but not a sufficient condition 3 processes are deadlocked!

Resource Allocation Graph With A Cycle But No Deadlock

Resource Allocation Graph With A Cycle But No Deadlock

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

Handling Deadlocks Prevention make it impossible for deadlock to happen Avoidance control allocation of resources Detection and Recovery look for a cycle in dependencies Ignore it Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX. It is up to the app. Developer to write programs that handle deadlocks

Deadlock Prevention Break one of the four conditions to prevent deadlock Mutual exclusion Make resources sharable (not generally practical) Hold and wait Process cannot hold one resource when requesting another Process requests, releases all needed resources at once Low resource utilization; possible starvation Preemption OS can preempt resource (costly) Not practical for many resources (printers, tape drives)! Circular wait Impose an ordering (numbering) on the resources and request them in order (popular implementation technique)

Deadlock Avoidance Avoidance Provide information in advance about what resources will be needed by processes to guarantee that deadlock will not happen System only grants resource requests if it knows that the process can obtain all resources it needs in future requests Avoids circularities (wait dependencies) Tough Hard to determine all resources needed in advance Good theoretical problem, not as practical to use

Deadlock Avoidance Requires that the system has some additional a priori information available. Requires that each process declare the maximum number of resources of each type that it may need. The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition. 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, system must decide if immediate allocation leaves the system in a safe state. System is in safe state if there exists a safe sequence of all processes. Sequence <P 1, P 2,, P n > is safe if 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. If 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.

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. Basic Facts

Resource-Allocation Graph Algorithm Only works for resource types each with one instance! Claim edge P i R j indicated 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. When a resource is released by a process, assignment edge reconverts to a claim edge. Resources must be claimed a priori in the system. Before a process executes, all its claim edges must already appear in the graph!

Resource-Allocation Graph For Deadlock Avoidance Can the request P2-->R2 be granted?

Unsafe State In Resource-Allocation Graph NO. converting to assignment edge forms a cycle, unsafe state!

Banker s Algorithm The resource-allocation-graph algorithm is not applicable to a resource allocation with multiple instances of each resource type Multiple instances. Each process must a priori claim maximum use. When a process requests a resource it may have to wait. 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, and m = number of resources types. Available: Vector of length m. 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]

Banker s Algorithm Goal: find a safe sequence of processes! 1. Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish [i] = false for i = 1,2,, n. 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. Require O(m x n 2 ) operations!!!

Example of Banker s Algorithm 5 processes P 0 through P 4 ; 3 resource types A (10 instances), B (5 instances), and C (7 instances). Snapshot at time T 0 : Allocation Max Available A B C A B C A B C P 0 0 1 0 7 5 3 3 3 2 P 1 2 0 0 3 2 2 P 2 3 0 2 9 0 2 P 3 2 1 1 2 2 2 The content of the matrix. Need is defined to be Max Allocation. Need A B C P 0 7 4 3 P 1 1 2 2 P 2 6 0 0 P 3 0 1 1 P 4 4 3 1 The system is in a safe state since the sequence < P 1, P 3, P 4, P 2, P 0 > satisfies safety criteria. P 4 0 0 2 4 3 3

Resource-Request Algorithm for Process P i -- To determine if a request should be granted or not? Request = request vector for process P i. If Request i [j] = k then process P i wants k instances of resource type R j. 1. If Request i Need i go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim. 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 i ; Allocation i = Allocation i + Request i ; Need i = Need i Request i ; If safe the resources are allocated to Pi. If unsafe Pi must wait, and the old resource-allocation state is restored

Example P 1 Request (1,0,2) Allocation Need Available A B C A B C A B C P 0 0 1 0 7 4 3 3 3 2 P 1 2 0 0 0 2 0 P 2 3 0 1 6 0 0 P 3 2 1 1 0 1 1 P 4 0 0 2 4 3 1 Can this request be satisfied?

Example P 1 Request (1,0,2) (Cont.) Check that Request Available (that is, (1,0,2) (3,3,2) true. Allocation Need Available A B C A B C A B C P 0 0 1 0 7 4 3 2 3 0 P 1 3 0 2 0 2 0 P 2 3 0 1 6 0 0 P 3 2 1 1 0 1 1 P 4 0 0 2 4 3 1 Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2> satisfies safety requirement. Question: Can request for (3,3,0) by P4 be granted? Question: Can request for (0,2,0) by P0 be granted?

Detection and Recovery Detection and recovery If we don t have deadlock prevention or avoidance, then deadlock may occur In this case, we need to detect deadlock and recover from it To do this, we need two algorithms One to determine whether a deadlock has occurred Another to recover from the deadlock Possible, but expensive (time consuming) Implemented in VMS Run detection algorithm when resource request times out

Deadlock Detection Detection Traverse the resource graph looking for cycles If a cycle is found, preempt resource (force a process to release) Expensive Many processes and resources to traverse Only invoke detection algorithm depending on How often or likely deadlock is How many processes are likely to be affected when it occurs

Dead Recovery Once a deadlock is detected, we have two options 1. Abort processes Abort all deadlocked processes Processes need start over again Abort one process at a time until cycle is eliminated System needs to rerun detection after each abort 2. Preempt resources (force their release) Need to select process and resource to preempt Need to rollback process to previous state Need to prevent starvation

Summary Deadlock occurs when processes are waiting on each other and cannot make progress Cycles in Resource Allocation Graph (RAG) Deadlock requires four conditions Mutual exclusion, hold and wait, no resource preemption, circular wait Four approaches to dealing with deadlock: Ignore it Living life on the edge Prevention Make one of the four conditions impossible Avoidance Banker s Algorithm (control allocation) Detection and Recovery Look for a cycle, preempt or abort