Deadlocks Today Resources & deadlocks Dealing with deadlocks Other issues Next Time Memory management.

Slides:



Advertisements
Similar presentations
Advanced Operating Systems
Advertisements

Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Chapter 6 Concurrency: Deadlock and Starvation
Ceng Operating Systems Chapter 2.4 : Deadlocks Process concept  Process scheduling  Interprocess communication  Deadlocks Threads.
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
5/25/2015Page 1 Deadlock Management B. Ramamurthy.
DEADLOCK.
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.
DPNM Lab. Dept. of CSE, POSTECH
Deadlock Chapter 3 R1 R2 P2P1 Allocated Requested.
Deadlocks Tore Larsen With slides from T. Plagemann, C. Griwodz, K. Li, A. Tanenbaum and M. van Steen.
Mehdi Naghavi Spring 1386 Operating Systems Mehdi Naghavi Spring 1386.
Chapter 3: Deadlocks Chapter 3 2 CMPS 111, UC Santa Cruz Overview  Resources  Why do deadlocks occur?  Dealing with deadlocks Ignoring them: ostrich.
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
Deadlock CSCI 444/544 Operating Systems Fall 2008.
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
CS450/550 Deadlocks.1 Adapted from MOS2E UC. Colorado Springs CS450/550 Operating Systems Lecture 3 Deadlocks Palden Lama Department of Computer Science.
Chapter 3 Chapter 3: Deadlocks Chapter 3 2 CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt) Overview Resources Why do.
Deadlock Chapter 3 Thursday, February 22, Today’s Schedule Assignment #4 from Chapter 3 posted Deadlock - Chapter 3  Skip multiple resources (3.4.2.
1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.
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.
Chapter 3: Deadlocks. CMPS 111, Fall Overview ✦ Resources ✦ Why do deadlocks occur? ✦ Dealing with deadlocks Ignoring them: ostrich algorithm Detecting.
Overview Resources Why do deadlocks occur? Dealing with deadlocks Ignoring them: ostrich algorithm Detecting & recovering from deadlock Avoiding deadlock.
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.
1 Pertemuan 10 Deadlock (lanjutan) Matakuliah: T0316/sistem Operasi Tahun: 2005 Versi/Revisi: 5.
Operating Systems 软件学院 高海昌 Operating Systems Gao Haichang, Software School, Xidian University 22 Contents  1. Introduction** 
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.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
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.
Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money.
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.
Sistem Operasi IKH311 Deadlock. 2 Resources Examples of computer resources printers tape drives tables Processes need access to resources in reasonable.
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.
Operating Systems Chapter 3: Deadlocks
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Deadlock Management.
Chapter 6 : Deadlocks What is a Deadlock?
Deadlocks References text: Tanenbaum ch.3.
Lecture 19: Deadlock: Conditions, Detection and Avoidance
Deadlocks References text: Tanenbaum ch.3.
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
CS 333 Introduction to Operating Systems Class 7 - Deadlock
DPNM Lab. Dept. of CSE, POSTECH
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Lecture 19: Deadlock: Conditions, Detection and Avoidance
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:

Deadlocks Today Resources & deadlocks Dealing with deadlocks Other issues Next Time Memory management

EECS 343 Operating Systems Northwestern University 2 System model System – a collection of resources to be shared among a set of processes Resources partitioned in types, each with multiple instances (printers, files, memory,…) Resources can be –Preemptable - can be taken away from process w/o ill effects e.g. memory –Nonpreemptable - process will fail if resource was taken away e.g. CD recorder A request for resource type R can be satisfied by any instance of the type

3 System model A process must request a resource before using it & release it once done ( open/close, malloc/free, …) Sequence of events to use a resource 1.request it – if not granted then block or return error down(semaphore) 2.use it 3.release it up(semaphore) Suppose –Process A holds resource R & requests S –Process B holds resources S and requests R –A & B are now blocked EECS 343 Operating Systems Northwestern University AB R S

