Operating Systems, 152 Practical Session 8 Deadlocks 1.

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Operating Systems, 122 Practical Session 8 Deadlocks 1.
Lecture 7: Deadlock     Necessary Conditions for Deadlock     Deadlock Prevention - Havender's Linear Ordering Deadlock Avoidance Deadlock Detection &
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Chapter 7: Deadlocks.
Chapter 7: Deadlocks (Continuation). 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks.
Resource Allocation, Deadlock and Banker’s Algorithm Supplementary Notes Dr. R. D. Kent Last modified: Dec. 11, 2006.
Ceng Operating Systems Chapter 2.4 : Deadlocks Process concept  Process scheduling  Interprocess communication  Deadlocks Threads.
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.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
Avishai Wool lecture Introduction to Systems Programming Lecture 5 Deadlocks.
Deadlocks CS 3100 Deadlocks1. The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held by another.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks.
1 Wednesday, June 28, 2006 Command, n.: Statement presented by a human and accepted by a computer in such a manner as to make the human feel that he is.
Deadlocks. 2 System Model There are non-shared computer resources –Maybe more than one instance –Printers, Semaphores, Tape drives, CPU Processes need.
Deadlocks. 2 Deadlock Avoidance If we have future information –Max resource requirement of each process before they execute Can we guarantee that deadlocks.
03/07/2007CSCI 315 Operating Systems Design1 Deadlock Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Deadlock Detection with One Resource of Each Type (1)
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
Chapter 6 Concurrency: Deadlock and Starvation
03/03/2004CSCI 315 Operating Systems Design1 Deadlock Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
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.
7: Deadlocks1 DEADLOCKS EXAMPLES: "It takes money to make money". You can't get a job without experience; you can't get experience without a job. BACKGROUND:
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Deadlocks.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
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 8: Deadlocks Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock.
 The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock Avoidance  Deadlock.
Chapter 7 Deadlocks. 7.1 Introduction Deadlock Common approaches to deal with deadlock – Prevention – Avoidance – Detection and recovery.
Deadlocks System Model RAG Deadlock Characterization
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks System Model Deadlock Characterization Methods.
Lecture 12 Handling Deadlock – Prevention, avoidance and detection.
Chapter 8 Deadlocks. Objective System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
CS307 Operating Systems Deadlocks Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2012.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 6 Deadlocks Slide 1 Chapter 6 Deadlocks.
Deadlocks Introduction to Operating Systems: Module 7.
Deadlock A deadlock is a situation wherein two or more competing actions are waiting for the other to finish, and thus neither ever does. Example : “When.
Deadlock CS Introduction to Operating Systems.
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);
Deadlock. Examples You can't get a job without experience; you can't get experience without a job. A set of blocked processes each holding a resource.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
OPERATING SYSTEM CONCEPTS AND PRACTISE
Chapter 7: Deadlocks.
Operating Systems, Summer Semester 2011
Operating Systems, Winter Semester 2011
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Concurrency: Deadlock and Starvation
Chapter 6 : Deadlocks What is a Deadlock?
Operating System: DEADLOCKS
OPERATING SYSTEMS DEADLOCKS
Process Deadlocks.
Practical Session 12 Deadlocks
Outline Deadlocks, dead lock prevention, avoidance.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Deadlocks Peng Lu In a multiprogramming environment, several processes may compete for a finite number of resources. A process requests resources; if the.
Chapter 7: Deadlocks.
OPERATING SYSTEMS DEADLOCKS.
DEADLOCKS.
CSE 380 Lecture Note 12 Insup Lee
Chapter 7: Deadlocks.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Presentation transcript:

Operating Systems, 152 Practical Session 8 Deadlocks 1

The ultimate form of starvation. 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.

Conditions for Deadlock 1.Mutual exclusion – resources may be used by only one process 2.Hold and wait – a process can request a resource while holding one 3.No preemption - only the process holding a resource can release it 4.Circular wait - two or more processes each waiting for a resource held by the other

Solving the Deadlock Problem 1.The Ostrich ‘Algorithm’ – Ignore the problem 2.Deadlock Detection & Recovery – Detect a deadlock by finding a cyclic graph of processes and resources, and recover 3.Deadlock Avoidance – Detect safe and unsafe states; Banker’s algorithm 4.Deadlock Prevention – Ensure that at least one of the four conditions for a deadlock is never satisfied

