Operating Systems (OS)

Slides:



Advertisements
Similar presentations
Advanced Operating Systems
Advertisements

Deadlock Prevention, Avoidance, and Detection
Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
1 Deadlock Solutions: Avoidance, Detection, and Recovery CS 241 March 30, 2012 University of Illinois.
Deadlock Prevention, Avoidance, and Detection.  The Deadlock problem The Deadlock problem  Conditions for deadlocks Conditions for deadlocks  Graph-theoretic.
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
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.
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.
The Banker’s Algorithm for A Single Resource
CS 450 OPERATING SYSTEMS DEADLOCKS Manju Muralidharan Priya.
The ‘deadlock’ conditions Reviewing some key points concerning the potential for ‘deadlock’ in an operating system.
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Deadlock Detection with One Resource of Each Type (1)
Chapter 6 Deadlocks Resources Introduction Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
CS 342 – Operating Systems Spring 2003 © Ibrahim Korpeoglu Bilkent University1 Deadlocks CS 342 – Operating Systems Ibrahim Korpeoglu Bilkent University.
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.
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.
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.
Chapter 7 – Deadlock (Pgs 283 – 306). Overview  When a set of processes is prevented from completing because each is preventing the other from accessing.
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.
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.
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.
Deadlock cs550 Operating Systems David Monismith.
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
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.
Deadlocks.  Deadlocks: Occurs when threads are waiting for resources with circular dependencies Often involve nonpreemptable resources, which cannot.
Deadlocks Copyright ©: University of Illinois CS 241 Staff1.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Deadlock.
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.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
ITEC 202 Operating Systems
Chapter 6 : Deadlocks What is a Deadlock?
Deadlocks References text: Tanenbaum ch.3.
Chapter 7 Deadlock.
Lecture 19: Deadlock: Conditions, Detection and Avoidance
Deadlocks References text: Tanenbaum ch.3.
Deadlocks Definition A set of processes is in a Deadlock state when every process in the set is waiting for an event that can only be caused by another.
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.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Presentation transcript:

Operating Systems (OS) Deadlock

Outline What is deadlock? Strategies for handling deadlock Vad är deadlock? Nån som råkat ut för det?

Consider the following example: What is deadlock? Consider the following example: Legend: A S1 Process B Resource S2 A owns B A B A wants B A B Controlling: Om R controls P: Då håller R P (P är i tillståndet waiting och väntar på R) Om P controls R: Då äger P R, dvs den har tillgång till resursen. “The arrow points at the controlling node”

Formal definition of Deadlock 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 Vad betyder då det här?

Resources A hardware device or a piece of data Two types of resources Preemptable Non-preemptable Deadlock can only occur with non-preemptable resources. The following show how a resource is used: Claim the resource Use the resource Release the resource Preemptable resource: It is possible to take the resource from the process owning it without bad sideeffects One example: Memory [sometimes] Can be swapped to disk

Conditions for deadlock Mutual exclusion A resource is either assigned to 1 process or available Hold and wait condition Possible to own one resource while claiming another No preemption condition granted resources cannot taken away by force Circular wait condition Each waiting process is waiting for the next in the chain All four conditions must be met in order for a deadlock to occur Det här är VILLKOR för deadlock inte strategier...

Dining philosophers problem 1 table 5 forks A philosopher can either think or eat Eating procedure: Pick up left fork Pick up right fork Eat Return both forks Possible solutions: Buy more forks, how many?? Solution 1…. Då kan man nysta upp det Grab fork by force (Deadlock detection) Make some protocol, e.g., return if locked (Deadlock avoidance and Deadlock prevention) Shoot philosopher (Deadlock detection) Don’t care at all, who cares about philosophers…let them starve (Ostrich algorithm) How can we deal with the problem?

Deadlock management strategies The Ostrich algorithm Deadlock detection Deadlock avoidance Deadlock prevention

The ostrich algorithm Stick your head in the sand and The easiest way to deal with the problem: Stick your head in the sand and pretend that there is no problem How is it possible to use such an algorithm? Other errors are much more frequent To not add limitations to the operating system Give the fork example: Consider 10 programs, each program can create up to 12 processes, however it is not likely that is occurs at the same time. Say that unix can handle 100 processes max. No restrictions are used because the user might accept a crash every now and then instead of not beeing able to run all the programs. There is a tradeoff between convenience and correctness The ostrich algorithm is both Windows and UNIX approved!! 

Deadlock Detection No limitations to resource allocations Use algorithm to detect possible deadlock Two cases: One resource of each type Multiple resources of each type Resolve any deadlock found When should algorithm be run? When resource is claimed Periodically, every n time unit During idle periods Här accepterar vi deadlock, men vi dtekterar och hanterar det när det väl uppstår Praktiskt att göra när man är idle.

Deadlock Detection - One resource of each type Make a resource graph Locate any cycles Break the cycles found Exercise: PA owns R and wants S PB wants T PC owns S PD owns U and wants S and T PE owns T and wants V PF owns W and wants S PG owns V and wants U Is there deadlock in the system? Process Resource A owns B A B A wants B Studenterna gör övningen! A B “The arrow points at the controlling node”

Deadlock Detection - One resource of each type YES!!! Hur kan man göra en algoritm som löser detta?