4 Introduction to deadlocks Formal definition A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause None of the processes can … –run –release resources –be awakened Assumptions –Processes are single threaded –There are no interrupts possible to wake up a blocked process A “cute” example “When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up until the other has gone.” An actual law passed by the Kansas legislature … EECS 343 Operating Systems Northwestern University

5 Conditions for deadlock 1. Mutual exclusion - Each resource assigned to 1 process or available 2. Hold and wait - A process holding resources can request others 3. No preemption - Previously granted resources cannot forcibly be taken away 4. Circular wait – A circular chain of 2+ processes, each waiting for resource held by next one All conditions must hold for a deadlock to occur. Each of the 1-3 conditions is associated with a policy the system can or not have; break one condition → no deadlock EECS 343 Operating Systems Northwestern University

6 Deadlock modeling Modeled with directed graphs –Process B is requesting/waiting for resource S –Resource R assigned to process A –Process C & D in deadlock over resources T & U You can generalize it to multiple resource instances per class TUCDA R A R Assignment B S Request EECS 343 Operating Systems Northwestern University

7 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, maybe a deadlock. CBA R1R2 R4 A B C D R1 R2 No deadlock here Deadlock here EECS 343 Operating Systems Northwestern University

8 Deadlock modeling A Request R Request S Release R Release S B Request S Request T Release S Release T C Request T Request R Release T Release R 1.A requests R 2.B requests S 3.C requests T 4.A requests S 5.B requests T 6.C requests R …. deadlock 1.A requests R 2.C requests T 3.A requests S 4.C requests R 5.A releases R 6.A releases S.... no deadlock A RS BC T Requests and releases of each process Two possible orderings EECS 343 Operating Systems Northwestern University Clearly, the ordering of operations plays a role

9 Dealing with deadlocks Possible strategies Ignore the problem altogether – ostrich “algorithm” Detection and recovery – do not stop it; let it happen, detect it and recover from it Dynamic avoidance - careful resource allocation Prevention - negating one of the four necessary conditions EECS 343 Operating Systems Northwestern University

10 The ostrich algorithm Pretend there is no problem Reasonable if –deadlocks occur very rarely –cost of prevention is high UNIX’s & Windows’ approach A clear trade off between –convenience –correctness EECS 343 Operating Systems Northwestern University

11 Deadlock detection – single instance How, when & what Simplest case 1.L ← empty all arcs set as unmarked 2.For each node N /* depth-first search */ 2.1.Add N to L & check if N in L twice there’s a deadlock; exit 2.2.Pick one arc at random, mark it & follow it to next current node 3.At end, if no arc no deadlock Arcs: A→S, A←R, B→T, C→S D→S, D←T, E→V, E←T F→S, F←W, G→V, G←V L:[R], L:[R,A], L:[R,A,S] L:[B], L:[B,T], L:[B,T,E], … A F D G E B R S W U T V C EECS 343 Operating Systems Northwestern University

12 Detection - multiple instances n processes, m classes of resources E – vector of existing resources A – vector of available resources C – matrix of currently allocated resources R – request matrix C ij – P i holds C ij instances of resource class j R ij – P i wants C ij instances of resource class j Invariant – Σ i C ij + A j = E j ( Currently allocated + available = existing ) i.e. all resources are either allocated or available Algorithm: All processes unmarked 1.Look for unmarked process P i for which R i ≤ A 2.If found, add C i. to A, mark the process and go to 1 3.If not, exit All unmarked processes, if any, are deadlock Idea: See if there’s any process that can be run to completion with available resources, mark it and free its resources … EECS 343 Operating Systems Northwestern University

13 Detection (existing) (available) E = ( ) A = ( ) C = R = Three processes and 4 resource types After running process 3 A = ( ) Now you can run process 2 A = ( ) Algorithm: All processes unmarked 1.Look for unmarked process P i for which R i ≤ A 2.If found, add C i. to A, mark the process and go to 1 3.If not, exit All unmarked processes, if any, are deadlock Idea: See if there’s any process that can be run to completion with available resources, mark it and free its resources … EECS 343 Operating Systems Northwestern University What process 1 has What process 1 needs

