Deadlock. CSE 2431: Introduction to Operating Systems Reading: Chap. 7, [OSC]

Similar documents
CSC501 Operating Systems Principles. Deadlock

CS 453 Operating Systems. Lecture 7 : Deadlock

Last class: Today: Synchronization. Deadlocks

Deadlock (2) Dave Eckhardt Brian Railing Roger Dannenberg

Deadlock Ezio Bartocci Institute for Computer Engineering

CS162 Operating Systems and Systems Programming Lecture 7 Semaphores, Conditional Variables, Deadlocks"

Operating Systems. VII. Synchronization

Improper Nesting Example

Clojure Concurrency Constructs, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 13 10/07/2014

CSE 380 Computer Operating Systems

CIS 4930/6930: Principles of Cyber-Physical Systems

Lecture 4: Process Management

Lab Course: distributed data analytics

INF 4140: Models of Concurrency Series 3

Safety and Liveness. Thread Synchronization: Too Much Milk. Critical Sections. A Really Cool Theorem

1 st Semester 2007/2008

Exercises Solutions. Automation IEA, LTH. Chapter 2 Manufacturing and process systems. Chapter 5 Discrete manufacturing problems

Real Time Operating Systems

Shared resources. Sistemi in tempo reale. Giuseppe Lipari. Scuola Superiore Sant Anna Pisa -Italy

Barrier. Overview: Synchronous Computations. Barriers. Counter-based or Linear Barriers

Real Time Operating Systems

Simulation of Process Scheduling Algorithms

CPU scheduling. CPU Scheduling

Modal and Temporal Logics

Lesson 3: Using Linear Combinations to Solve a System of Equations

Distributed Systems Principles and Paradigms. Chapter 06: Synchronization

Distributed Systems Principles and Paradigms

Overview: Synchronous Computations

Correctness of Concurrent Programs

TDDB68 Concurrent programming and operating systems. Lecture: CPU Scheduling II

Causality & Concurrency. Time-Stamping Systems. Plausibility. Example TSS: Lamport Clocks. Example TSS: Vector Clocks

CPU Scheduling. Heechul Yun

The Weakest Failure Detector to Solve Mutual Exclusion

INF Models of concurrency

Che-Wei Chang Department of Computer Science and Information Engineering, Chang Gung University

FACTORS AFFECTING CONCURRENT TRUNCATE

CPU SCHEDULING RONG ZHENG

Process Scheduling for RTS. RTS Scheduling Approach. Cyclic Executive Approach

Scheduling I. Today Introduction to scheduling Classical algorithms. Next Time Advanced topics on scheduling

Process Scheduling. Process Scheduling. CPU and I/O Bursts. CPU - I/O Burst Cycle. Variations in Bursts. Histogram of CPU Burst Times

Generate i/o load on your vm and use iostat to demonstrate the increased in i/o

An Optimal k-exclusion Real-Time Locking Protocol Motivated by Multi-GPU Systems

Scheduling I. Today. Next Time. ! Introduction to scheduling! Classical algorithms. ! Advanced topics on scheduling

6.852: Distributed Algorithms Fall, Class 24

CMSC 451: Lecture 7 Greedy Algorithms for Scheduling Tuesday, Sep 19, 2017

Sub-Optimal Scheduling of a Flexible Batch Manufacturing System using an Integer Programming Solution

Bounded Delay for Weighted Round Robin with Burst Crediting

Latches. October 13, 2003 Latches 1

CPU Scheduling. CPU Scheduler

Introduction to functions

CS/IT OPERATING SYSTEMS

1 Lamport s Bakery Algorithm

Chapter 6: CPU Scheduling

Lecture Note #6: More on Task Scheduling EECS 571 Principles of Real-Time Embedded Systems Kang G. Shin EECS Department University of Michigan

Non-Work-Conserving Non-Preemptive Scheduling: Motivations, Challenges, and Potential Solutions

CSE 105 Theory of Computation

Methods for the specification and verification of business processes MPB (6 cfu, 295AA)

Embedded Systems 14. Overview of embedded systems design

CptS 464/564 Fall Prof. Dave Bakken. Cpt. S 464/564 Lecture January 26, 2014

Real-time operating systems course. 6 Definitions Non real-time scheduling algorithms Real-time scheduling algorithm

Impossibility of Distributed Consensus with One Faulty Process

Allocate-On-Use Space Complexity of Shared-Memory Algorithms

Today s Agenda: 1) Why Do We Need To Measure The Memory Component? 2) Machine Pool Memory / Best Practice Guidelines

Lecture 9: Cri,cal Sec,ons revisited, and Reasoning about Programs. K. V. S. Prasad Dept of Computer Science Chalmers University Monday 23 Feb 2015

CS 152 Computer Architecture and Engineering. Lecture 17: Synchronization and Sequential Consistency

