Yet another synchronization problem

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Deadlocks Today Resources & deadlocks Dealing with deadlocks Other issues Next Time Memory management.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Yet another synchronization problem  The dining philosophers problem  Deadlocks o Modeling deadlocks o Dealing with deadlocks Operating Systems, 2014,
EEE 435 Principles of Operating Systems Deadlock Avoidance, Prevention, and Wrap-Up (Modern Operating Systems 3.5, 3.6, and 3.7)
Operating Systems COMP 4850/CISG 5550 Deadlock Avoidance Dr. James Money.
Chapter 3 Deadlocks TOPICS Resource Deadlocks The ostrich algorithm
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Tanenbaum Ch 6 Silberschatz Ch 7
Chapter 3 Deadlocks - Αδιέξοδα 3.1. Resource
With slides from C. Griwodz, K. Li, A. Tanenbaum and M. van Steen
Avishai Wool lecture Introduction to Systems Programming Lecture 5 Deadlocks.
Deadlock Chapter 3 R1 R2 P2P1 Allocated Requested.
1 CS 333 Introduction to Operating Systems Class 7 - Deadlock Jonathan Walpole Computer Science Portland State University.
Deadlocks. 2 System Model There are non-shared computer resources –Maybe more than one instance –Printers, Semaphores, Tape drives, CPU Processes need.
The Banker’s Algorithm for A Single Resource
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 7: Deadlock Dr. Mohamed Hefeeda.
Deadlock Detection with One Resource of Each Type (1)
Chapter 6 Concurrency: Deadlock and Starvation
Deadlocks Gordon College Stephen Brinton. Deadlock Overview The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 7: Deadlock Dr. Mohamed Hefeeda.
What we will cover…  The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock.
Deadlocks.
Deadlock Chapter 3 Thursday, February 22, Today’s Schedule Assignment #4 from Chapter 3 posted Deadlock - Chapter 3  Skip multiple resources (3.4.2.
CH 3 Deadlock When 2 (or more) processes remain blocked forever!
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance 3.6. Deadlock prevention.
1 Deadlocks 2 Resources Examples of computer resources –printers –tape drives –tables Processes need access to resources in reasonable order Suppose.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
1 Deadlocks Chapter 3. 2 Resources Examples of computer resources –printers –tape drives –tables Processes need access to resources in reasonable order.
Operating Systems 软件学院 高海昌 Operating Systems Gao Haichang, Software School, Xidian University 22 Contents  1. Introduction** 
Operating Systems Part III: Process Management (Deadlocks)
1 Deadlock Definition of deadlock Condition for its occurrence Solutions for avoiding and breaking deadlock –Deadlock Prevention –Deadlock Avoidance –Deadlock.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Operating Systems (OS)
1 MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc.
CHAPTER 8: DEADLOCKS System Model Deadlock Characterization
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
Chapter 7 Deadlocks. 7.1 Introduction Deadlock Common approaches to deal with deadlock – Prevention – Avoidance – Detection and recovery.
CSC 322 Operating Systems Concepts Lecture - 28: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Deadlocks System Model RAG Deadlock Characterization
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 8 Deadlocks. Objective System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection.
Deadlocks Introduction to Operating Systems: Module 7.
Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks.
CH 3 Deadlock When 2 (or more) processes remain blocked forever!
Deadlocks References –text: Tanenbaum ch.3. Deadly Embrace Deadlock definition –A set of process is dead locked if each process in the set is waiting.
Lecture 6 Deadlock 1. Deadlock and Starvation Let S and Q be two semaphores initialized to 1 P 0 P 1 wait (S); wait (Q); wait (Q); wait (S);. signal (S);
Deadlocks Lots of resources can only be used by one process at a time. Exclusive access is needed. Suppose process A needs R1 + R2 and B needs R1 + R2.
Yet another synchronization problem
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Deadlock Management.
Deadlocks References text: Tanenbaum ch.3.
Operating System: DEADLOCKS
Chapter 7 Deadlocks.
Practical Session 12 Deadlocks
Deadlocks References text: Tanenbaum ch.3.
DPNM Lab. Dept. of CSE, POSTECH
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Introduction to Deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Deadlocks References text: Tanenbaum ch.3.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Presentation transcript:

