Operating Systems 2010/2011

Similar documents
Operating Systems 2006/2007

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

The Deadlock Problem (1)

Deadlocks. Dr. Yingwu Zhu

Chapter 8: Deadlocks

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

Chapter 7: Deadlocks

What is Deadlock? Two or more entities need a resource to make progress, but will never get that resource. Examples from everyday life:

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

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

CSE Opera+ng System Principles

The Deadlock Problem

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

Chapter 7: Deadlocks

System Model. Types of resources Reusable Resources Consumable Resources

OPERATING SYSTEMS. Deadlocks

Concurrency: Deadlock and Starvation. Chapter 6

Chapter - 4. Deadlocks Important Questions

Principles of Operating Systems

Last Class: Monitors. Real-world Examples

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

Last Class: Synchronization Problems!

Chapter 8: Deadlocks. The Deadlock Problem

CS370 Operating Systems

Module 6: Deadlocks. Reading: Chapter 7

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

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

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

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

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

The deadlock problem

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

Deadlocks. Copyright : University of Illinois CS 241 Staff 1

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

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

Deadlocks. Deadlock Overview

Chapter 7: Deadlocks

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

CMSC 412. Announcements

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

The Deadlock Problem

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.

Operating Systems. Deadlocks. Stephan Sigg. November 30, Distributed and Ubiquitous Systems Technische Universität Braunschweig

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

Deadlocks. Prepared By: Kaushik Vaghani

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

CS370 Operating Systems

Module 7: Deadlocks. The Deadlock Problem

CS420: Operating Systems. Deadlocks & Deadlock Prevention

Principles of Operating Systems

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

Chapter 8: Deadlocks. Operating System Concepts with Java

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

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

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

Chapter 7: Deadlocks

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

CS370 Operating Systems

Chapter 7: Deadlocks CS370 Operating Systems

CS370 Operating Systems

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

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

Operating Systems. Deadlocks

CS307 Operating Systems Deadlocks

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

Deadlocks. System Model

Basics of Deadlock Theory. Deadlocks

Resource Management and Deadlocks 1

Operating Systems ECE344. Ding Yuan

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

Operating Systems CMPSC 473. Deadlocks February 28, Lecture 13 Instructor: Trent Jaeger

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

CS307: Operating Systems

Deadlock Risk Management

Resource Management and Deadlocks 43

Deadlocks. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CISC 7310X. C10: Deadlocks. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 4/12/2018 CUNY Brooklyn College

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

Chapter 3. Deadlocks

Chapter 6 Concurrency: Deadlock and Starvation

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

Deadlock Prevention. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Resource Allocation and Deadlock Handling

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

Operating Systems. Deadlock. User OS. Kernel & Device Drivers. Interface Programs. Brian Mitchell - Operating Systems

CSE 380 Computer Operating Systems

Potential Deadlock Example

Operating Systems 2015 Spring by Euiseong Seo DEAD LOCK

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

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

CMPT 300 Introduction to Operating Systems

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

Concurrency: Deadlock and Starvation

Chapter 8: Deadlocks. The Deadlock Problem

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

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

Transcription:

Operating Systems 2010/2011 Blocking and locking (with figures from Bic & Shaw) Johan Lukkien 1

Blocking & locking Blocking: waiting for a certain condition to become true Starvation: unpredictable, even infinite blocking times the opposite of fairness can be the result of interference of scheduling and blocking typical, in real-time systems can be the result of cooperation of several processes e.g. the dining philosophers example perhaps in combination with a greedy scheduler happens through livelock or other forms of blocking Livelock repeated trying to pass a critical condition without making progress typically, associated with polling results functionally in starvation or deadlock Deadlock extreme case of starvation: continuation not possible typically, a cul-de-sac state of the system 2

Blocking times and scheduling: priority inversion A low priority task obtains a resource; a high priority task waits on it A middle priority task pre-empts the low priority task the high priority task now waits on the middle priority task... and executes effectively at the low priority... unfairness, hence starvation danger high priority task waiting middle priority task executing high priority task waiting resource blocked by low priority task resource blocked by low priority task time time 3

A solution: priority inheritance protocol The priority of the task P using the resource is dynamically adjusted to be the maximum of the priority of any other task that is blocked on the allocated resources of P (... and its own priority) low priority job increases priority high priority job waiting...middle priority jobs will wait now. middle priority job waiting resource blocked by low priority job time 4

Recall action synchronization: preventing deadlock The exercises A4, A7, A8, give the following insights for deadlock prevention Let critical sections terminate in principle, no P operations between P(m)...V(m) Use a fixed order in P-operations on semaphores P(m);P(n);... in one process may deadlock with P(n);P(m);... in another process in fact: satisfy the synchronization conditions in a fixed order this can be easily verified inside an operating system Beware of greedy consumers Let P(a) k be an indivisable operation when there is a danger of deadlock In general: avoid cyclic waiting! 5

