Avishai Wool lecture 5 - 1 Introduction to Systems Programming Lecture 5 Deadlocks.

Slides:



Advertisements
Similar presentations
Advanced Operating Systems
Advertisements

Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Deadlocks Today Resources & deadlocks Dealing with deadlocks Other issues Next Time Memory management.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Ceng Operating Systems Chapter 2.4 : Deadlocks Process concept  Process scheduling  Interprocess communication  Deadlocks Threads.
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
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.
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
1 Concurrency: Deadlock and Starvation Chapter 6.
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** 
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.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.
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 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings.
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.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
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.
Deadlock Management.
Synchronization.
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
DPNM Lab. Dept. of CSE, POSTECH
Review: Readers-Writers Problem
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.
Presentation transcript:

Avishai Wool lecture Introduction to Systems Programming Lecture 5 Deadlocks

Avishai Wool lecture Example of Deadlock Dining Philosophers problem “pick left, pick right, eat” algorithm With some bad luck: –All “pick left”, –All stuck on “pick right” This is called Deadlock

Avishai Wool lecture When Do Deadlocks Occur Deadlocks occur when … –processes are granted exclusive access to devices –we refer to these devices generally as resources

Avishai Wool lecture Resources Examples of computer resources –Printers –Tape drives –Entries in OS tables (like PCB entries) –Memory –Database records Processes need access to resources in reasonable order Access restricted via: critical sections / mutex / semaphore

Avishai Wool lecture Resources Usage Pattern Sequence of events required to use a resource 1.request the resource 2.use the resource 3.release the resource Must wait if request is denied –requesting process may be blocked –may fail with error code

Avishai Wool lecture 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. Usually the event is the release of a currently held resource. None of the processes can … –run –release resources –be awakened

Avishai Wool lecture Four Conditions for Deadlock 1.Mutual exclusion condition each resource assigned to 1 process or is available 2.Hold-and-wait condition process holding resources can request additional resources 3.No preemption condition previously granted resources cannot forcibly taken away 4.Circular wait condition must be a circular chain of 2 or more processes each is waiting for resource held by next member of the chain

Avishai Wool lecture Strategies for Dealing with Deadlock 1.Just ignore the problem altogether. 2.Detection and recovery. 3.Avoidance careful resource allocation. 4.Prevention negating one of the four necessary conditions.

Avishai Wool lecture Deadlock Detection and Recovery

Avishai Wool lecture Modeling Deadlock with Graphs Process == circle, Resource == square –resource R assigned to process A –process B is requesting/waiting for resource S –process C and D are in deadlock over resources T and U

Avishai Wool lecture A B C Using Resource Allocation Graph

Avishai Wool lecture How deadlock can be avoided (o) (p) (q) Scheduling B is delayed

Avishai Wool lecture Detection with One Resource of Each Type Note the resource ownership and requests A cycle can be found within the graph, denoting deadlock

Avishai Wool lecture Detection with Multiple Resource of Each Type Data structures needed by deadlock detection algorithm

Avishai Wool lecture Deadlock Detection: overview Algorithm knows resources, requests Needs to determine whether there exists a scheduling order such that all requests can be honored. Algorithm is not (necessarily) responsible for scheduling.

Avishai Wool lecture Deadlock Detection Algorithm For vectors X,Y we say X ≤ Y if X i ≤ Y i for all i. 1.Set all processes as “unmarked” Loop: 2.Find unmarked process i with R i* ≤ A // i can run 3.If found: A  A + C i* ; “mark” process i; // assume i has run, make its resources available 4.Else: If all processes are marked: Success 5. Else: System is deadlocked

Avishai Wool lecture Example of Deadlock Detection Algorithm After process 3 runs: A = ( 2, 2, 2, 0) After process 2 runs: A = ( 4, 2, 2, 1) X X

Avishai Wool lecture Recovery from Deadlock (1) Recovery through preemption –take a resource from some other process –depends on nature of the resource Recovery through rollback –checkpoint a process periodically –use this saved state –restart the process if it is found deadlocked

Avishai Wool lecture Recovery from Deadlock (2) Recovery through killing processes –crudest but simplest way to break a deadlock –kill one of the processes in the deadlock cycle –the other processes get its resources –choose process that can be rerun from the beginning None of the deadlock recovery options is very attractive.

Avishai Wool lecture Deadlock Avoidance

Avishai Wool lecture Deadlock Avoidance Can we make decisions one at a time and always avoid deadlock? Need some advance knowledge: –Each process has to “declare”, in advance, what resources, and what is the maximum number of resource units, it will need

