CSC 539: Operating Systems Structure and Design. Spring 2005

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

Chapter 7: Deadlocks

Bridge Crossing Example

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

The Deadlock Problem

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

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

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. Operating System Concepts 8 th Edition,

Chapter 7: Deadlocks

Module 7: Deadlocks. The Deadlock Problem

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

Chapter 7: Deadlocks

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 8: Deadlocks. The Deadlock Problem

Chapter 7: Deadlocks

CMSC 412. Announcements

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

Deadlocks. Deadlock Overview

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

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

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

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

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

Chapter 8: Deadlocks. Operating System Concepts with Java

Principles of Operating Systems

Principles of Operating Systems

System Model. Types of resources Reusable Resources Consumable Resources

Chapter 8: Deadlocks. The Deadlock Problem

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

CS307: Operating Systems

Deadlocks. Prepared By: Kaushik Vaghani

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

The Deadlock Problem

CS420: Operating Systems. Deadlocks & Deadlock Prevention

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

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.

The Deadlock Problem (1)

Deadlocks. System Model

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

CSE Opera+ng System Principles

CS307 Operating Systems Deadlocks

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

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

Deadlocks. Dr. Yingwu Zhu

System Model. Deadlocks. Deadlocks. For example: Semaphores. Four Conditions for Deadlock. Resource Allocation Graph

Lecture 7 Deadlocks (chapter 7)

Deadlocks. The Deadlock Problem. Bridge Crossing Example. Topics

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

The Deadlock Problem

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

Deadlock Risk Management

Module 6: Deadlocks. Reading: Chapter 7

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

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

Part II Process M anagement Management Chapter 7: Deadlocks Fall 2010

Contents. Chapter 8 Deadlocks

Chapter 6 Concurrency: Deadlock and Starvation

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. Operating System Concepts 9 th Edition

UNIT-3 DEADLOCKS DEADLOCKS

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

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

Deadlocks: Part I Prevention and Avoidance Yi Shi Fall 2017 Xi an Jiaotong University

Deadlock. Operating Systems. Autumn CS4023

CHAPTER 7 - DEADLOCKS

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Chapter 8: Deadlocks. The Deadlock Problem

Operating Systems 2015 Spring by Euiseong Seo DEAD LOCK

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

Deadlocks Detection and Avoidance. Prof. Sirer CS 4410 Cornell University

Deadlocks. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

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

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

Deadlock. Chapter Objectives

Last Class: Synchronization Problems!

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

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

OPERATING SYSTEMS. Deadlocks

Chapter 7: Deadlocks

Last Class: Synchronization Problems. Need to hold multiple resources to perform task. CS377: Operating Systems. Real-world Examples

TDDB68 + TDDD82. Lecture: Deadlocks

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

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Chapter - 4. Deadlocks Important Questions

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

Operating Systems. Lecture 07: Resource allocation and Deadlock Management. Elvis C. Foster

Chapter 7: Deadlocks CS370 Operating Systems

Deadlocks. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Last Class: Deadlocks. Today

Process-1 requests the tape unit, waits. In this chapter, we shall analyze deadlocks with the following assumptions:

Deadlock. Disclaimer: some slides are adopted from Dr. Kulkarni s and book authors slides with permission 1

More on Synchronization and Deadlock

The deadlock problem

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

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Transcription:

CSC 539: Operating Systems Structure and Design Spring 2005 Process deadlock deadlock prevention deadlock avoidance deadlock detection recovery from deadlock 1 Process deadlock in general, can partition system resources into equivalence types or classes e.g, CPUs, RAM, files, printers, tape drives, when a process requests a resource from a particular resource class, any available resource in the class is allocated to the process generally, resources go through the sequence: 1. request 2. use 3. release deadlock occurs when a set of processes is waiting for resources that are held by other processes in the set 2

Deadlock examples real-world example: single-lane bridge each section of a bridge can be viewed as a resource if a deadlock occurs, it can be resolved if one car backs up (preempt & rollback) several cars may have to be backed up if a deadlock occurs starvation is possible system example: 2 tape drives P 1 and P 2 each hold one tape drive and each needs another one system example: semaphores A and B, initialized to 1 P 0 : wait(a); P 1 : wait(b); wait(b); wait(a); 3 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 n } of waiting processes such that: P 0 is waiting for a resource held by P 1 P 1 is waiting for a resource held by P 2 P n is waiting for a resource held by P 0 consider single-lane bridge example: 4 necessary conditions? 4

System resource allocation graph we can use a directed graph to represent the state of the system as it allocates resources and deals with requests for resources nodes of graph represent processes or resources process nodes will have circles around them resource nodes will have rectangles around them edges of graph represent allocations or requests process to resource edge is a request resource to process edge is an allocation in this example: P 1 has R 2, has requested R 1 P 2 has R 1 & R 2, has requested R 3 P 3 has R 3 5 cycle = deadlock? 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, deadlock is possible no deadlock deadlock no deadlock 6