Deadlock: terminology Deadlock is usually associated with access to resources consumable resources: resource use takes it away (variable number) typical producer / consumer problems example: characters typed at a keyboard, blocks of data from the network reusable resources: resource is shared (fixed number) typical mutual exclusion problems (consumer and producer are same process), readers/writers problems example: processor, memory blocks,... physical entities 6

Definitions We call a task (process or thread) blocked if: it is waiting on a blocking synchronization action or has terminated (for technical reasons included) Definition: a set D of tasks with at least one not terminated is called deadlocked if all tasks in D are blocked, and for each non-terminated task t in D, any task that might unblock t is also in D 7

Deadlock: conditions Conditions for deadlock to be possible mutual exclusion hold and wait greediness, several resources of the same type as well as several of different types are reserved incrementally no preemption circular waiting These all play a role and can be addressed explicitly in the solution i.e., deadlock is addressed by avoiding these conditions However, deadlock is more general than these four it may depend on general synchronization conditions (cf. condition synchronization) 8

Model for analysis: graphs For consumable resources and general condition synchronization, the graph representation is the wait-for graph nodes: tasks i.e., the activities, thread/process edges: a wait-for, or blocked-on relationship an edge t0 -> t1 means that t0 may unblock t1 9

Wait-for graph p1: x<0... P(m); x := x+y; sigall (c); V(m)... p2: b... P(m); x := a; sigall (c); V(m)... x<0 p3:... P(m); while x<0 do wait (m,c) od; x := x-1; b := true; signal (d); V(m)... p4:... P(m); while not b do wait (m, d) od;...b := false; V(m)... The graph captures a possible dynamic situation, a system state possibility of existence of the graph needs evidence e.g., if a is always negative, there is never a state with an arrow p2 p3 may label the arrows with corresponding conditions i.e., state information about the state when this waiting occurs e.g., if this graph occurs, we have: x<0 and b Note: we leave out the dependence on m since we know mutual exclusion does not add to deadlock provided critical sections terminate. Question: what are the critical sections then? 10

Model for analysis: graphs For consumable resources and general condition synchronization, the graph representation is the wait-for graph nodes: tasks i.e., the activities, thread/process edges: a wait-for, or blocked-on relationship an edge t0 -> t1 means that t0 may unblock t1 With reusable resources (action synchronization), we use the resource dependency graph bipartite graph with two classes of nodes: tasks & resources edges of three types, capturing states,and classes of states task has requested and now waits for the resource task has acquired (holds) the resource task may request the resource the graph captures dynamic states (a particular one, or all) three events change the state request (by a task), acquire (response to a request by the system, according to a policy), release (by the task) 11

Edges Resource dependency graph p R : p requests R R p : p holds R p blocked: it has a (stable, not directly removable) outgoing arrow removable: the requested resource is free Examples p1 holds one of R1 p2 holds one of R1 and requests two of R2 12

Analysis: reduction Assume that the graph represents a stable state Repeatedly remove a non-blocked task and all its incoming connections this represents possible completion of that task Remaining set: deadlocked a knot Sufficient condition for deadlock for the greedy allocation policy: existence of a knot in the original graph greedy: direct allocation decision based on availability this greedy allocation could be implemented just with a semaphore (counting the resources) Question: is the example reducible? 13

Reduction in progress... 14

Dealing with deadlock Deadlock occurs infrequent because low probability of occurrence considered as a problem by programmers hence, most often solved Approaches Ignore timeouts external intervention Prevent (avoidance from programmer side) using the discussed design techniques Avoid (system side) dynamic checks Detect and recover 15

Prevention Make the reduced dependency graphs empty by construction Prevent cycles in the wait-for graphs Use the mentioned techniques (see before on action synchronization) prevent greediness, use fixed nesting (prevent circular wait), have terminating critical sections not always possible Use preemption of the resource when needed not always possible All resources at once (through condition synchronization) avoid the wait-and-hold greediness leads to possible starvation, see Philosophers 16

Prevention (cnt d) Avoid cyclic dependencies (hence circular wait) extremely boring programs Prove correctness, typically by contraposition assume, a deadlock occurs, show a contradiction in principle: examine all possible combinations of blocking states in all tasks examine the corresponding dependency and wait-for graphs and show that deadlocked ones are not possible or not reachable for example, for resource dependency graph: examine the reachable states of the Finite State Machine corresponding to the request/acquisition sequences 17

Example: state diagrams and reachable states Example of a system trace of p1 and p2 2 resources, R1 and R2 p2 reserves in opposite order than p1 State diagram according to all possible traces deadlocked state turns out to be reachable 18

19