Yet another synchronization problem The dining philosophers problem Deadlocks Modeling deadlocks Dealing with deadlocks Operating Systems, 2011, Danny Hendler & Amnon Meisels

The Dining Philosophers Problem think take forks (one at a time) eat put forks (one at a time) Eating requires 2 forks Pick one fork at a time How to prevent deadlock? What about starvation? What about concurrency? Slide taken from a presentation by Gadi Taubenfeld, IDC Operating Systems, 2011, Danny Hendler & Amnon Meisels

Dining philosophers: definition Each process needs two resources Every pair of processes compete for a specific resource A process may proceed only if it is assigned both resources Every process that is waiting for a resource should sleep (be blocked) Every process that releases its two resources must wake-up the two competing processes for these resources, if they are interested Operating Systems, 2011, Danny Hendler & Amnon Meisels

An incorrect naïve solution ( means “waiting for this fork”) Operating Systems, 2011, Danny Hendler & Amnon Meisels Slide taken from a presentation by Gadi Taubenfeld, IDC

Dining philosophers: textbook solution The solution A philosopher first gets only then it tries to take the 2 forks. Slide taken from a presentation by Gadi Taubenfeld, IDC Operating Systems, 2011, Danny Hendler & Amnon Meisels

Dining philosophers: textbook solution code #define N 5 #define LEFT (i-1) % N #define RIGHT (i+1) % N #define THINKING 0 #define HUNGRY 1 #define EATING 2 int state[N]; semaphore mutex = 1; semaphore s[N]; // per each philosopher void philosopher(int i) { while(TRUE) { think(); pick_sticks(i); eat(); put_sticks(i); } Operating Systems, 2011, Danny Hendler & Amnon Meisels

Dining philosophers: textbook solution code Π void pick_sticks(int i) { down(&mutex); state[i] = HUNGRY; test(i); up(&mutex); down(&s[i]); } void put_sticks(int i) { down(&mutex); state[i] = THINKING; test(LEFT); test(RIGHT); up(&mutex); } void test(int i) { if(state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING) { state[i] = EATING; up(&s[i]); } } Is the algorithm deadlock-free? What about starvation? Operating Systems, 2011, Danny Hendler & Amnon Meisels

Textbook solution code: starvation is possible Eat Block Starvation! Eat Slide taken from a presentation by Gadi Taubenfeld, IDC Operating Systems, 2011, Danny Hendler & Amnon Meisels

