1 Lecture 8: Deadlocks Operating System Spring 2008.

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
1 Concurrency: Deadlock and Starvation Chapter 6.
ICS Principles of Operating Systems Lectures 8 and 9 - Deadlocks Prof. Dmitri V. Kalashnikov dvk ics.uci.edu Slides © Prof. Nalini Venkatasubramanian.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Lecture 6 :Deadlocks. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate with each other Involves.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Chapter 7: Deadlocks.
Chapter 6 Concurrency: Deadlock and Starvation
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 7: 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.
Chapter 7: Deadlocks. 7.2 Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks.
1 Chapter 7: Deadlock. 2 The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Concurrency: Deadlock & Starvation
Witawas Srisa-an Chapter 6
CPSC 4650 Operating Systems Chapter 6 Deadlock and Starvation
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 7: Deadlock Dr. Mohamed Hefeeda.
1 Concurrency: Deadlock and Starvation Chapter 6.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Bridge Crossing Example Traffic only in one direction. Each section.
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.
Concurrency: Deadlock and Starvation Chapter 6. Goal and approach Deadlock and starvation Underlying principles Solutions? –Prevention –Detection –Avoidance.
Concurrency: Deadlock and Starvation
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Deadlocks.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks.
Chapter 7 Deadlocks. 7.2 Modified By Dr. Khaled Wassif Operating System Concepts – 7 th Edition Silberschatz, Galvin and Gagne ©2005 Chapter 7: Deadlocks.
Cosc 4740 Chapter 6, Part 4 Deadlocks. The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-6 Deadlocks Department of Computer Science and Software Engineering.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 AE4B33OSS Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization.
CHAPTER 8: DEADLOCKS System Model Deadlock Characterization
CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.
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.
Dr. Kalpakis CMSC 421, Operating Systems Deadlocks.
Deadlock Operating Systems: Internals and Design Principles.
Chapter 6 Concurrency: Deadlock and Starvation
Deadlocks System Model RAG Deadlock Characterization
Styresystemer og Multiprogrammering Block 3, 2005 Deadlocks Robert Glück.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 7 Operating Systems.
Chapter 8 Deadlocks. Objective System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection.
Chapter 7: Deadlocks. 7.2CSCI 380 – Operating Systems Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization.
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.
Deadlock. Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Deadlocks.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 7: Deadlocks.
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);
Chapter 7: Deadlocks.
ITEC 202 Operating Systems
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks.
Advanced Operating System Fall 2009
Operating System: DEADLOCKS
Chapter 7 Deadlock.
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks.
Deadlocks Session - 13.
Chapter 7: Deadlocks.
Presentation transcript:

1 Lecture 8: Deadlocks Operating System Spring 2008

2 Contents What is deadlock Conditions for deadlock Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection and Recovery

3 What is deadlock? A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. Example: Process P … request A … request B … release A … release B Process Q … request B … request A … release B … release A

4 Bridge Crossing Example Traffic only in one direction. Each section of a bridge can be viewed as a resource. If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback). Several cars may have to be backed up if a deadlock occurs. Starvation is possible.

5 Reusable Resources One that can be safely used by only one process at a time and is not depleted by that use. Serially Reusable Example: Memory (main & secondary) Processors I/O channels Semaphores Files Databases Printer etc.

6 Example 1 P Request disk drive Lock disk drive Request tape drive Lock tape drive … perform function Unlock disk drive Unlock tape drive Q Request tape drive Lock tape drive Request disk drive Lock disk drive … perform function Unlock tape drive Unlock disk drive

7 Example 2 Suppose we have a pool of 200 kbytes of memory for allocation: P … Request 80 Kbytes … Request 60 Kbytes Q … Request 70 Kbytes … Request 80 Kbytes

8 Consumable Resources One that can be created and destroyed Example: Message passed among processes

9 Conditions for deadlock Mutual exclusion: only one process at a time can use a resource. Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. No preemption: A resource can be released only voluntarily by the process holding it, after that process has completed its task. No resources can be forcibly removed from a process holding it. Circular wait: there exists a set {P 0, P 1, …, P 0 } of waiting processes such that P 0 is waiting for a resource that is held by P 1, P 1 is waiting for a resource that is held by P 2, …, P n–1 is waiting for a resource that is held by P n, and P 0 is waiting for a resource that is held by P 0.

10 Methods for Handling Deadlocks Deadlock prevention: Design policies so that deadlock can’t occur. Deadlock avoidance: The OS will monitor the progress of processes so that deadlock can always be avoided. Deadlock detection and recovery: Do nothing and allow deadlock occur, if deadlock are detected, then recover at a minimal cost. Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX.

11 Deadlock Prevention Design policies so that deadlock can’t occur: Mutual Exclusion – usually cannot be disallowed Hold and wait require each process to release all resources held before making a new request. a process may request resources ahead of time. Drawback: inefficient utilization of resources. No preemption – preemption is allowed. A good example is the example of the memory pool. If we get into a deadlock, move the allocated memory to secondary memory (preemption) and restore later. Circular Wait – Assign a linear ordering of resource types. R1<R2<R3… If a process has been allocated resources of type i (Ri), then it may request resources of type higher than i. If it wants resources of type lower than i, then it must release resources of type i.