Avoidance prevent from system side Maintain as an invariant that no deadlocked sets can occur upon each blocking action investigate if always an open execution path remains (system remains in a safe state) e.g. check semaphore P-operation order for each task otherwise, deny the action or postpone completion Note: just make sure the reduced dependency is empty Postponing works if the blocking actions refer to resource allocations then we can compute the future states example: bankers algorithm need information about possible system behaviors in terms of resource requirements (maximum numbers) requires separation between request and grant hence, no greediness in the granting! very difficult (impossible?) to extend to general condition synchronization needs information about the evolution and modification of variables 20

Given: Bankers algorithm: problem description set of N tasks set of R resources c[j] : number of type j resources max[i,j] : maximum number of type j resources task i needs Tasks acquire resources incrementally and release those eventually Requirement synchronize requests such that always each task can acquire resources until its specified maximum not having this requirement represents a deadlock (potential? or guaranteed?) 21

(Maximum) claim graph The specified maximum is included in the resource dependency graph, as dashed arrows This gives the maximum claim graph 22

Problem analysis Problem: while giving out resources arbitrarily, a state may be reached such that no additional request can be served of some tasks (not enough left), ever hence these tasks can never proceed, and will also never give back their reservations... actually, an instance of wait-and-hold 23

Given: set of N tasks set of R resources State c[j] : max[i,j] : Formalization number of type j resources maximum number of type j resources task i needs av[j] : available type j resources alloc[i,j] : type j resources in use by task i claim[i,j] : maximum number type j resources to be claimed by task i initially, claim = max, av = c, alloc = 0 Invariants:» av[j] = c[j] - ( i: : alloc[i,j])» claim [i,j] + alloc[i,j] = max[i,j] Note: we use vector and matrix addition, vector comparison and assignment with the 0 vector all interpreted componentwise 24

Formalization (cnt d) A state is called safe for a task if this task can be given its maximum number of resources eventually possibly by giving available resources to other tasks first and then waiting until these release them again if that is not needed the state is called open for that task claim[i,j] <= Av[j], all j (or: claim[i] <= Av) A state is called safe in general if it is safe for all tasks The initial state must be safe (what does this mean?) A new state resulting from granting a request is accepted only if it is safe note: just requesting does not change state safety Now the problem reduces to verification of state safety 25

Example: avoiding deadlock Avoiding deadlock (a) to (b): give resource to p1 leaves a reducible graph (a) to (c): granting p2 s request leaves a non-reducible graph hence denied or postponed Note: also granting p3 s request is safe 26

State safety upon new request Assume the current state is safe Consider a new request by task i req[j] denotes the number of type j resources requested by task i The new state is obtained as follows av, alloc[i], claim[i] := av-req, alloc[i]+req, claim[i]-req It is enough to verify whether this new state is safe for just task i if it is, then eventually all its resources will be returned thus reaching an even safer state than the one before the adjustment Task i is safe when its claims can be satisfied, either directly or by completing any of the other tasks (this must be verified) Note: completing the open tasks corresponds to the graph reduction mentioned before 27

Algorithm func NextOpen (Av: Vector; Claim, Alloc: Matrix): int { returns index of first open and relevant task (one that has claimed resources) } [ for i := 0 to N-1 do if Alloc[i] <> 0 and Claim[i] <= Av then return(i) fi od; return (N) ] ; func Safe (Av: Vector; Claim, Alloc: Matrix; req: Vector; i: int): bool { returns whether the parameters encode a transition to a new safe state precondition: Av <= Req (otherwise the transition is impossible anyway) } [ var k: int; Av, Alloc[i], Claim[i] := Av-req, Alloc[i]+req, Claim[i]-req; { determine new state } k := NextOpen (Av, Claim, Alloc); while k <> N and not (Claim[i] <= Av) { there are open tasks left and i itself is not open in current state } do Av, Alloc[k], Claim[k] := Av+Alloc[k], 0, Max[k]; { assume open task k completes } k := NextOpen (Av, Claim, Alloc); od; return (Claim[i] <= Av); ] 28

Incremental upon each blocking action Detection investigate if a deadlock occurs if so, deny the action this may be too late though, roll-back may be needed close to avoidance example: checking of semaphore nesting Repeatedly, monitor the system gives overhead needs detection algorithm 29

Detection (cnt d) Dealing with the error locally, inside the task that tried blocking globally, through a recovery policy kill all selectively, based on criteria, like priority, progress made etc. roll back to safe state works only if alternatives exist need recording checkpoints, i.e., enough history information to restart preempt resources, if possible select victom based on criteria however, one may argue that the deadlock does not exist in this case 30

Strategy depends on resource type Swap space in secondary memory reserve all, or avoidance Process resources: files, printers,... require declaration ahead of time use avoidance Main memory prevention through preemption see swap space if preemption is not possible Internal, shared resources (i/o ports, shared memory segments) prevention through ordering 31

Overview (Stallings) additional starvation 32