Deadlocks. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Similar documents
Deadlocks. Prepared By: Kaushik Vaghani

Chapter 7: Deadlocks

Deadlocks. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

The Deadlock Problem

Chapter 8: Deadlocks. The Deadlock Problem

Chapter 7: Deadlocks

Chapter 8: Deadlocks

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

CMSC 412. Announcements

Chapter 8: Deadlocks. The Deadlock Problem

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

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

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

Chapter 3. Deadlocks

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

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 7: Deadlocks

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

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.

CS307 Operating Systems Deadlocks

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

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

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Chapter 8: Deadlocks. Operating System Concepts with Java

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

Bridge Crossing Example

Module 7: Deadlocks. The Deadlock Problem

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

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

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

Resources. Lecture 4. Deadlocks. Resources (2) Resources (1) Four Conditions for Deadlock. Introduction to Deadlocks

The Deadlock Problem

Contents. Chapter 8 Deadlocks

System Model. Types of resources Reusable Resources Consumable Resources

Chapter 3. Deadlocks

Deadlocks. Deadlock Overview

CSE Opera+ng System Principles

Lecture 7 Deadlocks (chapter 7)

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

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

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 9 th Edition

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

Principles of Operating Systems

Deadlock Risk Management

The Deadlock Problem (1)

CS307: Operating Systems

Principles of Operating Systems

OPERATING SYSTEMS. Deadlocks

UNIT-3 DEADLOCKS DEADLOCKS

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

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

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Deadlock. Operating Systems. Autumn CS4023

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

CS420: Operating Systems. Deadlocks & Deadlock Prevention

The Deadlock Problem

Introduction to OS. Deadlock. MOS Ch. 6. Mahmoud El-Gayyar. Mahmoud El-Gayyar / Introduction to OS 1

CHAPTER 7 - DEADLOCKS

Deadlock and Starvation

Deadlocks. Today. Next Time. Resources & deadlocks Dealing with deadlocks Other issues. I/O and file systems

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

Deadlock. Chapter Objectives

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

Deadlocks. Today. Next Time. ! Resources & deadlocks! Dealing with deadlocks! Other issues. ! Memory management

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

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

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

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Deadlocks. Thomas Plagemann. With slides from C. Griwodz, K. Li, A. Tanenbaum and M. van Steen

Operating Systems 2015 Spring by Euiseong Seo DEAD LOCK

Operating Systems. Lecture3.2 - Deadlock Management. Golestan University. Hossein Momeni

Deadlocks. The Deadlock Problem. Bridge Crossing Example. Topics

DEADLOCKS M O D E R N O P E R A T I N G S Y S T E M S C H A P T E R 6 S P R I N G

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

Deadlocks. Dr. Yingwu Zhu

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Chapter 3: Deadlocks

Chapter 8: Deadlocks. The Deadlock Problem

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

CS450/550 Operating Systems

Chapter 6. Deadlocks

Deadlocks. Mehdi Kargahi School of ECE University of Tehran Spring 2008

MODERN OPERATING SYSTEMS. Third Edition ANDREW S. TANENBAUM. Chapter 6 Deadlocks

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

Module 6: Deadlocks. Reading: Chapter 7

Chapter 7: Deadlocks CS370 Operating Systems

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

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

Deadlocks Prof. James L. Frankel Harvard University. Version of 7:05 PM 7-Mar-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

Shared and Preemptable Resources. A few resources may be sharable. Anyone can write the system log.

Operating Systems ECE344. Ding Yuan

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

Chapter 7: Deadlocks

Introduction to Deadlocks

VI. Deadlocks. What is a Deadlock? Intended Schedule. Deadlock Problem (1) ???

VI. Deadlocks Operating Systems Prof. Dr. Marc H. Scholl DBIS U KN Summer Term

Transcription:

Deadlocks Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

Today s Topics What is the deadlock problem? Four conditions for deadlock Handling deadlock Prevention Avoidance Detection & Recovery Ignorance 2

Deadlock (1) Traffic deadlock 3

Deadlock (2) Example 1 System has 2 disk drives. P1 and P2 each hold one disk drive and each needs another one. Example 2 Semaphores A and B, initialized to 1. P1 P2 wait (A); wait (B); wait (B); wait (A); 4

