CSC501 Operating Systems Principles. Deadlock

Similar documents
CS 453 Operating Systems. Lecture 7 : Deadlock

Last class: Today: Synchronization. Deadlocks

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

Deadlock Ezio Bartocci Institute for Computer Engineering

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

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

CIS 4930/6930: Principles of Cyber-Physical Systems

CPU Scheduling. Heechul Yun

Real Time Operating Systems

Deadlock (2) Dave Eckhardt Brian Railing Roger Dannenberg

Operating Systems. VII. Synchronization

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

Improper Nesting Example

CSE 380 Computer Operating Systems

Real Time Operating Systems

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

Lab Course: distributed data analytics

Clock-driven scheduling

Lock using Bakery Algorithm

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

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

EECS 571 Principles of Real-Time Embedded Systems. Lecture Note #7: More on Uniprocessor Scheduling

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

Resource Sharing in an Enhanced Rate-Based Execution Model

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

CPU scheduling. CPU Scheduling

Aperiodic Task Scheduling

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

Lecture 13. Real-Time Scheduling. Daniel Kästner AbsInt GmbH 2013

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

CEC 450 Real-Time Systems

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

Selected solutions for. TDDD07 Real-time Systems

Coin Changing: Give change using the least number of coins. Greedy Method (Chapter 10.1) Attempt to construct an optimal solution in stages.

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

Correctness of Concurrent Programs

INF 4140: Models of Concurrency Series 3

Simulation of Process Scheduling Algorithms

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

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

Real-Time Systems. Event-Driven Scheduling

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

Global Real-Time Semaphore Protocols: A Survey, Unified Analysis, and Comparison

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

Overview: Synchronous Computations

1 st Semester 2007/2008

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

Exam Spring Embedded Systems. Prof. L. Thiele

Embedded Systems 15. REVIEW: Aperiodic scheduling. C i J i 0 a i s i f i d i

Automata Theory CS Complexity Theory I: Polynomial Time

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

Embedded Systems 14. Overview of embedded systems design

Module 5: CPU Scheduling

Safety Analysis Using Petri Nets

Chapter 6: CPU Scheduling

CSE 421 Greedy Algorithms / Interval Scheduling

An Asynchronous Message-Passing Distributed Algorithm for the Generalized Local Critical Section Problem

Lecture 4: Process Management

Non-Preemptive and Limited Preemptive Scheduling. LS 12, TU Dortmund

CPU Scheduling. CPU Scheduler

How to deal with uncertainties and dynamicity?

2/5/07 CSE 30341: Operating Systems Principles

Rate Monotonic Analysis (RMA)

Real-time Systems: Scheduling Periodic Tasks

The Application and Evaluation of Banker s Algorithm for Deadlock-Free Buffer Space Allocation in Flexible Manufacturing Systems

Lecture 1: Contraction Algorithm

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

Distributed Deadlock-Avoidance. IMDEA Software Institute, Spain

Scheduling Slack Time in Fixed Priority Pre-emptive Systems

Exploring an Unknown Graph to Locate a Black Hole Using Tokens

Modal and Temporal Logics

Improved Priority Assignment for the Abort-and-Restart (AR) Model

Lecture 2e Row Echelon Form (pages 73-74)

Algorithmic verification

Embedded Systems Development

1 Introduction. 1.1 The Problem Domain. Self-Stablization UC Davis Earl Barr. Lecture 1 Introduction Winter 2007

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

Blocking time under basic priority inheritance: Polynomial bound and exact computation

4 Sequencing problem with heads and tails

Lecture 3: Decidability

Counting Strategies: Inclusion/Exclusion, Categories

AGREEMENT PROBLEMS (1) Agreement problems arise in many practical applications:

There are three priority driven approaches that we will look at

Logic Design II (17.342) Spring Lecture Outline

Real-Time Scheduling and Resource Management

CHAPTER 2. The Simplex Method

Real-time Scheduling of Periodic Tasks (1) Advanced Operating Systems Lecture 2

Resilience Management Problem in ATM Systems as ashortest Path Problem

Lecture Notes on Software Model Checking

Part 1: You are given the following system of two equations: x + 2y = 16 3x 4y = 2

Measurement and Uncertainty

Scheduling of Frame-based Embedded Systems with Rechargeable Batteries

Real-time Scheduling of Periodic Tasks (2) Advanced Operating Systems Lecture 3

Our Problem. Model. Clock Synchronization. Global Predicate Detection and Event Ordering

Online Energy-Aware I/O Device Scheduling for Hard Real-Time Systems with Shared Resources

Distributed Algorithms Time, clocks and the ordering of events

Distributed Algorithms (CAS 769) Dr. Borzoo Bonakdarpour

The Importance of Irrelevance of Rejected Contracts in Matching under Weakened Substitutes Conditions

On-line scheduling of periodic tasks in RT OS

Linear Programming Redux

Transcription:

CSC501 Operating Systems Principles Deadlock 1

Last Lecture q Priority Inversion Q Priority Inheritance Protocol q Today Q Deadlock 2

The Deadlock Problem q Definition Q A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set Q None of the processes can proceed or back-off (release resources it owns) q Example Q semaphores A and B, initialized to 1 P 0 P 1 wait (A); wait (B); wait(b) wait(a) 3

Deadlock Characterization q Deadlock can arise if four conditions hold simultaneously Q Mutual exclusion: only one process at a time can use a resource Q Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes Q No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task Q 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. 4

Methods for Handling Deadlocks q Ignore the problem and pretend that deadlocks would never occur. q Prevent the system from entering a deadlock state. q Allow the system to enter a deadlock state and then detect/recover. 5

