Operating System: DEADLOCKS

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Chapter 7: Deadlocks.
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.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
Deadlocks Gordon College Stephen Brinton. Deadlock Overview The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks.
What we will cover…  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 Deadlocks.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks.
System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention, Avoidance, and Detection Recovering from Deadlock Combined Approach.
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
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
 The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock Avoidance  Deadlock.
Dr. Kalpakis CMSC 421, Operating Systems Deadlocks.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks System Model Deadlock Characterization Methods.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 7 Operating Systems.
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.
Deadlocks - System Model - Deadlock characterization - Methods for handling deadlocks - Deadlock prevention,avoidance - Deadlock detection and recovery.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 7: Deadlocks.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
7.1 CSE Department MAITSandeep Tayal 7: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
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. The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Thursday, February 23, 2012 Chapter 6 homework questions?
Chapter 7: Deadlocks.
OPERATING SYSTEM CONCEPTS AND PRACTISE
Chapter 7: Deadlocks.
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks.
Chapter 7 Deadlocks.
Process Deadlocks.
Chapter 7: Deadlocks.
Deadlock B.Ramamurthy CSE421 1/11/2019 B.Ramamurthy.
Lecture 6: Deadlocks, Deadlock Risk Management
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks.
Outline Deadlocks, dead lock prevention, avoidance.
Deadlock Prevention Restrain the ways request can be made.
Deadlock B.Ramamurthy CSE421 2/23/2019 B.Ramamurthy.
Deadlocks Session - 13.
Lecture 27 Syed Mansoor Sarwar
Chapter 7: Deadlocks.
Deadlock B.Ramamurthy CSE421 4/23/2019 B.Ramamurthy.
Deadlock B.Ramamurthy CSE421 5/1/2019 B.Ramamurthy.
CSCI 315 Operating Systems Design
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Chapter 8: Deadlocks Deadlock Characterization
OPERATING SYSTEM CONCEPTS
Deadlock B.Ramamurthy CSE421 8/28/2019 B.Ramamurthy.
Deadlock B.Ramamurthy CSE421 9/3/2019 B.Ramamurthy.
Presentation transcript:

Operating System: DEADLOCKS Lecture#10

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. 8 November 2018

What is Deadlock? Example System has 2 tape drives. P1 and P2 each hold one tape drive and each needs another one. 8 November 2018

The Deadlock Problem Example: semaphores A and B, initialized to 1 P0 P1 wait (A); wait(B); wait (B); wait(A); 8 November 2018

The Deadlock Problem signal(A); P0 P1 signal(B); 8 November 2018

The Deadlock Problem The Dining Philosophers problem … all philosophers become hungry at the same time, pick up the chopsticks on their right and wait for getting the chopsticks on their left. 8 November 2018

Bridge Crossing Example Traffic only in one direction. Each section of a bridge can be viewed as a resource. 8 November 2018

Bridge Crossing Example 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. 8 November 2018

System Model Resource types R1, R2, . . ., Rm (CPU cycles, memory space, I/O devices) Each resource type Ri has Wi instances. Each process utilizes a resource as follows: request → use → release 8 November 2018

Deadlock Characterization Deadlock can arise if four conditions hold simultaneously. Mutual exclusion: only one process at a time can use a resource. 8 November 2018

Deadlock Characterization Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. 8 November 2018

Deadlock Characterization No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task. 8 November 2018

Deadlock Characterization Circular wait: There exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and P0 is waiting for a resource that is held by P0. P0 → P1 → P2 → … → Pn → P0 8 November 2018

Resource Allocation Graph A set of vertices V and a set of edges E. V is partitioned into two types: P = {P1, P2, …, Pn} R = {R1, R2, …, Rm} E = {Request Edges, Assignment Edges} Request Edge: P1  Rj Assignment Edge: Rj  Pi 8 November 2018

Resource Allocation Graph Process Resource Type with 4 instances Pi requests instance of Rj Pi is holding an instance of Rj Pi Pi 8 November 2018

Example Graph 8 November 2018