Question 1 Assume resources are ordered as: R 1, R 2,...R n Prove formally that if processes always request resources by order (i.e. if a process requests R k after R j then k>j) then a deadlock will not occur. For simplicity assume resources are unique. That is, there is a single unit of each resource.

Question 1 – a simple example There are 2 processes P 1,P 2 and resource R 1,R 2. P 1 has R 1 and P 2 has R 2. P 1 requests R 2 and is now waiting for P 2 to release it. For a deadlock to occur P 2 needs to request R 1. However, this is in contrast to the assumption that resources can only be requested in ascending order. That is, condition 4 (Circular wait) is prevented if resources are requested in ascending order.

Question 1 – a more formal proof Let P 1,...,P n denote the processes in the system and R 1,…R k the resources. Falsely assume that a deadlock may occur in this system. Specifically, for a deadlock to occur a subset P i1,...,P im of {P 1,...,P n } which satisfies the following condition must exist: P i1 -> (R j,1 )->P i2 - (R j,2 )->...-> (R j,m-1 )->P im -> (R j,m )->P i1 (*) Where P i -> R k ->P j is used to describe the following relation: P i requests a resource R k held by P j.

Question 1 – a more formal proof Each process P i,s, (s≠1) holds resource R j,s-1 and requests resource R j,s. This means that j,s-1<j,s for any s (since the resources are numbered, and are requested in an ascending order). We receive the following inequality which defines the resource ordering: j,1<j,2<...<j,m. In a deadlock, eq. (*) must hold. Thus we conclude that j,m<j,1 and j,1<j,m. That is, a circular wait is not possible and the system is deadlock- free.

Question 1 – Simple Explanation Lets assume we have a deadlock. A circular wait must therefore exist: P i1 - (R j,1 )->P i2 - (R j,2 )->P i3 - (R j,3 )->...- (R j,m-1 )->P im - (R j,m )->P i1 (*) One of these resources’ index is the largest of all resources indices listed. It doesn’t matter which one. Lets assume its R j,2 (since we deal with a circle it doesn’t matter which one we pick). P i2 -> (R j,2 )->P i3 -> (R j,3 )->… (*) This resource is held by process P i3, process P i2 is asking for it, and P i3 requires R j3 at the same time. Lets look at R j3 : If j3 > j2 : this contradicts R j2 being the largest index resource. If j3 = j2 : it makes no sense P i3 is asking for a resource it already has? It breaks the waiting circle. If j3 < j2 : this contradicts the fact that processes ask for resources only in ascending order. Contradictions in every scenario means that our initial assumption of deadlock is false!

The Dining Philosophers Problem 10 Philosophers think and eat every now and then 2 forks are required for eating There aren’t enough forks

Dining Philosophers: Monitor-based implementation monitordiningPhilosophers monitor diningPhilosophers condition self[N]; integer state[N]; pick_sticks procedure pick_sticks(i) state[i] := HUNGRY; test(i); if state[i] <> EATING then wait(self[i]); put_sticks procedure put_sticks(i) state[i] := THINKING; test(LEFT); test(RIGHT); test procedure test(i) if (state[LEFT] <> EATING && state[RIGHT] <> EATING && state[i] = HUNGRY) then { state[i] := EATING; signal(self[i]); } end monitor 11

Question 2 (from exam 2003 moed b, q.1) We extend the monitor-based implementation for 5 philosophers and add to the table a center plate, that is filled by a waiter. The plate holds at most 5 courses. Every philosopher, after taking 2 forks, takes exactly 3 courses from the central plate (one after the other). After taking the courses he eats and returns the forks. a. Write the code for the philosophers and the waiter (based on the previous implementation). Write the required code inside and outside the monitor.

Question 2.a condition variablesfullemptyinPlate  We add 2 condition variables: full, empty and an integer: inPlate = 0  We add 2 procedures to the monitor: fillPlate procedure fillPlate if (inPlate<5){ inPlate++ empty.signal() } else full.wait() takeFromPlate procedure takeFromPlate if(inPlate = = 0) empty.wait() inPlate-- full.signal()

Question 2.a update the philosopher’s function: philosopher procedure philosopher(int i) while(TRUE){ think(); pick_sticks(i); for(j = 1 to 3) takeFromPlate() eat() put_sticks(i); } and add the waiter’s function: waiter Procedure waiter() while(TRUE) fillPlate()

Question 2.b b. In the following sections assume that the center plate starts as full and that the waiter is quick (brings 3 courses each time). Also, the waiter fills the center plate only after a philosopher finished eating. Update the code of section a accordingly.