The IGNORE Approach q Pretend there is no problem Q Unfortunately they can occur (an example) Q Reasonable if v Deadlocks occur very rarely cost of prevention is high q Do your typical OSes take this approach? q It is a trade off between Q Convenience Q Correctness 6

The PREVENT Approach q Restrain the ways requests can be made to break one of the four necessary conditions for deadlocks q Attacking the mutual exclusion condition: Q Some devices (such as printer) can be spooled v only the printer daemon uses printer resource v thus deadlock for printer eliminated Q Not all devices can be spooled 7

The PREVENT Approach q Attacking the Hold and Wait Condition: Q Require processes to request all resources before starting q Problems Q may not know required resources at start of run Q also ties up resources other processes could be using q Variation: Q before a process requests for a new resource, it must give up all resources and then request all resources needed 8

The PREVENT Approach q Attacking the No Preemption Condition: Q When a process holding some resources and waiting for others, its resources may be preempted to be used by others q Problems Q Many resources may not allow preemption; i.e., preemption will cause process to fail 9

The PREVENT Approach q Attacking the Circular Wait Condition: Q Impose a total order of all resource types; and require that all processes request resources in the same order 10

Deadlock Avoidance q When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state q System is in safe state if there exists a sequence <P 1, P 2,, P n > of all processes, such that Q Q 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 That is: v v v 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 11

Deadlock Avoidance q If a system is in safe state no deadlocks q If a system is in unsafe state possibility of deadlock q Avoidance ensure that a system will never enter an unsafe state. 12

Resource-Allocation Graph q A set of vertices V and a set of edges E. Q V is partitioned into two types: v P = {P 1, P 2,, P n }, the set consisting of all the processes in the system v R = {R 1, R 2,, R m }, the set consisting of all resource types in the system Q E is partitioned into two types: v request edge directed edge P i R j v assignment edge directed edge R j P i 13

Resource-Allocation Graph q Process q Resource type with 4 instances q P i requests instance of R j P i R j q P i is holding an instance of R j P i Rj 14

Examples q If graph contains no cycles safe q If graph contains a cycle unsafe 15

Resource-Allocation Graph Algorithm q Suppose that process P i requests a resource R j q The request can be granted only if Q Converting the request edge to an assignment edge does not result in the formation of a cycle in the resource-allocation graph 16

Banker s Algorithm q Each process must a priori claim the maximum set of resources that might be needed in its execution. q Safety check Q Repeat v pick any process that can finish with existing available resources; finish it and release all its resources v until no such process exists Q all finished safe; otherwise unsafe. 17

Data Structure for the Banker s Algorithm q n = # of processes, m = # of resources types. Q Available: Vector of length m v If available [j] = k, there are k instances of resource type R j available Q Max: n x m matrix. v If Max [i,j] = k, then process P i may request at most k instances of resource type R j Q Allocation: n x m matrix. v If Allocation[i,j] = k then P i is currently allocated k instances of R j Q Need: n x m matrix. v 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] 18

Safety Algorithm q Step 1: Let Work and Finish be vectors of length m and n, respectively. Initialize: Q Work = Available Q Finish [i] = false for i = 0, 1,, n- 1 q Step 2: Find any i such that both (If no, Step 4) Q Finish [i] = false Q Need i Work q Step 3. Work = Work + Allocation i Q Finish[i] = true Q Step 2 q Step 4. If Finish [i] == true for all i, then the system is in a safe state 19

Resource-Request Algorithm for Process P i q Process P i wants k instances of R j (Request i [j] = k) Q Step 1: If Request i Need i, go to step 2 v Otherwise, raise error condition, since process has exceeded its maximum claim Q Step 2: If Request i Available, go to step 3 Q v Otherwise P i must wait, since resources are not available Step 3: Pretend to allocate requested resources to P i by modifying the state as follows: Available = Available Request; 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 20

Example of Banker s Algorithm q 5 processes P 0 through P 4 ; q 3 resource types: Q A (10 instances), B (5 instances), and C (7 instances) q 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 P 4 0 0 2 4 3 3 Need A B C 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 Question: Is this a safe state? Question: Can request for (1,0,2) by P 1 be granted? 21

Example of Banker s Algorithm q 5 processes P 0 through P 4 ; q 3 resource types: Q A (10 instances), B (5 instances), and C (7 instances) q 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 P 4 0 0 2 4 3 3 Need A B C 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 Question: Can request for (3,3,0) by P 4 be granted? Question: Can request for (0,2,0) by P 0 be granted? 22

Methods for Handling Deadlocks q Ignore the problem and pretend that deadlocks would never occur. q Prevent the system from entering a deadlock state. q Allow the system to enter a deadlock state and then detect/recover. 23

Single Instance of Each Resource Type q Maintain wait-for graph Q Nodes are processes Q P i P j if P i is waiting for P j q Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a deadlock q An algorithm to detect a cycle in a graph requires an order of n 2 operations, where n is the number of vertices in the graph 24

Single Instance of Each Resource Type Resource-Allocation Graph Corresponding wait-for graph 25

Additional Issues q When there are several instances of a resource type Q cycle detection in wait-for graph is not sufficient. q Deadlock detection is very similar to the safety check in the Banker s algorithm 26

Recovery from Deadlock q Recovery through preemption Q take a resource from some other process Q depends on nature of the resource q Recovery through rollback Q checkpoint a process state periodically Q rollback a process to its checkpoint state if it is found deadlocked q Recovery through killing processes Q kill one+ of the processes in the deadlock cycle Q the other processes get its resources Q In which order should we choose process to kill? 27

Next Lecture q Lab2 Session Q Please read the lab2 instructions before the next lecture 28