Graph with a Deadlock P1 -> R1 -> P2 -> R3 -> P3 -> R2 -> P1 P2 -> R3 -> P3 -> R2 -> P1 8 November 2018

Graph with a Cycle but No Deadlock P1 -> R1 -> P3 -> R2 -> P1 No deadlock P4 may release its instance of resource R2 Then it can be allocated to P3 8 November 2018

RESOURCE-ALLOCATION GRAPH 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, possibility of deadlock.

Ensure that the system will never enter a deadlock state. Deadlock Handling Ensure that the system will never enter a deadlock state. Allow the system to enter a deadlock state and then recover. Ignore the problem and pretend that deadlocks never occur in the system. 8 November 2018

Deadlock Handling Deadlock prevention Deadlock avoidance Deadlock detection and recovery 8 November 2018

Deadlock Prevention Restrain the ways resource allocation requests can be made to insure that at least one of the four necessary conditions is violated. 8 November 2018

Deadlock Prevention Mutual Exclusion Cannot be prevented for all resources. Some resources are inherently non-sharable because their states cannot be saved and restored without ill effects, such as a printer. 8 November 2018

Deadlock Prevention Hold and Wait – we must guarantee that whenever a process requests a resource, it does not hold any other resources. 8 November 2018

Deadlock Prevention Require a process to request and be allocated all its resources before it begins execution, or allow a process to request resources only when the process has none. Low resource utilization; starvation possible. 8 November 2018

Deadlock Prevention No Preemption If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. 8 November 2018

Deadlock Prevention Preempted resources are added to the list of resources for which the process is waiting for. Process will be restarted only when it can regain its old resources as well as the new ones that it has requested. 8 November 2018

Deadlock Prevention Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. 8 November 2018

Deadlock Prevention We assign a unique number to each resource type by using function F: R → N and make sure that processes request resources in an increasing order of enumeration. For example, tape drive = 1, disk drive = 5, and printer = 12. 8 November 2018

Deadlock Prevention Proof Let’s assume that there is a cycle P0 → P1 → P2 → … → Pk → P0 R0 R1 R2 Rk R0  F(R0) < F(R1) < … F(Rk) < F(R0)  F(R0) < F(R0), which is impossible  There can be no circular wait. 8 November 2018

Deadlock Avoidance Requires that the system has additional a priori information available about the use of resources by processes. Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need. 8 November 2018

Deadlock Avoidance The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition. Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes. 8 November 2018

Safe State When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. System is in a safe state if there exists a safe sequence of all processes. 8 November 2018

Safe State Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources that Pi can still request can be satisfied by the currently available resources, plus the resources held by all the Pj, with j<i. In other words, a safe sequence specifies the order in which processes can be finished. 8 November 2018

Safe State If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished. When Pj are finished, Pi can obtain needed resources, execute, return allocated resources, and terminate. When Pi terminates, Pi+1 can obtain its needed resources, and so on. 8 November 2018

Basic Facts If a system is in safe state  no deadlocks. If a system is in unsafe state  possibility of deadlock due to the behavior of processes. Avoidance  ensure that a system never enters an unsafe state. 8 November 2018

Safe, Unsafe, and Deadlock States 8 November 2018

Example System with 12 tape drives and three processes Current system state: Process Max Need Allocated P0 10 5 P1 4 2 P2 9 8 November 2018

Example System is in a safe state with the safe sequence <P1, P0, P2> P2 requests and is allocated one more tape drive. Process Max Need Allocated P0 10 5 P1 4 2 P2 9 8 November 2018

Example Assuming the the tape drive is allocated to P2, the new system state will be: Process Max Need Allocated P0 10 5 P1 4 2 P2 9 3 System gets into an unsafe state. 8 November 2018

Deadlock Detection Allow system to enter deadlock state Detection algorithm Recovery scheme

Single Instance of Each Resource Type Maintain a wait-for graph Nodes are processes. Pi  Pj if Pi is waiting for Pj.

Single Instance of Each Resource Type Periodically invoke an algorithm that searches for a cycle in the wait-for graph. The algorithm is expensive—it requires an order of n2 operations, where n is the number of vertices in the graph.

