CS 318 Principles of Operating Systems

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

Operating Systems ECE344. Ding Yuan

CSE 120 Principles of Operating Systems

CSE 120. Fall Lecture 8: Scheduling and Deadlock. Keith Marzullo

Deadlocks. Dr. Yingwu Zhu

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

CS 153 Design of Operating Systems Winter 2016

System Model. Types of resources Reusable Resources Consumable Resources

CS 31: Intro to Systems Deadlock. Kevin Webb Swarthmore College April 21, 2015

Lecture 9: Midterm Review

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.

CS 153 Design of Operating Systems Spring 18

Chapter 8: Deadlocks. The Deadlock Problem

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

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

CS307 Operating Systems Deadlocks

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

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

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

The Deadlock Problem (1)

Bridge Crossing Example

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

CS307: Operating Systems

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

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

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

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

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

Deadlock and Starvation

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

Principles of Operating Systems

Last Class: Monitors. Real-world Examples

Chapter 8: Deadlocks. The Deadlock Problem

! What is a deadlock? ! What causes a deadlock? ! How do you deal with (potential) deadlocks? Maria Hybinette, UGA

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

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!

CS420: Operating Systems. Deadlocks & Deadlock Prevention

The Deadlock Problem

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

Deadlocks: Detection & Avoidance

Readings and References. Deadlock. Deadlock. Simple Traffic Gridlock Example. Reading. CSE Computer Systems November 30, 2001.

Lecture 7 Deadlocks (chapter 7)

Deadlock and Starvation

Starvation and Deadlock

Deadlock. Operating Systems. Autumn CS4023

!! What is a deadlock? !! What causes a deadlock? !! How do you deal with (potential) deadlocks? Maria Hybinette, UGA

Module 7: Deadlocks. The Deadlock Problem

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

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

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

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

Deadlock Risk Management

Chapter 7: Deadlocks

Principles of Operating Systems

Chapter 7: Deadlocks

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

Chapter 8: Deadlocks. Operating System Concepts with Java

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Deadlock. Readings and References. Simple Traffic Gridlock Example. Deadlock. Reading. Other References. CSE 410, Spring 2006 Computer Systems

Deadlocks. Deadlock Overview

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

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

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

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

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

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Deadlocks. Prepared By: Kaushik Vaghani

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

DUH! Deadlocks. Concurrency Issues. The TENEX Case. If a process requests all systems buffers, operator console tries to print an error message

Quiz Answers. CS 537 Lecture 9 Deadlock. What can go wrong? Readers and Writers Monitor Example

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

Operating Systems 2015 Spring by Euiseong Seo DEAD LOCK

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

Concurrency Issues. Past lectures: What about coordinated access across multiple objects? Today s lecture:

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

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

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

Deadlocks. The Deadlock Problem. Bridge Crossing Example. Topics

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

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

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

CHAPTER 7 - DEADLOCKS

Process Synchronisation (contd.) Deadlock. Operating Systems. Spring CS5212

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

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

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

More on Synchronization and Deadlock

Chapter 7: Deadlocks

CSC 539: Operating Systems Structure and Design. Spring 2005

Deadlocks. Copyright : University of Illinois CS 241 Staff 1

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

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

ECE519 Advanced Operating Systems

Deadlock. Only one process can use the resource at a time but once it s done it can give it back for use by another process.

Transcription:

CS 318 Principles of Operating Systems Fall 2017 Lecture 8: Deadlock Ryan Huang

Administrivia Lab 1 deadline extended - Friday 09/29 11:59 pm - Saturday 09/30 11:59 pm [Hard] HW2 out - should try to solve it Discussion section - review lecture material plus some exercises 9/28/17 CS 318 Lecture 8 Deadlock 2

Deadlock 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 If one process tries to access 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 9/27/17 CS 318 Lecture 8 Deadlock 3

Deadlock Definition Deadlock is a problem that can arise: - When processes compete for access to limited resources - When processes are incorrectly synchronized Definition: - Deadlock exists among a set of processes if every process is waiting for an event that can be caused only by another process in the set. 9/27/17 CS 318 Lecture 8 Deadlock 4

Deadlock Example mutex_t m1, m2; void p1(void *ignored) { lock(m1); lock(m2); /* critical section */ unlock(m2); unlock(m1); } void p2(void *ignored) { lock(m2); lock(m1); /* critical section */ unlock(m1); unlock(m2); } 9/27/17 CS 318 Lecture 8 Deadlock 5