12 Deadlock Avoidance When a process requests resources, a decision is made whether the current request will, if granted, potentially lead to a deadlock. Deadlock avoidance thus requires knowledge of future processes resource request. Do not start a process if its demands might lead to deadlock Do not grant an incremental resource request to a process if this allocation might lead to deadlock.

13 Deadlock Avoidance(cont.) Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes. Total amount of each resource in the system Total amount of each resource not allocated to a process Requirement of each process of each resource (declared by the processes ahead of time) Current allocation

14 Example Total amount of each resource in the system Total amount of each resource not allocated to a process Requirement of each process of each resource (declared by the processes ahead of time) Current allocation n=4, m=3 4 processes, 3 resources in the system

15 Safe State The state of the system is simply the current allocation of resources to processes. A safe state is one in which there is at least one sequence that does not result in a deadlock An unsafe state is a state that is not safe.

16 Is this example a safe state? Total amount of each resource in the system Total amount of each resource not allocated to a process Requirement of each process of each resource Current allocation n=4, m=3 4 processes, 3 resources P1 cannot proceed. In the worst case, P1 will request 2 units of R1, 2 units of R2 and 2 units of R3. Impossible to satisfy P2 can proceed. In the worst case, P2 will request 1 more units of R3. Possible to satisfy. When it completes, it will release all resources. Now P1 can proceed. Now P3 can proceed. Now P4 can proceed. It is a safe sate.

17 Basic Facts If a system is in safe state  no deadlocks. If a system is in unsafe state  possibility of deadlock. Avoidance  ensure that a system will never enter an unsafe state. In deadlock avoidance, each time a process requests resource, the OS will determine if it will, after granting the resources to the process, lead it to an unsafe state. At the beginning, the system is in a safe sate, at each step, the system is always in a safe state. Avoidance algorithm: Banker’s Algorithm

18 Banker’s Algorithm Checking to see if a state is safe or not Algorithm: 1. Find a process P k such that Claim(k,*)- Allocation(k,*)  Available(*) for  *. If not found, go to Available(*) <- Available(*)+Allocation(k,*) for  *. 3. Goto 1 4. If there is no process left, the state is safe. Otherwise, it is not safe.

19 Example 1 Total amount of each resource in the system Total amount of each resource not allocated to a process Requirement of each process of each resource Current allocation n=4, m=3 4 processes, 3 resources This is a safe state. Suppose P1 makes a request of 1 unit of R2. Should the OS Grants the request? Well, the OS should if it lead it to a safe State. Is this a safe state? Yes. So the OS will grant this request.

20 Example 2 Total amount of each resource in the system Total amount of each resource not allocated to a process Requirement of each process of each resource Current allocation n=4, m=3 4 processes, 3 resources This is a safe state. Suppose P3 asks for one unit of R3. Should the OS grants the request? Is this a safe state? No. So the OS will block this request.

21 Deadlock Detection and Recovery Allow deadlocks to occur. If and when it occurs, recover from it. The system checks periodically whether it is in deadlock. If a deadlock occurs, recovery procedure will be called.

22 Deadlock Detection Algorithm Are we in a deadlock state? Detection Algorithm: 1.Initialize a temporary vector w to equal to Available vector. w(*)=V(*) 2.Mark each process that has a row in the Allocation matrix of all zeros. 3.Find an index k such that process k is currently unmarked and the k th row of Q  w. That is, Q(k,*)  w(*) for  *. If no such row is found, goto step 5 4.Mark process k and add the corresponding row of the allocation matrix to w. That is, w(*)=w(*)+A(k,*). Go to step 3 5.If every process is marked, then there is no deadlock. Otherwise, the unmarked process are deadlocked.

23 Example 1 Is there a deadlock? w=(0,1,1) P1’s request  w w=(0,1,1)+(1,0,0)=(1,1,1) P2’s request  w w=(1,1,1)+(6,1,2)=(7,2,3) P3’s request  w w=(7,2,3)+(2,1,1)=(9,3,4) P4’s request  w w=(9,3,4)+(0,0,2)=(9,3,6) Every process is marked. No deadlock.

24 Example 2 Is there a deadlock? w=(0,1,1) P4’s request  w w=(0,1,1)+(0,0,2)=(0,1,3) P1,P2,P3 are involved in a deadlock.

25 Recovery Two methods: 1. Abort all deadlocked processes. 2. Find a subset of deadlocked processes with the minimum cost that can break the deadlock.

26 Example for method 2 Cost of aborting P1 = c1 = 3 Cost of aborting P2 = c2 = 9 Cost of aborting P3 = c3 = 4 Aborting P2 will resolve the deadlock, the cost = 9 Aborting P1 will not resolve the deadlock Aborting P3 will resolve the deadlock, the cost = 4 P3 is the optimal choice. The problem is NP-Hard.

27 End of lecture 8 Thank you!