RAG and Wait-for Graph Resource-Allocation Graph Corresponding wait-for graph

Multiple Instance of Each Resource Type Available: A vector of length m indicates the number of available resources of each type. Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process.

Multiple Instance of Each Resource Type Request: An n x m matrix indicates the current request of each process. If Request[i,j]=k, then process Pi is requesting k more instances of resource type. Pj.

Detection Algorithm Let Work and Finish be vectors of length m and n, respectively Initialize: Work = Available; for i = 1, 2, …, n if Allocationi  0 then Finish[i] = false; else Finish[i] = true;

If no such i exists, go to step 4. Detection Algorithm Find an index i such that both: (a) Finish[i] == false (b) Requesti  Work If no such i exists, go to step 4.

Detection Algorithm Work = Work + Allocationi Finish[i] = true go to step 2. If Finish[i] == false, for some i, 1  i  n, then the system is in deadlock state. Moreover, if Finish[i] == false, then Pi is deadlocked.

Example of Detection Algorithm Consider the following system: P = { P0, P1, P2, P3, P4 } R = { A, B, C } A: 7 instances B: 2 instances C: 6 instances

Example System State Finish Sequence: <> Process P0 P1 P2 P3 P4 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 1 0 0 0 0 2 Work A B C Finish Sequence: <>

Example System State Finish Sequence: <P0> Process P0 P1 P2 P3 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 1 0 0 0 0 2 Work A B C 1 Finish Sequence: <P0>

Example System State Finish Sequence: <P0, P2> Process P0 P1 P2 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 1 0 0 0 0 2 Work A B C 1 3 2 Finish Sequence: <P0, P2>

Example System State Finish Sequence: < P0, P2, P3> Process P0 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 1 0 0 0 0 2 Work A B C 1 3 2 5 Finish Sequence: < P0, P2, P3>

Example System State Finish Sequence: < P0, P2, P3, P4 > Process Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 1 0 0 0 0 2 Work A B C 1 3 2 5 Finish Sequence: < P0, P2, P3, P4 >

Example System State Final finish sequence: < P0, P2, P3, P4 , P1 > Other possible finish sequences: < P0, P2, P3, P1 , P4 > < P0, P2, P4, P1 , P3 > < P0, P2, P4, P3 , P1 > …

Example System State P2 requests an additional instance of C. Do we have a finish sequence? Process P0 P1 P2 P3 P4 Request A B C 0 0 0 2 0 2 0 0 1 1 0 0 0 0 2

Example System State Finish Sequence: <> Process P0 P1 P2 P3 P4 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 0 0 1 1 0 0 0 0 2 Work A B C Finish Sequence: <>

Example System State Finish Sequence: <P0> Process P0 P1 P2 P3 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Request A B C 0 0 0 2 0 2 0 0 1 1 0 0 0 0 2 Work A B C 1 Finish Sequence: <P0>

Example System State P0’s request can be satisfied with currently available resources, but request for no other process can be satisfied after that.  Deadlock exists, consisting of processes P1, P2, P3, and P4.

Detection Algorithm How often should the detection algorithm be invoked? Every time a request for allocation cannot be granted immediately—expensive but process causing the deadlock is identified, along with processes involved in deadlock

Detection Algorithm Periodically, or based on CPU utilization Arbitrarily—there may be many cycles in the resource graph and we would not be able to tell which of the many deadlocked processes “caused” the deadlock.

Recovery from Deadlock: Process Termination Abort all deadlocked processes. Abort one process at a time until the deadlock cycle is eliminated.

Recovery from Deadlock: Process Termination In which order should we choose to abort processes? Priority of a process. How long the process has computed, and how much longer to completion.

Recovery from Deadlock: Process Termination Resources the process has used. Resources the process needs to complete. How many processes will need to be terminated. Is the process interactive or batch?

Recovery from Deadlock: Resource Preemption Selecting a victim – minimize cost. Rollback – return to some safe state, restart process from that state. Starvation – same process may always be picked as victim; include number of rollbacks in cost factor.