Deadlock (3) Deadlock problem A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. Processes need access to resources in reasonable order. Processes sometimes require more than one resource. Order in which the resources are allocated is important. Deadlocks can occur across machines. Shared devices between a number of machines Printers, scanners, CD recorders, etc. 5

Deadlock (4) Resources Can only be used by one process at a time. Can be both hardware and software. e.g., tape drives, printers, system tables, database entries, memory locations Request Allocate Use Release Preemptable resources Can be taken away from a process with no ill effects. Nonpreemptable resources Will cause the process to fail if taken away. Generally deadlocks involve nonpreemtable resources. 6

Conditions for Deadlock Four necessary conditions 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 must exist a set {P0,P1,,Pn,P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource held by P2, etc. 7

Deadlock Modeling (1) Resource-allocation graph A directed graph Vetices Processes: P = {P1, P2,, Pn} Resources: R = {R1, R2,, Rm} Edges Request edge: Pi Rj Assignment edge: Rj Pi 8

Deadlock Modeling (2) Examples 9

Deadlock Modeling (3) Basic facts Having a cycle is a necessary condition for deadlock. Deadlock can arise if four conditions hold simultaneously. 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. 10

Handling Deadlocks Strategies for handling deadlocks Deadlock prevention Restrain how requests are made. Ensure that at least one necessary condition cannot hold. Deadlock avoidance Require additional information about how resources are to be requested. Decide to approve or disapprove requests on the fly. Deadlock detection and recovery Allow the system to enter a deadlock state and then recover. Just ignore the problem altogether! 11

Deadlock Prevention (1) Mutual exclusion Not required for sharable resources, but must hold for non-sharable resources Some devices (e.g., printer) can be spooled Only the printer daemon uses printer resource Not all devices can be spooled. Competition for disk space for spooling can itself lead to deadlock. Principles Avoid assigning resource when not absolutely necessary. Make as few processes as possible actually claim the resource. 12

Deadlock Prevention (2) Hold and wait Must guarantee that whenever a process requests a resource, it does not hold any other resources. (1) Require process to request and be allocated all its resources before it begins execution. (2) Allow process to request resources only when the process has none. Problems May not know required resources at start of run. Low resource utilization. Starvation is possible. 13

Deadlock Prevention (3) No preemption (1) If a process must wait for another resource, all resources currently being held are implicitly preempted. (2) If the requesting resources are allocated to some other process that is waiting for additional resources, preempt the desired resources from the waiting process. Can be applied to resources whose state can be easily saved and restored later. e.g., CPU registers, memory 14

Deadlock Prevention (4) Circular wait Impose a total ordering of all resource types, as a one-to-one function F. F: R N, where R = {R1, R2,, Rn} is the set of resource types and N is the set of natural numbers. e.g., F(tape drive)=1, F(disk)=5, F(printer)=8, etc. Each process requests resources in an increasing order of enumeration. Whenever a process requests an instance of Rj, it has released any resources Ri such that F(Ri) >= F(Rj) F should be defined according to the normal order of usage of the resources in a system. 15

Deadlock Avoidance (1) Avoiding deadlocks Can the system decide whether granting a resource is safe or not and only make the allocation when it is safe? YES! only if certain information is available in advance. e.g., the maximum number of resources of each type Dynamically examines the resource allocation state to ensure that a circular wait can never exist. 16

Deadlock Avoidance (2) Resource trajectories Process B requests a plotter 17

Deadlock Avoidance (3) Safe state A state is safe if the system can allocate resources to each process in some order and still avoid a deadlock. A sequence of processes <P1,P2,..Pn> is a safe sequence if, for each Pi, the resources that Pi can still request can be satisfied by the currently available resources + the resources held by all the Pj, with j < i. If the resources that Pi needs are not immediately available, then Pi waits until all Pj have finished. When Pi terminates, Pi+1 can obtain its needed resources, and so on. 18

Deadlock Avoidance (4) Examples Total: 10units P1 gets 2 units P1 completes P2 gets 5 units P2 completes P0 3 9 P0 3 9 P0 3 9 P0 3 9 P1 requests 2 units Has Max P0 3 9 P1 2 4 P2 2 7 Free: 3 P0 requests 1 unit P1 4 4 P2 2 7 Free: 1 P0 gets 1 unit P0 4 9 P1 2 4 P2 2 7 P1 0 - P2 2 7 Free: 5 P1 gets 2 units P0 4 9 P1 4 4 P2 2 7 P1 0 - P2 7 7 Free: 0 P1 completes P0 4 9 P1 0 - P2 2 7 P1 0 - P2 0 - Free: 7 Free: 2 Free: 0 Free: 4 19

Deadlock Avoidance (5) Basic facts 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. 20

Deadlock Avoidance (6) Resource allocation graph algorithm Algorithm Claim edge: Pi Rj Pi requests Rj in the future. When Pi requests resource Rj, the claim edge (Pi Rj) is converted to a request edge. When Rj is released by Pi, the assignment edge (Rj Pi) is reconverted to a claim edge (Pi Rj). The request can be granted only if converting the request edge to an assignment edge does not result in a cycle. 21

Deadlock Avoidance (7) Resource allocation graph algorithm Notes Resources must be claimed a priori in the system. Applicable when each resource type has only one instance. Detecting a cycle in the graph requires O(n 2 ) operations, where n is the number of processes. 22

Deadlock Avoidance (8) Banker s algorithm Banker s algorithm Dijkstra, 1965. For resources having 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. 23

Deadlock Avoidance (9) Banker s algorithm Details Check every time if granting a request can lead to an unsafe state. Safety checking algorithm Choose a process whose resource needs are all smaller than or equal to the available resources. If there is no such process, the state is unsafe. If there is, assume the process requests all the resources it needs and finishes. Mark that process as terminated and add all its resources to the available resources. Repeat until all processes are marked terminated. 24

Deadlock Avoidance (10) Banker s algorithm Example Total Resources Allocated Resources Max Resources Needed Resources A B C A B C A B C A B C 10 5 7 P0 0 1 0 P0 7 5 3 P0 7 4 3 Available Resources P1 2 0 P2 3 0 0 2 P1 3 2 P2 9 0 2 2 P1 1 2 P2 6 0 2 0 A B C P3 2 1 1 P3 2 2 2 P3 0 1 1 3 3 2 P4 0 0 2 P4 4 3 3 P4 4 3 1 Currently safe: <P1, P3, P4, P0, P2> is a safe sequence 25

Deadlock Avoidance (11) Banker s algorithm Notes Safety checking algorithm requires O(mn 2 ) operations: m is the number of resource types n is the number of processes Processes rarely know in advance what their maximum resource needs will be. The number of processes is not fixed, but dynamically varying as new users log in and out. Resources that were thought to be available can suddenly vanish. e.g., tape drives or disk drives In practice, few, if any, existing systems use the banker s algorithm for avoiding deadlocks. 26

Deadlock Detection (1) Single instance / resource type Maintain a wait-for graph A variant of the resource allocation graph. Pi Pj if Pi is waiting for Pj Periodically invoke an algorithm that searches for a cycle in the graph. Requires O(n 2 ) operations 27

Deadlock Detection (2) Multiple instances / resource type Very similar to the safety checking in Banker s algorithm Total Resources A B C 7 2 6 Available Resources A B C 0 0 0 Allocated Resources A B C P0 0 1 P1 2 0 P2 3 0 P3 2 1 P4 0 0 0 0 3 1 2 Requested Resources A B C P0 0 0 P1 2 0 P2 0 0 P3 1 0 P4 0 0 0 2 0 0 2 What if this becomes 1? Currently not in a deadlock: <P0, P2, P3, P1, P4> will work. 28

Deadlock Recovery (1) Process termination Reclaims all resources allocated to the terminated processes Abort all deadlocked processes Abort one process at a time until the deadlock cycle is eliminated Crudest but simplest way to break a deadlock Not easy since the system may become inconsistent. e.g., files, printing jobs, etc. 29

Deadlock Recovery (2) Resource preemption Selecting a victim Minimize cost Rollback Roll back the process to some safe state and restart Starvation Ensure that a process can be picked up as a victim only a finite number of times. Include the number of rollbacks in the cost factor. 30

Deadlock Ignorance The Ostrich algorithm Just put your head in the sand and pretend there is no problem at all. Reasonable if Deadlocks occur very rarely Cost of prevention is high UNIX and Windows take this approach. Manual deadlock detection and resolution! It is a trade-off between Convenience Correctness 31