14 When to check & what to do When to try –Every time a resource is requested –Every fixed period of times or when CPU utilization drops What to do then - recovery –Through preemption depends on nature of the resource –Through rollback Need to checkpoint processes periodically –By killing a process Crudest but simplest way to break a deadlock Kill one in or not in the deadlock cycle EECS 343 Operating Systems Northwestern University

15 Deadlock avoidance Dynamically make sure not to get into a deadlock Two process resource trajectories printer I1I1 I2I2 I3I3 I4I4 qp I5I5 I6I6 I7I7 I8I8 B A * u (Both processes done) printer plotter s r t impossible deadlock Your only option here is to run A up to I 4 impossible EECS 343 Operating Systems Northwestern University

16 Safe and unsafe states Safe if –There is no deadlock –There is some scheduling order by which all processes can run to completion Un-safe is not deadlock – just no guarantee Example with one resource (10 instances of it) A39 B24 C27 Free: 3 A39 B24 C27 A requests and is granted another instance Safe Unsafe HasNeeds HasNeeds In retrospect, A’s request should not have been granted EECS 343 Operating Systems Northwestern University A39 B44 C27 Free: 1 HasNeeds A39 B0- C27 Free: 5 HasNeeds A39 B0- C77 Free: 0 HasNeeds A39 B0- C0- Free: 7 HasNeeds A49 B24 C27 Free: 2 HasNeeds A49 B44 C27 Free: 0 Has Needs A49 B0- C27 Free: 4 HasNeeds

17 Banker's algorithm Considers –Each request as it occurs –Sees if granting it leads to a safe state i.e. there are enough resources to satisfy one customer With multiple resources 1.Look for a row R i. ≤ A, if none the system will eventually deadlock 2.If found, mark P i and add C i. to A 3.Repeat until processes are terminated or a deadlock occurs Very cute, but mostly useless –Most processes don’t know in advance what they need –The lists of processes and resources are not static –Processes may depend on each other EECS 343 Operating Systems Northwestern University

18 Deadlock prevention Avoidance is pretty hard or impossible Can we break one of the condition? –Mutual exclusion –Hold & wait –No preemption Not a viable option How can you preempt a printer? –Circular wait EECS 343 Operating Systems Northwestern University

19 Attacking mutual exclusion Some devices can be spooled (printer) –Only the printer daemon uses printer resource –Thus deadlock for printer eliminated But not all devices can be spooled – process table? Principle: –Assigning resource only when absolutely necessary –Reduce number of processes that may claim the resource EECS 343 Operating Systems Northwestern University

20 Attacking hold & wait Processes request all resources at start (wait) –Process never has to wait for what it needs But –May not know required resources at start –It ties up resources others could be using Variation (hold) –Process must release all resources to request a new one EECS 343 Operating Systems Northwestern University

21 Impose total order on resources Processes request resources in order If all processes follow order, no circular wait occurs Deadlock if i → A → j & j → B → i If i < j then A → j … Process cannot request resource lower than what it’s holding Advantage - Simple Disadvantage - Arbitrary ordering Attacking circular wait A ij B EECS 343 Operating Systems Northwestern University

22 Related issues Two-phase locking – gather all locks, work & free all –If you cannot get all, drop all you have and start again Non-resource deadlocks –Each is waiting for the other to do some task –E.g. communication deadlocks: A sends a request and blocks until B replies, message gets lost! Timeout! Starvation –Algorithm to allocate a resource –SJF – consider allocation of a printer Great for multiple short jobs in a system May cause long job to be postponed indefinitely –even though not blocked –Solution: FIFO EECS 343 Operating Systems Northwestern University

23 Next time … We have discussed sharing CPU to improve utilization and turnaround time For that to happen we also need to share memory We’ll start with memory organization and basic management techniques (e.g. paging) Before moving to memory virtualization … … of course, all this after the midterm! EECS 343 Operating Systems Northwestern University