Exploring an Unknown Graph to Locate a Black Hole Using Tokens

Solutions to the second midterm. COSC 4330/6310 Summer 2013

Module 5: CPU Scheduling

Decentralized Control of Discrete Event Systems with Bounded or Unbounded Delay Communication

Introduction to Model Checking. Debdeep Mukhopadhyay IIT Madras

Distributed Algorithms (CAS 769) Dr. Borzoo Bonakdarpour

Safety and Liveness Properties

Coordination. Failures and Consensus. Consensus. Consensus. Overview. Properties for Correct Consensus. Variant I: Consensus (C) P 1. v 1.

Abstractions and Decision Procedures for Effective Software Model Checking

CHAPTER 5 - PROCESS SCHEDULING

Comp 204: Computer Systems and Their Implementation. Lecture 11: Scheduling cont d

Lock using Bakery Algorithm

Agreement. Today. l Coordination and agreement in group communication. l Consensus

More Turing Machines. CS154 Chris Pollett Mar 15, 2006.

1 Introduction (January 21)

Announcements. Project #1 grades were returned on Monday. Midterm #1. Project #2. Requests for re-grades due by Tuesday

UC Santa Barbara. Operating Systems. Christopher Kruegel Department of Computer Science UC Santa Barbara

LSN 15 Processor Scheduling

DRAFT - do not circulate

CEC 450 Real-Time Systems

A subtle problem. An obvious problem. An obvious problem. An obvious problem. No!

Lecture 3: Decidability

CS 550 Operating Systems Spring CPU scheduling I

CPU Scheduling Exercises

P (E) = P (A 1 )P (A 2 )... P (A n ).

The algorithmic analysis of hybrid system

Scheduling Slack Time in Fixed Priority Pre-emptive Systems

Activity selection. Goal: Select the largest possible set of nonoverlapping (mutually compatible) activities.

Outline. policies for the first part. with some potential answers... MCS 260 Lecture 10.0 Introduction to Computer Science Jan Verschelde, 9 July 2014

Recognizing Safety and Liveness by Alpern and Schneider

TDDI04, K. Arvidsson, IDA, Linköpings universitet CPU Scheduling. Overview: CPU Scheduling. [SGG7] Chapter 5. Basic Concepts.

416 Distributed Systems

Single-part-type, multiple stage systems. Lecturer: Stanley B. Gershwin

THE WEAKEST FAILURE DETECTOR FOR SOLVING WAIT-FREE, EVENTUALLY BOUNDED-FAIR DINING PHILOSOPHERS. A Dissertation YANTAO SONG

Clocks in Asynchronous Systems

Transcription:

Deadlock CSE 2431: Introduction to Operating Systems Reading: Chap. 7, [OSC] 1

Outline Resources Deadlock Deadlock Prevention Deadlock Avoidance Deadlock Detection Deadlock Recovery 2

Review: Synchronization Data races Critical regions and mutual exclusions Solutions: Peterson s solution TSL Semaphores & Mutex Monitor Message Passing Barrier Classic Synchronization Problems 3

Resource (1) A resource is a commodity needed by a process. Resources can be either: Serially reusable: e.g., CPU, memory, disk space, I/O devices, files. acquire use release Consumable: produced by a process, needed by a process; e.g., messages, buffers of information, interrupts. create acquire use Resource ceases to exist after it has been used, so it is not released. 4

Resource (2) Resources can also be either: Preemptible: e.g., CPU, central memory or Non-preemptible: e.g., tape drives. And resources can be either: Shared among several processes or Dedicated exclusively to a single process. 5

Outline Resources Deadlock Deadlock Prevention Deadlock Avoidance Deadlock Detection Deadlock Recovery 6