Deadlock Example Can you have deadlock w/o mutexes? Same problem with condition variables - Suppose resource 1 managed by c 1, resource 2 by c 2 - A has 1, waits on c 2, B has 2, waits on c 1 Or have combined mutex/condition variable deadlock: - lock (a); lock (b); while (!ready) wait (b, c); unlock (b); unlock (a); - lock (a); lock (b); ready = true; signal (c); unlock (b); unlock (a); One lesson: Dangerous to hold locks when crossing abstraction barriers! - i.e., lock (a) then call function that uses condition variable 9/27/17 CS 318 Lecture 8 Deadlock 6

Deadlocks w/o Computers Real issue is resources & how required E.g., bridge only allows traffic in one direction - 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 resources and rollback). - Several cars may have to be backed up if a deadlock occurs. - Starvation is possible. 9/27/17 CS 318 Lecture 8 Deadlock 7

Conditions for Deadlock 1. Mutual exclusion At least one resource must be held in a non-sharable mode 2. Hold and wait There must be one process holding one resource and waiting for another resource 3. No preemption Resources cannot be preempted (critical sections cannot be aborted externally) 4. Circular wait There must exist a set of processes [P 1, P 2, P 3,,P n ] such that P 1 is waiting for P 2, P 2 for P 3, etc. All of 1 4 necessary for deadlock to occur Two approaches to dealing with deadlock: - Pro-active: prevention - Reactive: detection + corrective action 9/27/17 CS 318 Lecture 8 Deadlock 8

Prevent by Eliminating One Condition 1. Mutual exclusion - Buy more resources, split into pieces, or virtualize to make "infinite" copies - Threads: threads have copy of registers = no lock 2. Hold and wait - Wait on all resources at once (must know in advance) 3. No preemption - Physical memory: virtualized with VM, can take physical page away and give to another process! 4. Circular wait - Single lock for entire system: (problems?) - Partial ordering of resources (next) 9/27/17 CS 318 Lecture 8 Deadlock 9

Resource Allocation Graph View system as graph - Processes and Resources are nodes - Resource Requests and Assignments are edges Process: Resource with 4 instances: P i requesting R j : P i holding instance of R j : 9/27/17 CS 318 Lecture 8 Deadlock 10

Example Resource Allocation Graph 9/27/17 CS 318 Lecture 8 Deadlock 11

Resource Allocation Graph with Deadlock 9/27/17 CS 318 Lecture 8 Deadlock 12

Is This Deadlock? 9/27/17 CS 318 Lecture 8 Deadlock 13

Cycles and Deadlock If graph has no cycles no deadlock If graph contains a cycle - Definitely deadlock if only one instance per resource (waits-for graph (WFG)) - Otherwise, maybe deadlock, maybe not Prevent deadlock with partial order on resources - e.g., always acquire mutex m 1 before m 2 - Usually design locking discipline for application this way 9/27/17 CS 318 Lecture 8 Deadlock 14

Dealing With Deadlock There are four approaches for dealing with deadlock: - Ignore it how lucky do you feel? - Prevention make it impossible for deadlock to happen - Avoidance control allocation of resources - Detection and Recovery look for a cycle in dependencies 9/27/17 CS 318 Lecture 8 Deadlock 15

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 9/27/17 CS 318 Lecture 8 Deadlock 16

Banker s Algorithm The Banker s Algorithm is the classic approach to deadlock avoidance for resources with multiple units 1. Assign a credit limit to each customer (process) - Maximum credit claim must be stated in advance 2. Reject any request that leads to a dangerous state - A dangerous state is one where a sudden request by any customer for the full credit limit could lead to deadlock - A recursive reduction procedure recognizes dangerous states 3. In practice, the system must keep resource usage well below capacity to maintain a resource surplus - Rarely used in practice due to low resource utilization 9/27/17 CS 318 Lecture 8 Deadlock 17

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 9/27/17 CS 318 Lecture 8 Deadlock 18

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 9/27/17 CS 318 Lecture 8 Deadlock 19

Deadlock Recovery Once a deadlock is detected, we have two options 1. Abort processes - Abort all deadlocked processes Processes need to 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 9/27/17 CS 318 Lecture 8 Deadlock 20

Deadlock 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 9/27/17 CS 318 Lecture 8 Deadlock 21

Next time Read Chapter 15, 16, 18 9/27/17 CS 318 Lecture 8 Deadlock 22