Methods for dealing with deadlock Prevention or Avoidance: ensure system never enters a deadlocked state deadlock prevention scheme ensure that at least one of the necessary conditions cannot hold deadlock avoidance scheme require the OS to know the resource usage requirements of all processes for each request, the OS decides if it could lead to a deadlock before granting Detection & Recovery: allow deadlocks to occur system implements an algorithm for deadlock detection, if so then recover Ignorance: assume/pretend deadlocks never occur in the system used by most operating systems, including UNIX & Windows 7 Deadlock prevention to prevent deadlock, must eliminate one of the four necessary conditions Mutual Exclusion: can't eliminate since some resources are not shareable Hold and Wait: options exist, but wasteful and may lead to starvation 1. grant all resources when process starts, or 2. allocate and deallocate resources in complete groups No Preemption: options exist, but taking away resources can be tricky Circular Wait: most likely candidate impose an ordering on resources [R 0, R 1,, R n ], and require that each process requests resources in increasing order e.g., P 0 wants R 0, R 1, and R 2 P 1 wants R 1, R 2, and R 3 informal proof that resource ordering ensures no circular wait? 8

Deadlock avoidance if have some knowledge of future resource demands (e.g., max number of resources needed per process), can avoid deadlock situations safe state: a state is safe if the system can allocate resources to each process (up to its maximum need) in some sequence and still avoid a deadlock safe sequence: a sequence of processes {P 0, P 1,, P n } is safe for the current allocation state if, for each P i, the resources that P i can still request are either free or else held by {P 0, P 1,, P i-1 } a safe sequence implies that each process need only wait for lower numbered processes to finish before it can finish Note: unsafe deadlock a safe state is guaranteed no deadlock an unsafe state may lead to deadlock 9 Example: safe or unsafe? suppose there are 12 instances of a given resource (e.g., tape drives) in the current state, resources are allocated to 3 processes Process P: max need = 10, currently allocated = 5 Process Q: max need = 4, currently allocated = 2 Process R: max need = 9, currently allocated = 2 is this a safe state? if so, identify a safe sequence. 10

Resource allocation graph algorithm if there is only one instance of each resource class, then unsafe states can be avoided by considering a generalized resource allocation graph allocation edge R i P j if R i is currently held by P j (as before) request edge P i R j if P i has requested R j (as before) claim edge P i R j if P i may eventually request R j unsafe avoidance: do not allocate resource if it creates a cycle allocate R 2 to P 1 : OK allocate R 2 to P 2 : NOT ALLOWED leads to cycle in graph (unsafe state) 11 Banker's algorithm the banker's algorithm can avoid deadlocks even with multiple instances looks at each request for resources and tests if the request moves the system into an unsafe state if the system is still safe, then the request is granted if the system would become unsafe, then the request is denied utilizes the following data structures: Available[m] number of resources of R m that are unallocated Max[n][m] max demand of P n for R m Allocation[n][m] number of R m that are allocated to P n Need[n][m] number of R m that may be needed by P n Note: Need[n, m] = Max[n, m] Allocation[n, m] as in C++, will treat a matrix as a vector of vectors (Max[i] is max resources demand for P i ) for vectors X & Y, X Y if and only if X[i] Y[i] for all i 12

Example: banker's algorithm suppose have: 3 types of resources (6 of type A, 2 of type B, 2 of type C) 3 processes (P, Q, and R) P 2 1 0 0 1 0 1 1 0 2 0 0 Q 3 1 1 2 0 0 1 1 1 R 5 1 2 3 0 2 2 1 0 is the system in a safe state? 13 Banker's: safety algorithm to find out whether a system is in a safe state: 1. define vector: Finish[k] = false (for all k n) 2. find an i such that (Finish[i] == false && Need[i] Available) if no such i, then go to STEP 4 3. update: Available = Available + 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 otherwise, the system is in an unsafe state P 2 1 0 0 1 0 2 1 1 2 0 0 Q 3 1 1 2 0 0 1 1 1 R 5 1 2 2 0 1 3 1 1 is this state safe? 14