Avishai Wool lecture Safe and Unsafe States The “State” of the system includes –How many resources are allocated to each process –What is the maximum number of resources a process could request –How many are available A state is Safe if –It is not deadlocked –There exists a scheduling order in which all processes run to completion.

Avishai Wool lecture The state in (a) is safe (a) (b) (c) (d) (e) Only one resource, 10 copies exist We could schedule B After B terminates we could Schedule C

Avishai Wool lecture The state in (b) is not safe (a) (b) (c) (d) Schedule B

Avishai Wool lecture Unsafe != Deadlocked If state is safe  deadlock can be avoided. If state is unsafe  not clear –We are making “worst case” assumptions, processes could use less than their maximum

Avishai Wool lecture Banker’s Algorithm (Dijkstra, 1965) When process requests resource X, 1.Assume the process gets X Loop: 2.Find unmarked process i with R i* ≤ A // i can run 3.If found: A  A + C i* ; mark process i as terminated. // assume i has run, make its resources available 4.Else: System is deadlocked, giving the resource was unsafe

Avishai Wool lecture The Banker's Algorithm for a Single Resource Three resource allocation states –safe –unsafe (a) (b) (c)

Avishai Wool lecture Is Deadlock Avoidance Practical? Banker’s algorithm needs information about future needs (max values for each resource). In general OS does not have this information. Number of processes varies. Resources can “disappear”.

Avishai Wool lecture Deadlock Prevention

Avishai Wool lecture Idea: invalidate one of the four conditions for deadlock 1.Mutual exclusion condition 2.Hold-and-wait condition 3.No preemption condition 4.Circular wait condition

Avishai Wool lecture Attacking the Mutual Exclusion Condition Some devices (such as printer) can be spooled –only the printer daemon uses printer resource –thus deadlock for printer eliminated Not all devices can be spooled Principle: –avoid assigning resource when not absolutely necessary –as few processes as possible actually claim the resource

Avishai Wool lecture Attacking the Hold and Wait Condition Require processes to request resources before starting –a process never has to wait for what it needs Problems –may not know required resources at start of run –also ties up resources other processes could be using Variation: before requesting a new resource, –process must give up all resources –then request all immediately needed

Avishai Wool lecture Attacking the No Preemption Condition In general this is not a viable option Consider a process given the printer –halfway through its job –now forcibly take away printer –!!??

Avishai Wool lecture Attacking the Circular Wait Condition Every resource has a unique number A process must request resources in increasing number order

Avishai Wool lecture Requesting in Order Prevents Deadlock 2 resources (i > j), 2 processes A, B Deadlock can only occur if : –A holds i and requests j, –B holds j and requests i. But if they requested resources in order, after A holds i it is not allowed to request j! It should have asked for j earlier (and become blocked).

Avishai Wool lecture Summary of deadlock prevention All these options are very restrictive, and not very practical

Avishai Wool lecture The Ostrich Algorithm Reasonable if –deadlocks occur very rarely. –cost of prevention is high. UNIX and Windows takes this approach. It is a trade off between –convenience –correctness General-purpose OSs do not do much towards deadlock detection, recovery, avoidance, or prevention.

Avishai Wool lecture A Database example A database maintains many tables. A transaction might update records in many tables. A transaction has to be atomic: –either all records are updated, or none are.

Avishai Wool lecture Example transfer $1000 from account A to account B Read A A  A – 1000 Write A Read B B  B Write B

Avishai Wool lecture Need to Avoid Inconsistencies Lock records before accessing to them –otherwise race conditions possible. Database software (Oracle, Sybase, IBM DB2…) provides record locking mechanism. Deadlock is certainly possible.

Avishai Wool lecture Two-Phase Locking Phase One –A transaction locks all records it needs –(no real work done in phase one) Phase Two –performing updates –Issuing “commit” –releasing locks

Avishai Wool lecture Properties of Two-phase locking Deadlock detection by a per-transaction timeout. –Has occasional “false positives”, when disk response time is bad. Deadlock recovery by aborting transactions –An aborted transaction releases all its locks

Avishai Wool lecture Why can this work in DB? Unlike general OS, a transaction knows which records it will need in advance. A transaction can be aborted (rolled back) safely. Rollback capability needed for other reasons –Power outage, program crash, may leave DB inconsistent.

Avishai Wool lecture Concepts for review Deadlock Conditions for deadlock Graph models of deadlock Deadlock detection algorithms Deadlock avoidance: Safe/Unsafe states Banker’s algorithm Deadlock prevention Spooling Database transaction 2-phase locking