Deadlock Detection - One resource of each type Algorithm for finding cycle: For every node N in the graph, perform the following steps using N as starting node. Initiate L as an empty set and unmark all arrows If current node is NOT in the set, add current node to set, else deadlock is detected and the algorithm is terminated If there are unmarked arrows from the current node go to 5 else go to 6 Randomly select one arrow and follow it.Set the new node as the current node. Go to 3 We have reached a dead end. Backtrack to previous node. If previous node is starting node go to 1 using a new starting node. If not, go to 4

Deadlock Detection - One resource of each type N=PB L={} =>L={PB} =>L={PB, T} =>L={PB, T, PE} =>L={PB, T, PE, V} =>L={PB, T, PE, V, PG} =>L={PB, T, PE, V, PG, U} =>L={PB, T, PE, V, PG, U, PD} =>L={PB, T, PE, V, PG, U, PD ,T} Deadlock

Deadlock Detection - Many resources of each type Solved using Matrices A vector of existing resources of type ex A vector of available resources of type ax A matrix containing the # of resources of Type n claimed by process m. A matrix containing the # of resources of Type n requested by process m. We see that:

Deadlock Detection - Many resources of each type For two vectors, X and Y iff for For example: Vad betyder detta? X is less or equal to Y if all elements in x are less or equal to the corresponding element in y.

Deadlock Detection - Many resources of each type The detection algorithm is applied periodically Find an unmarked process Pi for which Ri A. If such a process is found, add Ci to A. Mark the process and go to step 1. If no such process is found, terminate. All unmarked processes are deadlocked In this case C is the previously claimed resources, R is the resources claimed since last invocation. If Pi can continue, it should (as it is now) be able to terminate and thus release all its resources (adding ci to A) The Algorithm DOES NOT look into the future….it looks at the current state. How on earth does this algorithm work?!?!?

Deadlock Detection - Many resources of each type Exercise: 3 2 1 Is there deadlock in the system?? Kommer de att köra i denna ordning, nej kanske inte, men om det finns ett sätt att slutföra allt så har vi inte deadlock än. NO!!!

Deadlock Detection How to recover from deadlock Preemption Process rollback Kill one of the deadlocked process’s Preemption – To take the resource from the process (laser printer, put pages in pile, and then back). This is very hard, in most cases impossible, Rollback – The OS takes continuous snapshots (a process state and resource needs are recorded in a file) of the system, the system is rolled back (to a state where the process doe not have a resource) to the last snapshot. (Databaser funkar så) Kill a process that can be rerun with no ill effects (a compilation) a database update can not be rerun (=> 2 updates)

Deadlock Avoidance Avoids deadlock by cautious allocations Safe and unsafe states An allocation can only be granted if it puts the system in a safe state. Vi nekar en P att ta en resurs om vi kommer att hamna i DL om vi tillåter den ta resursen

Deadlock Avoidance - Safe and unsafe states

Deadlock Avoidance - Safe and unsafe states Algorithms to determine the state Based on the E,A,C and R matrices. Example: A total of 10 resources, with the following allocations: Proc Has Max A 3 9 B 2 4 C 7 3 1 SAFE!!! 2 Looks into the future and predicts the worst case scenario… Free 3 5 7 10 Is the system in a safe or unsafe state??

Deadlock Avoidance -Bankers Algorithm Made by Dijkstra Originating from loan allocations in a bank Examines if an allocation leads to a safe or unsafe state Is run when resource is requested Two cases One type of resource Multiple types of resources

Deadlock Avoidance -Bankers Algorithm Example using one type of resource: 7 2 C 4 B 9 3 A Max Has Proc Free 3 Initial state 7 2 C 4 B 9 A Max Has Proc Free 2 A requests 1 NOT GRANTED! SAFE! UNSAFE! 7 2 C 4 3 B 9 A Max Has Proc Free 2 B requests 1 7 3 C 4 B 9 A Max Has Proc Free 1 C requests 1 A Request one, B (4 free) Neither C or A can run, UNSAFE B Request one, B (5 free) C (7 free) A (10 free) SAFE C Request one, B(4 free) C (7 free) A (10 free) SAFE Problems: Not east to know the max, bad utilization (Always assuming worst case) GRANTED! GRANTED! SAFE! SAFE!

Deadlock Prevention Very difficult to achieve Attack one of the conditions for deadlock Attack Mutual Exclusion Attack Hold and Wait Attack No preemption Attack Circular wait Mutual Exclusion: Spooling (daemon pretending to be the resource, serialization by daemon) Printer Not all resources can be spooled Hold and Wait: Initially request all resources (2 phase locking) Low utilization, should Word allocate the printer from the very start??? OR first release its resources then request them+the new Preemption: Allow taking resources from a process by force. Not easily done, which one to take?? Circular wait: Always allocate resources in a predefined order, often used with semaphores…

How would you choose to do? In general purpose OS? In real-time systems (OSEK)? Why?

Dining philosophers revisited Bottom line: Deadlock is rarely controlled by OS Therefore: You as a programmer needs to handle it One easy rule: Always take semaphores in certain order! Let one philosopher take right fork first! 5-1 1 5 1-2 4-5 Not easy to achieve in complex systems developed by different companies… 2 4 3 2-3 3-4

Summary What is deadlock? Strategies for handling deadlock 4 conditions Strategies for handling deadlock 4 strategies Vad är deadlock? Nån som råkat ut för det?