Monitor-based implementation procedure test(i){ if (state[LEFT] <> EATING && state[RIGHT] <> EATING && state[i] = HUNGRY) then { state[i] := EATING; signal(self[i]); } for i := 0 to 4 do state[i] := THINKING; end monitor monitor diningPhilosophers condition self[N]; integer state[N]; procedure pick_sticks(i){ state[i] := HUNGRY; test(i); if state[i] <> EATING then wait(self[i]); } procedure put_sticks(i){ state[i] := THINKING; test(LEFT); test(RIGHT); Operating Systems, 2011, Danny Hendler & Amnon Meisels

Text-book solution disadvantages An inefficient solution reduces to mutual exclusion not enough concurrency Starvation possible Operating Systems, 2011, Danny Hendler & Amnon Meisels

Slide taken from a presentation by Gadi Taubenfeld, IDC The LR Solution If the philosopher acquires one fork and the other fork is not immediately available, she holds the acquired fork until the other fork is free. Two types of philosophers: L -- The philosopher first obtains its left fork and then its right fork. R -- The philosopher first obtains its right fork and then its left fork. The LR solution: the philosophers are assigned acquisition strategies as follows: philosopher i is R-type if i is even, L-type if i is odd. R L Slide taken from a presentation by Gadi Taubenfeld, IDC Operating Systems, 2011, Danny Hendler & Amnon Meisels

Theorem: The LR solution is starvation-free Assumption: “the fork is fair”. L R 1 2 3 4 6 R L L R An arbitrary fork to the left of an L-philosopher is numbered 0 Forks at (clockwise) distance i from it, for odd i, are numbered i+1 Forks at (clockwise) distance i from it , for even i, are numbered i-1 The result is a total order on fork numbers and each philosopher picks forks in ascending order. Hence the algorithm is deadlock-free. Since we assume each fork is starvation-free, so is the algorithm. ( means “first fork taken”) Slide taken from a presentation by Gadi Taubenfeld, IDC Operating Systems, 2011, Danny Hendler & Amnon Meisels

Deadlocks The dining philosophers problem Deadlocks Modeling deadlocks Dealing with deadlocks Operating Systems, 2011, Danny Hendler & Amnon Meisels

Synchronization: Deadlocks Operating Systems, 2011, Danny Hendler & Amnon Meisels

Deadlocks Deadlock of Resource Allocation: Process A requests and gets Tape drive Process B requests and gets Fast Modem Process A requests Fast Modem and blocks Process B requests Tape drive and blocks Deadlock situation: Neither process can make progress and no process can release its allocated device (resource) Both resources (devices) require exclusive access Operating Systems, 2011, Danny Hendler & Amnon Meisels

Resources Resources - Tapes, Disks, Printers, Database Records, semaphores, etc. Some resources are non-preemptable (i.e. tape drive) It is easier to avoid deadlock with preemptable resources (e.g., main memory, database records) Resource allocation procedure Request Use Release only at the end – and leave Block process while waiting for Resources Iterate Operating Systems, 2011, Danny Hendler & Amnon Meisels

Defining Deadlocks A set of processes is deadlocked if each process is waiting for an event that can only be caused by another process in the set Necessary conditions for deadlock: 1. Mutual exclusion: exclusive use of resources 2. Hold and wait: process can request resource while holding another resource 3. No preemption: only holding process can release resource 4. Circular wait: there is an oriented circle of processes, each of which is waiting for a resource held by the next in the circle Operating Systems, 2011, Danny Hendler & Amnon Meisels

Process B requests resource Q Process A holds resource Q Modeling deadlocks modeled by a directed graph (resource graph) Requests and assignments as directed edges Processes and Resources as vertices Cycle in graph means deadlock R S F M Deadlock P B Process B requests resource Q Q A Process A holds resource Q Operating Systems, 2011, Danny Hendler & Amnon Meisels

Different possible runs: an example C Request R Request S Release R Release S Request S Request T Release S Release T Request T Request R Release T Release R Round-robin scheduling: A B C A requests R B requests S C requests T A requests S B requests T C requests R R S T Operating Systems, 2011, Danny Hendler & Amnon Meisels

Different possible runs: an example C Request R Request S Release R Release S Request S Request T Release S Release T Request T Request R Release T Release R An alternative scheduling: A B C A requests R C requests T A requests S C requests R A releases R A releases S R S T Operating Systems, 2011, Danny Hendler & Amnon Meisels

Multiple Resources of each Type Operating Systems, 2011, Danny Hendler & Amnon Meisels

A Directed Cycle But No Deadlock Operating Systems, 2011, Danny Hendler & Amnon Meisels

Resource Allocation Graph With A Deadlock Operating Systems, 2011, Danny Hendler & Amnon Meisels

If graph contains no cycles  no deadlock If graph contains a cycle  Basic Facts 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 possible Operating Systems, 2011, Danny Hendler & Amnon Meisels

Dealing with Deadlocks The dining philosophers problem Deadlocks Modeling deadlocks Dealing with deadlocks Operating Systems, 2011, Danny Hendler & Amnon Meisels

Dealing with Deadlocks Possible Strategies: Prevention structurally negate one of the four necessary conditions Avoidance allocate resources carefully, so as to avoid deadlocks Detection and recovery Do nothing (The “ostrich algorithm’’) deadlocks are rare and hard to tackle... do nothing Example: Unix - process table with 1000 entries and 100 processes each requesting 20 FORK calls... Deadlock. users prefer a rare deadlock over frequent refusal of FORK Operating Systems, 2011, Danny Hendler & Amnon Meisels

Deadlock prevention Attack one of the 4 necessary conditions: 1. Mutual exclusion Minimize exclusive allocation of devices Use spooling: only spooling process requests access (not good for all devices - Tapes; Process Tables); may fill up spools (disk space deadlock)... 2. Hold and Wait Request all resources immediately (before execution) Problem: resources not known in advance, inefficient or to get a new resource, free everything, then request everything again (including new resource) Operating Systems, 2011, Danny Hendler & Amnon Meisels

Attack one of the 4 necessary conditions (cont'd) 3. No preemption Not always possible (e.g., printer) 4. Circular wait condition Allow holding only a single resource (too restrictive) Number resources, allow requests only in ascending order: Request only resources numbered higher than anything currently held Impractical in general Operating Systems, 2011, Danny Hendler & Amnon Meisels

Deadlock Avoidance Safe state: System grants resources only if it is safe basic assumption: maximum resources required by each process is known in advance Safe state: Not deadlocked There is a scheduling that satisfies all possible future requests Operating Systems, 2011, Danny Hendler & Amnon Meisels

Safe states: example B l8 l7 l6 t l5 r s A p q l1 l2 l3 l4 Both have printer l6 Printer t Both have plotter l5 Plotter r Both have both s Unsafe state A p q l1 l2 l3 l4 Printer Plotter Operating Systems, 2011, Danny Hendler & Amnon Meisels

Safe and Unsafe states (single resource) Not deadlocked There is a way to satisfy all possible future requests (a) (b) (c) Fig. 6-9. Three resource allocation states: (a) Safe. (b) Safe. (c) Unsafe. Operating Systems, 2011, Danny Hendler & Amnon Meisels

Banker's Algorithm, Dijkstra 1965 (single resource) Checks whether a state is safe 1. Pick a process that can terminate after fulfilling the rest of its requirements (enough free resources) 2. Free all its resources (simulation) 3. Mark process as terminated 4. If all processes marked, report “safe”, halt 5. If no process can terminate, report “unsafe”, halt 6. Go to step 1 Operating Systems, 2011, Danny Hendler & Amnon Meisels

Multiple resources of each kind Assume n processes and m resource classes Use two matrixes and two vectors: Current allocation matrix Cn x m Request matrix Rn x m (remaining requests) Existing resources vector Em Available resources vector Am Operating Systems, 2011, Danny Hendler & Amnon Meisels

Banker’s Algorithm for multiple resources Look for a row of R whose unmet resource needs are all smaller than or equal to A. If no such row exists, the system will eventually deadlock. Otherwise, assume the process of the row chosen finishes (which will eventually occur). Mark that process as terminated and add the i’th row of C to the A vector Repeat steps 1 and 2 until either all processes are marked terminated, which means safe, or until a deadlock occurs, which means unsafe. Operating Systems, 2011, Danny Hendler & Amnon Meisels

deadlock avoidance – an example with 4 resource types, 5 processes Tape-drives Plotters Scanners CD-ROMs E = (6 3 4 2) A = (1 0 2 0) T P S C T P S C 1 3 A B C D E 1 A 2 B 3 C D E R = C = Is the current state safe? Yes, let’s see why… We let D run until it finishes Operating Systems, 2011, Danny Hendler & Amnon Meisels

deadlock avoidance – an example with 4 resource types, 5 processes Tape-drives Plotters Scanners CD-ROMs E = (6 3 4 2) A = (2 1 2 1) T P S C T P S C 1 3 A B C D E 1 A 2 B 3 C D E R = C = We now let E run until it finishes Next we let A run until it finishes Operating Systems, 2011, Danny Hendler & Amnon Meisels

deadlock avoidance – an example with 4 resource types, 5 processes Tape-drives Plotters Scanners CD-ROMs E = (6 3 4 2) A = (5 1 3 2) T P S C T P S C A 1 B C D E 1 A 2 B 3 C D E R = C = Finally we let B and C run. Operating Systems, 2011, Danny Hendler & Amnon Meisels

Back to original state If B now requests a Scanner, we can allow it. Tape-drives Plotters Scanners CD-ROMs E = (6 3 4 2) A = (1 0 2 0) T P S C T P S C 1 3 A B C D E 1 A 2 B 3 C D E R = C = If B now requests a Scanner, we can allow it. Operating Systems, 2011, Danny Hendler & Amnon Meisels

This is still a safe state… Tape-drives Plotters Scanners CD-ROMs E = (6 3 4 2) A = (1 0 1 0) T P S C T P S C 1 3 A B C D E 1 A 2 B 3 C D E R = C = If E now requests a Scanner, granting the request leads to an unsafe state Operating Systems, 2011, Danny Hendler & Amnon Meisels

This state is unsafe We must not grant E’s request R = C = Tape-drives Plotters Scanners CD-ROMs E = (6 3 4 2) A = (1 0 0 0) T P S C T P S C 1 3 A B C D E 1 A 2 B 3 C D E R = C = We must not grant E’s request Operating Systems, 2011, Danny Hendler & Amnon Meisels

Deadlock Avoidance is not practical Maximum resource request per process is unknown beforehand Resources may disappear New processes (or resources) may appear Operating Systems, 2011, Danny Hendler & Amnon Meisels

Deadlock Detection and Recovery Find if a deadlock exists if it does, find which processes and resources it involes Detection: detect cycles in resource graph Algorithm: DFS + node and arc marking Operating Systems, 2011, Danny Hendler & Amnon Meisels

Find cycles: For each node, N, in the graph, perform the following 5 steps with N as the starting node 1. Initialize L to the empty list and designate all arcs as unmarked 2. Add the current node to the end of L and check if the node appears twice in L. If it does, the graph contains a cycle, terminate. 3. If there are any unmarked arcs from the given node, go to 4., if not go to 5. 4. Pick an unmarked outgoing arc and mark it. Follow it to the new current node and go to 2. 5. We have reached a deadend. Go back to the previous node, make it the current node and go to 3. If all arcs are marked and the node is the initial node, there are no cycles in the graph, terminate Operating Systems, 2011, Danny Hendler & Amnon Meisels

Detection - extract a cycle 1. Process A holds R and requests S 2. Process B holds nothing and requests T 3. Process C holds nothing and requests S 4. Process D holds U and requests S and T 5. Process E holds T and requests V 6. Process F holds W and requests S 7. Process G holds V and requests U Operating Systems, 2011, Danny Hendler & Amnon Meisels

When should the system check for deadlock ? Whenever a request is made - too expensive every k time units... whenever CPU utilization drops bellow some threshold (indication of a possible deadlock..) Operating Systems, 2011, Danny Hendler & Amnon Meisels

Recovery Preemption - possible in some rare cases temporarily take a resource away from its current owner Rollback - possible with checkpointing Keep former states of processes (checkpoints) to enable release of resources and going back Killing a process - easy way out, may cause problems in some cases, depending on process being rerunable… Bottom line: hard to recover from deadlock, avoid it Operating Systems, 2011, Danny Hendler & Amnon Meisels

Factors Determining Process to “Kill” What the priority of the process is How long the process has computed, and how much longer the program will compute before completing its designated task How many and what type of resources the process has used (for example, whether the resources are simple or preempt) How many more resources the process needs in order to complete How many processes will need to be terminated Whether the process is interactive or batch Operating Systems, 2011, Danny Hendler & Amnon Meisels

Example - deadlocks in DBMSs For database records that need locking first and then updating (two-phase locking) Deadlocks occur frequently because records are dynamically requested by competing processes DBMSs, therefore, need to employ deadlock detection and recovery procedures Recovery is possible - transactions are “checkpointed” - release everything and restart Operating Systems, 2011, Danny Hendler & Amnon Meisels

Deadlock handling – combined approach Different strategies can be used for different classes of resources Simplistic example - four classes Internal resources – used by the system – PCB… Central memory – for users’ processes Assignable devices – Tape drives, etc. Swap space – on disk for user processes Operating Systems, 2011, Danny Hendler & Amnon Meisels

Combined approach (cntd.) Internal Resources – use Prevention through resource ordering, no selection among pending processes Central memory – use prevention through Preemption, a process can always be swapped-out Assignable devices – if device-requirement information is available, use Avoidance Swap space – use Preallocation, since maximal storage requirements are known in advance Operating Systems, 2011, Danny Hendler & Amnon Meisels

Additional deadlock issues Deadlocks may occur with respect to actions of processes, not resources - waiting for semaphores Starvation can result from a bad allocation policy (such as smallest-file-first, for printing) and for the “starved” process will be equivalent to a deadlock (cannot finish running) Summary of deadlock treatment: Ignore problem Detect and recover Avoid (be only in safe states) Prevent by using an allocation policy or conditions Operating Systems, 2011, Danny Hendler & Amnon Meisels

The Situation in Practice Most OSs in use, and especially NT, Solaris …, ignore deadlock or do not detect it Tools to kill processes but usually without loss of data In Windows NT there is a system call WaitForMultipleObjects that requests all resources at once System provides all resources, if free There is no lock of resources if only few are free Prevents Hold & Wait, but difficult to implement! Operating Systems, 2011, Danny Hendler & Amnon Meisels