Question 2.b still_eating We add another condition varaiable: still_eating finished_eating and integer: finished_eating = 0. inPlate = 5 Since we want the waiter to fill the plate only when a philosopher has finished eating we have to assume that the plate is full in the beginning, i.e inPlate = 5 (we don’t need the full condition variable anymore).

Question 2.b Cont’ fillPlate procedure fillPlate if (finished_eating==0) still_eating.wait() inPlate+=3 finished_eating-- empty.signal() takeFromPlate procedure takeFromPlate if(inPlate = = 0){ empty.wait() } inPlate -- /* the only change is that we don’t use full anymore */

Question 2.b Cont’ We also add two lines to the put_sticks function : put_sticks(i) procedure put_sticks(i) state[i] := THINKING; test(LEFT); test(RIGHT); finished_eating ++ still_eating.signal()

Question 2.c Is a deadlock possible? (assuming a quick waiter)

Question 2.c In the case of 5 philosophers this cannot happen: at any given moment there may be at most 2 philosophers that picked up 2 forks. Each one asks for 3 dishes and there are 5 available dishes. At least one of the philosophers will manage 3 dishes on her plate and will eventually wake up the waiter.

Question 2.d Is it possible to have a deadlock with 6 philosophers?

Question 2.d In the case of 6 philosophers, 3 philosophers may pick up 2 forks at the same time. In this case, one possible scenario is that the first takes 2 dishes to her plate, the second takes 2 as well, and the third grabs the last one. As a result, no one will reach the put_sticks phase and the waiter will not be woken up.

Question 3 Six philosophers sit around a table, with a large plate of food in the middle. Forks (represented as ‘F’) and knives (represented as ‘K’) are arranged as shown below. Each philosopher obeys the following algorithm: 1) Grab the closest knife. 2) Grab the closest fork. 3) Carve and devour a piece of broccoli [yummy]. 4) Put down the knife and fork back to where they were. Can a deadlock occur? Indicate why this algorithm satisfies our deadlock conditions, or which one(s) it avoids. If you said that the system could deadlock, describe a reasonable deadlock avoidance scheme. If you said otherwise, specify which scheme is used in this solution.

Answer 3 A deadlock will not occur since the algorithm defines a partial ordering, removing the ability to have circular requests. The easiest way to see the partial order is to label knives 1,2, 3 and forks 4,5,6. Clearly we only acquire in ascending order, and as shown in the previous questions this prevents circular waiting

Banker’s Algorithm Safe – a state is said to be safe if a.It is not deadlocked. b.There is some scheduling order in which every process can run to completion even if all of them suddenly request their maximum number of resources immediately.

Banker’s Algorithm Resources: A.Vectors: 1.E 1.E - Number of Existing resources of each type. 2.P 2.P – Number of resources of each type in Possession by the processes. 3.A 3.A – Number of Available resources of each type. B.Matrices: (rows are processes and columns are resources) 1.C 1.C – Current allocation matrix 2.R 2.R – Request matrix

Banker’s Algorithm R A 1.Look for a row in matrix R whose unmet resource needs are all smaller than or equal to A. If no such row exists, the system may eventually deadlock. A 2.Assume the process of the row chosen finishes (which is possible). Mark that process as terminated and add all its resources to the A vector safe unsafe 3.Repeat steps 1 and 2 until either all processes are marked terminated, which means safe, or until a deadlock occurs, which means unsafe.

Question 4 current allocationmax demandstill needs Process r1r1 r2r2 r3r3 r4r4 r1r1 r2r2 r3r3 r4r4 r1r1 r2r2 r3r3 r4r4 p1p p2p p3p p4p p5p r1r1 r2r2 r3r3 r4r Consider the following snapshot of a system with five processes (p 1,... p 5 ) and four resources (r 1,... r 4 ). currently Available resources

Question 4 a. Compute what each process still might request and fill in the “still needs” columns. support b. Is this system currently deadlocked, or will any process become deadlocked? Use the baker’s algorithm to support your answer

Question 4 a) current allocation max demandstill needs Process r1r1 r2r2 r3r3 r4r4 r1r1 r2r2 r3r3 r4r4 r1r1 r2r2 r3r3 r4r4 p1p p2p p3p p4p p5p