Banker's: resource request algorithm let Request[i] be the request vector from P i 1. if Request[i] > Need[i], then ERROR (asked for too many resources) & EXIT 2. if Request[i] > Available[i], then deny request and P i must wait (the requested resources are not currently available) 3. otherwise, pretend to allocate the resources Available = Available Request[i] Allocation[i] = Allocation[i] + Request[i] Need[i] = Need[i] Request[i] 4. call the Safety algorithm, if safe then allocation is OK otherwise, the request is denied and the pretended allocation is undone P 2 1 0 0 1 0 2 1 1 2 0 0 Q 3 1 1 2 0 0 1 1 1 R 5 1 2 2 0 1 3 1 1 what if R asks for [1 0 1]? 15 Example: banker's algorithm suppose 5 processes: P 0 through P 4 3 resource types: A (10), B (5), and C (7) snapshot at time T 0 : P 0 7 5 3 0 1 0 3 3 2 7 4 3 P 1 3 2 2 2 0 0 1 2 2 P 2 9 0 2 3 0 2 6 0 0 P 3 2 2 2 2 1 1 0 1 1 P 4 4 3 3 0 0 2 4 3 1 the system is in a safe state since the sequence P 1, P 3, P 4, P 2, P 0 satisfies the safety criteria 16

Example: banker's algorithm (cont.) P 0 7 5 3 0 1 0 3 3 2 7 4 3 P 1 3 2 2 2 0 0 1 2 2 P 2 9 0 2 3 0 2 6 0 0 P 3 2 2 2 2 1 1 0 1 1 P 4 4 3 3 0 0 2 4 3 1 P 1 requests [1 0 2] 1. Request[i] Need[i]: [1 0 2] [1 2 2], so no ERROR 2. Request[i] Available[i]: [1 0 2] [3 3 2], so resources are available 3. pretend to allocate the resources P 0 7 5 3 0 1 0 2 1 0 7 4 3 P 1 3 2 2 3 2 2 0 0 0 P 2 9 0 2 3 0 2 6 0 0 P 3 2 2 2 2 1 1 0 1 1 P 4 4 3 3 0 0 2 4 3 1 the sequence P1, P3, P4, P0, P2 satisfies the safety requirement 17 Example: banker's algorithm (cont.) P 0 7 5 3 0 1 0 3 3 2 7 4 3 P 1 3 2 2 2 0 0 1 2 2 P 2 9 0 2 3 0 2 6 0 0 P 3 2 2 2 2 1 1 0 1 1 P 4 4 3 3 0 0 2 4 3 1 P 4 requests [3 3 0]? P 0 requests [0 2 0]? 18

Deadlock detection if there is only a single instance of each resource, can construct a wait-for graph out of the resource allocation graph remove all resource nodes and collapse the edges wait-for graph a deadlock has occurred if and only if there is a cycle in the wait-for graph to detect deadlock, need to periodically check for cycles can be accomplished in O(n 2 ) operations, where n is number of processes 19 Deadlock detection algorithm if multiple instances of resources, need test similar to safety algorithm 1. define vector: Finish[k] = (Allocation[k] == 0) 2. find an i such that (Finish[i] == false && Request[i] Available) if no such i, then go to STEP 4 3. update: Available = Available + Allocation[i], Finish[i] = true go to STEP 2 4. if (Finish[i] == false) for any i, then P i is deadlocked otherwise, no deadlock Allocation Request Available A B C A B C A B C P 0 0 1 0 0 0 0 0 0 0 P 1 2 0 0 2 0 2 P 2 3 0 3 0 0 0 P 4 2 1 1 1 0 0 P 5 0 0 2 0 0 2 not deadlocked P0, P2, P3, P1, P4 results in Finish[i] == true for all i 20

Deadlock detection algorithm (cont.) 1. define vector: Finish[k] = (Allocation[k] == 0) 2. find an i such that (Finish[i] == false && Request[i] Available) if no such i, then go to STEP 4 3. update: Available = Available + Allocation[i], Finish[i] = true go to STEP 2 4. if (Finish[i] == false) for any i, then P i is deadlocked otherwise, no deadlock Allocation Request Available A B C A B C A B C P 0 0 1 0 0 0 0 0 0 0 P 1 2 0 0 2 0 2 P 2 3 0 3 0 0 1 P 4 2 1 1 1 0 0 P 5 0 0 2 0 0 2 deadlock! can reclaim P 0 's resources, but not enough to meet requests of other processes 21 When to detect deadlock? checking for deadlock is time consuming given n processes, m resource types, deadlock detection is O(m*n 2 ) testing at every resource request would greatly slow down all requests if deadlocks are rare or only affect a small number of processes, then may resort to testing periodically (e.g., once a day, when CPU use < 40%) note that if testing is sporadic, there may be many cycles in the resource graph will not be able to tell which of the deadlocked processes caused the deadlock 22

Deadlock recovery to recover from deadlock, must eliminate one of the necessary conditions this involves preempting a resource or aborting a deadlocked process preempting a resource How do you select a victim? When preempt a resource, how do you roll back the process? How do you prevent starvation? aborting a process Do you abort all deadlocked processes or one at a time? Note: it is possible to combine the three basic approaches prevention + avoidance + detection allowing the use of the optimal approach for each class of resources 23