Using Semaphore to Share Resource 2 3 4 5 Process P(); { A.Wait(); B.Wait(); 0 6 //use both resource B.Signal(); A.Signal(); } Process Q(); { A.Wait(); B.Wait(); //use both resource B.Signal(); A.Signal(); } External Semaphore A(1), B(1); 2 External Semaphore A(0), B(1); 3 External Semaphore A(0), B(0); 4 External Semaphore A(0), B(1); 5 External Semaphore A(1), B(1); 7

But Deadlock can Happen! 1 2 Process P(); 1 { A.Wait(); 3 B.Wait(); //use both resources B.Signal(); A.Signal(); } Process Q(); { B.Wait(); A.Wait(); //use both resources A.Signal(); B.Signal(); } 1 External Semaphore A(1), B(1); 2 External Semaphore A(0), B(1); 3 External Semaphore A(0), B(0); DEADLOCK! 8

Deadlock Mechanisms for Deadlock Control 9

Deadlock Definition What is a deadlock? A process is deadlocked if it is waiting for an event that will never occur. Typically, but not necessarily, more than one process will be involved together in a deadlock (the deadly embrace). Is deadlock the same as starvation (or infinitely postponed)? A process is infinitely postponed if it is delayed repeatedly over a long period of time while the attention of the system is given to other processes. i.e. logically the process may proceed but the system never gives it resources. 10

Conditions for Deadlock What conditions should exist in order to lead to a deadlock Real life analogy such as You take the monitor, I grab the keyboard Cars in intersection Your turn for real life analogies 11

Necessary Conditions for Deadlock Mutual exclusion Processes claim exclusive control of the resources they require Wait-for condition Processes hold resources already allocated to them while waiting for additional resources No preemption condition Resources cannot be removed from the processes holding them until used to completion Circular wait condition A circular chain of processes exists in which each process holds one or more resources that are requested by the next process in the chain 12

Resource Allocation Graph 13

Deadlock Model 14

How To Deal With Deadlock? Prevention Design a system in such a way that deadlocks cannot occur, at least with respect to serially reusable resources. Avoidance Impose less stringent conditions than for prevention, allowing the possibility of deadlock, but sidestepping it as it approaches. Detection In a system that allows the possibility of deadlock, determine if deadlock has occurred, and which processes and resources are involved. Recovery After a deadlock has been detected, clear the problem, allowing the deadlocked processes to complete and the resources to be reused. Usually involves destroying the affected processes and restarting them. 15

Outline Resources Deadlock Deadlock Prevention Deadlock Avoidance Deadlock Detection Deadlock Recovery 16

The Ostrich Algorithm Guess: what is implemented in Linux? Don t do anything, simply restart the system (stick your head into the sand, pretend there is no problem at all). Rationale: make the common path faster and more reliable Deadlock prevention, avoidance or detection/recovery algorithms are expensive if deadlock occurs only rarely, it is not worth the overhead to implement any of these algorithms. 17

Deadlock Prevention: Havender s Algorithms Break one of the deadlock conditions. Mutual exclusion Solution: exclusive use of resources is an important feature, but for some resources (virtual memory, CPU), it is possible. Hold-and-Wait condition Solution: Force each process to request all required resources at once. It cannot proceed until all resources have been acquired. No preemption condition Solution: forcibly take away the resources assigned to the process due to lack of other requested resources. Circular wait condition Solution: All resource types are numbered. Processes must request resources in numerical order 18

Locking with Two Phases Phase One process tries to lock all records it needs, one at a time if needed record found locked, start over (no real work done in phase one) If phase one succeeds, it starts second phase performing updates releasing locks Note similarity to requesting all resources at once Problems? 19

Break Circular Wait Condition Request one resource at a time. Global ordering of resources Requests have to be made in increasing order Req(resource1), req(resource2).. Why no circular wait? Problems? 20

Summary: Deadlock Prevention Condition Mutual Exclusion Hold and wait No preemption Circular wait How to Break It Spool everything Request all resources initially Take resources away Order resources numerically 21

Review Resource: preemptible or non-preemptible Deadlock conditions Mutual exclusion Hold and wait Non-preemption Circular wait Deadlock prevention Break the condition Locking with two phases 22

Questions Design an algorithm to detect deadlocks? Single resource per type Multiple resources per type How to avoid deadlocks? How to recover from deadlocks? 23

Deadlock Avoidance The system needs to know the resource ahead of time Banker Algorithm (Dijkstra, 1965) Each customer tells banker the maximum number of resources it needs Customer borrows resources from banker Customer returns resources to banker Customer eventually pays back loan Banker only lends resources if the system will be in a safe state after the loan Safe state - there is a lending sequence such that all customers can take out a loan Unsafe state - there is a possibility of deadlock 24

Safe State and Unsafe State Safe State there is some scheduling order in which every process can run to completion even if all of them suddenly request their maximum number of resources immediately From a safe state, the system can guarantee that all processes will finish Unsafe state: no such guarantee Not a deadlock state Some process may be able to complete 25

Is Allocation (1 0 2) to P1 Safe? Process Alloc Max Need Available A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 3 3 3 2 P1 2 0 0 3 2 2 1 2 2 P2 3 0 0 9 0 2 6 0 2 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 If P1 requests max resources, can complete 26

Run Safety Test Process Alloc Max Need Available A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 3 3 3 2 P1 3 0 2 3 2 2 0 2 0 2 3 0 P2 3 0 0 9 0 2 6 0 2 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 If P1 requests max resources, can complete 27

Allocate to P1, Then Process Alloc Max Need Available A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 3 2 3 0 P1 3 2 2 3 2 2 0 0 0 2 1 0 P2 3 0 0 9 0 2 6 0 2 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 28

Release: P1 Finishes Process Alloc Max Need Available A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 3 2 1 0 P1 0 0 0 3 2 2 3 2 2 5 3 2 P2 3 0 0 9 0 2 6 0 2 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 Now P3 can acquire max resources and release 29

Release - P3 Finishes Process Alloc Max Need Available A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 3 5 3 2 P1 0 0 0 3 2 2 3 2 2 7 4 3 P2 3 0 0 9 0 2 6 0 2 P3 0 0 0 2 2 2 2 2 2 P4 0 0 2 4 3 3 4 3 1 Now P4 can acquire max resources and release 30

Release - P4 Finishes Process Alloc Max Need Available A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 3 7 4 3 P1 0 0 0 3 2 2 3 2 2 7 4 5 P2 3 0 0 9 0 2 6 0 2 P3 0 0 0 2 2 2 2 2 2 P4 0 0 0 4 3 3 4 3 3 Now P2 can acquire max resources and release 31

Release - P2 Finishes Process Alloc Max Need Available A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 3 7 4 5 P1 0 0 0 3 2 2 3 2 2 10 4 5 P2 0 0 0 9 0 2 9 0 2 P3 0 0 0 2 2 2 2 2 2 P4 0 0 0 4 3 3 4 3 3 Now P0 can acquire max resources and release 32

So P1 Allocation (1 0 2) Is Safe Process Alloc Max Need Available A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 3 3 3 2 P1 3 0 2 3 2 2 0 2 0 2 3 0 P2 3 0 0 9 0 2 6 0 2 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 33

Is allocation (0 2 0) to P0 Safe? Process Alloc Max Need Available A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 3 2 3 0 P1 3 0 2 3 2 2 0 2 0 P2 3 0 0 9 0 2 6 0 2 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 Try to allocate 2 B to P0 34

Run Safety Test Process Alloc Max Need Available A B C A B C A B C A B C P0 0 3 0 7 5 3 7 2 3 2 3 0 P1 3 0 2 3 2 2 0 2 0 2 1 0 P2 3 0 0 9 0 2 6 0 2 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 No process can get max resources and release them 35

So Unsafe State Do Not Enter Process Alloc Max Need Available A B C A B C A B C A B C P0 0 3 0 7 5 3 7 2 3 2 3 0 P1 3 0 2 3 2 2 0 2 0 2 1 0 P2 3 0 0 9 0 2 6 0 2 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 Return to safe state and do not allocate resource 36

P0 Suspended Pending Request Process Alloc Max Need Available A B C A B C A B C A B C P0 0 1 0 7 5 3 7 4 3 2 3 0 P1 3 0 2 3 2 2 0 2 0 P2 3 0 0 9 0 2 6 0 2 P3 2 1 1 2 2 2 0 1 1 P4 0 0 2 4 3 3 4 3 1 When enough resources become available, P0 can awaken 37

Results P0 s request for 2 Bs cannot be granted because that would prevent any other process from completing if they need their maximum claim 38

How to Compute Safety? Given: n kinds of resources p processes Set P of processes struct {resource needs[n], owns[n]} ToDo[p] resource_available[n]... while there exists p in P such that for all i (ToDo[p].needs[i] < available[i]) { for all i { available[i] += ToDo[p].owns[i]; P = P {p}; } } If (P is empty) then system is safe; 39

Just Because It s Unsafe P0 could have been allocated 2 Bs and a deadlock might not have occurred if: P1 didn t use its maximum resources but finished using the resources it had 40

If P1 Doesn t Need Max Process Alloc Max Need Available A B C A B C A B C A B C P0 3 0 2 1 0 P1 0 0 5 1 2 P2 0 0 P3 1 1 P4 0 2 Then P0 would have finished 41

Tradeoff What is the tradeoff? The banker s algorithm is conservative it reduces parallelism for safety s sake 42

Banker Solution Issues Process needs to indicate its resource needs in the beginning Process may not terminate Process may request more than claimed A process may suffer infinite postponement (starvation problem) 43

Deadlock Detection Check to see if a deadlock has occurred! Single resource per type Can use wait-for graph Check for cycles How? How about multiple resources per type? 44

Wait-For Graphs Resource allocation graph Corresponding wait-for graph 45

Detection: Multiple Resources per Type Run variant of banker s algorithm to see if processes can finish Time: O(mn 2 ), m: # resources, n: # processes Optimizations? Optimistic version check only that at least one process can finish The deadlock detection is delayed How often should the detection algorithm be run? 46

Options: Recovery From Deadlock Kill deadlocked processes and release resources Kill one deadlocked process at a time and release its resources Rollback all or one of the processes to a checkpoint that occurred before they requested any resources Note: may have starvation problems 47

Deadlock Summary In general, deadlock detection or avoidance is expensive Must evaluate cost of deadlock against detection or avoidance costs Deadlock avoidance and recovery may cause starvation problems UNIX and Windows use Ostrich Algorithm 48