Question 4 a) Not deadlocked and will not become deadlocked. Using the Banker’s algorithm, we determine the process execution order: p 1, p 4, p 5, p 2, p 3. r1r1 r2r2 r3r3 r4r b) currently available resources current allocation max demandstill needs Process r1r1 r2r2 r3r3 r4r4 r1r1 r2r2 r3r3 r4r4 r1r1 r2r2 r3r3 r4r4 p1p p2p p3p p4p p5p

Question 4 c. If a request from p 3 arrives for (0, 1, 0, 0), can that request be safely granted immediately? In what state (deadlocked, safe, unsafe) would immediately granting the request leave the system? Which processes, if any, are or may become deadlocked if this whole request is granted immediately?

Question 4 r1r1 r2r2 r3r3 r4r currently available resources current allocation max demandstill needs Process r1r1 r2r2 r3r3 r4r4 r1r1 r2r2 r3r3 r4r4 r1r1 r2r2 r3r3 r4r4 p1p p2p p3p p4p p5p

Question 4 c) Change available to (2, 0, 0, 0) and p3’s row of “still needs” to (6, 5, 2, 2). Now p1, p4, and p5 can finish. Available will now be (4, 6, 9, 8) meaning that neither p2 nor p3’s “still needs” can be satisfied. So, it is not safe to grant p3’s request. Correct answer NO. Processes p2 and p3 may deadlock.

Question 5 (7.6 from Silberschats) If deadlocks are controlled (avoided) by applying the banker‘s algorithm, which of the following changes can be made safely and under what circumstances: 1.Increase Available (add new resources) 2.Decrease Available (remove resources) 3.Increase Max for one process 4.Increase the number of processes

Question 5 1.Increasing the number of resources available can't create a deadlock since it can only decrease the number of processes that have to wait for resources. 2.Decreasing the number of resources can cause a deadlock.

Question 5 3.From the Banker’s algorithm point of view, increasing the maximum claim of a process may turn a safe state into unsafe. 4.If the number of processes is increased, the state remains safe, since we can first run the “old” processes, until they terminate, and release all their resources. Now, when all the system’s resources are free, we can choose a “new” process and give it all its demands. It will finish and again all the system’s resources will be free. Again, we choose a new process and give it all its demands, etc.

Question 6 (7.9 from Silberschats) Consider a system consisting of m resources of the same type, being shared by n processes. Resources can be requested and released by processes only one at a time. Show that the system is deadlock free if the following two conditions hold: 1. Each process needs between 1 and m resources. 2. The sum of maximum needs is less than m+n.

Question 6 circular wait By contradiction, assume that the 4 conditions for deadlock exist in the system and thus there is a group of processes involved in a circular wait. P 1,...,P k, k≤nD 1,...,D k H 1,...,H k Let these processes be P 1,...,P k, k≤n, their current demands be D 1,...,D k and the number of resources each of them holds be H 1,...,H k. P 1 ->P 2 ->...->P k ->P 1 The circular wait condition should look like: P 1 ->P 2 ->...->P k ->P 1, but in fact it is simpler: M 1,...,M n P 1,...,P n Let M 1,...,M n be total (maximum) demands of processes P 1,...,P n. Then a circular wait can occur only if all resources are in use and every process hasn't acquired all its resources: H H k =m D i >=1 H H k =m and D i >=1 for 1≤ i≤k.

Question 6 M i =H i +D i M M k ≥m+k Since M i =H i +D i, the sum of maximum demands of the processes involved in a circular wait is: M M k ≥m+k. P k+1,...,P n Note that the remaining processes’ P k+1,...,P n maximum demands are at least 1: M i ≥ 1, M k M n ≥ n-k M i ≥ 1, k+1 ≤ i ≤ n and thus M k M n ≥ n-k. The total sum of maximum demands is thus: M M n = M M k +M k M n ≥ m+k+(n-k)=m+n M M n = M M k +M k M n ≥ m+k+(n-k)=m+n. m+n It is defined that sum of all maximal needs is strictly less than m+n, thus we have a contradiction.

Question 6 – Intuitive Explanation If we have a deadlock all resources are held by the various processes, otherwise some process can take a resource and “advance” and we are not in a deadlock. Therefore: Every process needs at least 1 resource more, or else we don’t have a deadlock (it is free to “advance”). Therefore: Total sum of maximum demands is: This contradicts the assumption total sum of maximum demands is less than m+n.

Question 7 True or False Monitors prevent deadlocks from ever occurring.

Question 7 If monitor code has a circular dependency, it can deadlock. Assume there are two monitors a,b and two processes A,B. A holds a and waits for b. B holds b and waits for a.