Chapter 8: 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.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 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.
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 8: Deadlocks System Model Deadlock Characterization
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: 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.
System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention, Avoidance, and Detection Recovering from Deadlock Combined Approach.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks Modified.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition 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.
CHAPTER 8: DEADLOCKS System Model Deadlock Characterization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
 The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock Avoidance  Deadlock.
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 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.
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.
CSE Operating System Principles Deadlocks. CSE – Operating System Principles2 Overview System Model Deadlock Characterization Methods for.
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 Source & Copyright: Operating System Concepts, Silberschatz, Galvin and Gagne.
Operating Systems (CS 340 D)
Chapter 7: Deadlocks.
Operating System: DEADLOCKS
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.
Chapter 7: Deadlocks.
Deadlock B.Ramamurthy CSE421 4/23/2019 B.Ramamurthy.
Deadlock B.Ramamurthy CSE421 5/1/2019 B.Ramamurthy.
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Chapter 8: Deadlocks Deadlock Characterization
Deadlock B.Ramamurthy CSE421 8/28/2019 B.Ramamurthy.
Deadlock B.Ramamurthy CSE421 9/3/2019 B.Ramamurthy.
Presentation transcript:

Chapter 8: Deadlocks

Chapter 8: Deadlocks System Model Deadlock in Multithreaded Applications Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock

Chapter Objectives Illustrate how deadlock can occur when mutex locks are used Define the four necessary conditions that characterize deadlock Identify a deadlock situation in a resource allocation graph Evaluate the four different approaches for preventing deadlocks Apply the banker’s algorithm for deadlock avoidance Apply the deadlock detection algorithm Evaluate approaches for recovering from deadlock

Background In a multiprogramming/multithreading environment, Several threads may compete for a finite number of resources A thread requests resources: If the resources are not available at that time, the thread enters a waiting state. Sometimes, a waiting thread can never again change state, because the resources it has requested are held by other waiting threads This situation is called a deadlock. Deadlock is a situation in which every process in a set of processes is waiting for an event that can be caused only by another process in the set, all of which are waiting Perhaps the best illustration of a deadlock can be drawn from a law passed by the Kansas legislature early in the 20th century. It said, in part: “When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone.” 

System Model

System Model System consists of resources Resource types R1, R2, . . ., Rm E.g. CPU cycles, memory space, I/O devices, etc. Each resource type Ri has Wi instances. Under the normal mode of operation, a process/thread utilizes a resource as follows: Request: The thread requests the resource. If the request cannot be granted immediately (for example, if a mutex lock is currently held by another thread), then the requesting thread must wait until it can acquire the resource. Use: The thread can operate on the resource (for example, if the resource is a mutex lock, the thread can access its critical section) Release: The thread releases the resource

Deadlock in Multithreaded Application Two mutex locks are created an initialized: Note: If a thread attempts to acquire a locked mutex, the call to: Pthread_mutex_lock() blocks the thread until the owner of the mutex lock invokes the pthread_mutex_unlock()

Deadlock in Multithreaded Application /* thread one runs in this function */ void *do_work_one(void *param) { pthread_mutex_lock( &first_mutex ); pthread_mutex_lock( &second_mutex ); /** * Do some work */ pthread_mutex_unlock( &second_mutex ); pthread_mutex_unlock( &first_mutex ); pthread_exit(0); } /* thread two runs in this function */ void *do_work_two(void *param)

Deadlock in Multithreaded Application Deadlock is possible, if : thread_one acquires first_mutex and thread_two acquires second_mutex thread_one then waits for second_mutex and thread_two waits for first_mutex. Can be illustrated with a resource allocation graph:

Deadlock in Multithreaded Application Note that, even though deadlock is possible, it will not occur if thread_one can acquire and release the mutex locks for first mutex and second mutex before thread_two attempts to acquire the locks. And, of course, the order in which the threads run depends on how they are scheduled by the CPU scheduler. This example illustrates a problem with handling deadlocks: it is difficult to identify and test for deadlocks that may occur only under certain scheduling circumstances

Deadlock Characterization Deadlock can arise if four conditions hold simultaneously. 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 Circular wait: There exists a set {P0, P1, …, Pn} 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 Pn is waiting for a resource that is held by P0.

Resource-Allocation Graph Deadlocks can be described more precisely in terms of a directed graph called a system resource-allocation graph. A set of vertices V and a set of edges E. V is partitioned into two types: P = {P1, P2, …, Pn}, the set consisting of all the processes in the system R = {R1, R2, …, Rm}, the set consisting of all resource types in the system Request edge – directed edge Pi  Rj Assignment edge – directed edge Rj  Pi (or Pi  Rj )

Resource Allocation Graph Example One instance of R1 Two instances of R2 One instance of R3 Three instance of R4 T1 holds one instance of R2 and is waiting for an instance of R1 T2 holds one instance of R1, one instance of R2, and is waiting for an instance of R3 T3 holds one instance of R3

Resource Allocation Graph With A Deadlock No Deadlock 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

Methods for Handling Deadlocks

Methods for Handling Deadlocks Generally speaking, we can deal with the deadlock problem in one of three ways: Ignore the problem and pretend that deadlocks never occur in the system Use a protocol to prevent or avoid deadlocks, ensuring that the system will never enter a deadlocked state Deadlock Prevention Ensure that at least one of the necessary conditions cannot hold (Mutual Exclusion, Hold and Wait, No Preemption, Circular Wait) Deadlock Avoidance OS need to be given additional information in advance concerning which resources a thread will request and use during its lifetime, then the OS can decide for each request whether or not the thread should wait For each thread need to know: Available vs. Allocated resources, and what are the future requests and releases Allow the system to enter a deadlock state and then If neither a deadlock-prevention nor a deadlock-avoidance is implemented, then a deadlock situation may arise Detect it, and Recover

Invalidate one of the four necessary conditions for deadlock: 1. Deadlock Prevention Invalidate one of the four necessary conditions for deadlock: Mutual Exclusion – We cannot prevent deadlocks by denying the mutual-exclusion condition not required for sharable resources (e.g., read-only files); must hold for non-sharable resources (e.g. a mutex lock cannot be simultaneously shared by several threads) Hold and Wait – We need to: “ensure that the hold-and-wait condition never occurs in the system” must guarantee that whenever a process requests a resource, it does not hold any other resources Approach #1: require process to request and be allocated all its resources before it begins execution, Cons: impractical for most applications and if applied Low resource utilization Approach #1: allows a thread to request resources only when it has none allocated to it. Cons: may lead to starvation, wait indefinitely If the needed R is always allocated to some other thread

Deadlock Prevention (Cont.) No Preemption – of resources that have already been allocated, to prevent deadlock, we need to ensure that this condition does not hold: If a process is holding some resources and requests another that cannot be immediately allocated to it, then all resources currently being held are released (preempted) 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 is requesting All three above options are almost impractical in most situations Circular Wait – preventing it is a practical solution  imposing a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration

How to Invalidate Circular Wait Invalidating the circular wait condition is most common. Simply assign each resource (i.e. mutex locks) a unique number, ( this number determines whether one precedes another) An ordering among all synchronization objects Then, resources must be acquired in order. i.e. each thread can request resources only in an increasing order of enumeration and released in a decreasing order Example: first_mutex = 1 second_mutex = 5 code for thread_two could not be written as follows: If used, then circular-wait condition cannot hold.

1. Deadlock Prevention (Cont.) Deadlock-prevention algorithms prevent deadlocks by limiting how requests/releases can be made. Pros: The limits ensure that at least one of the necessary conditions for deadlock cannot occur Cons: Possible side effects of preventing deadlocks by this method, low device utilization and reduced system throughput

Deadlock Avoidance Safe State Resource-Allocation-Graph Algorithm Banker’s Algorithm

2. Deadlock Avoidance Requires that the system has some additional a priori information available Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need 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

2.1. Safe State When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state A state is safe if the system can allocate resources to each thread (up to its maximum) in some order and still avoid a deadlock. System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the processes in the systems such that: for each Pi, the resources that Pi can still request to be satisfied by currently available resources + resources held by all the Pj, with j < i That is: If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate When Pi terminates, then Pi+1 can obtain its needed resources, and so on

Example Consider a system with: 12 resources and 3 threads/processes: T0, T1, and T2. Assume: T0 requires 10 resources, thread T1 may need as many as 4, and thread T2 may need up to 9 resources. Suppose that, at time t0, thread T0 is holding 5 resources, thread T1 is holding 2 resources, and thread T2 is holding 2 resources. (Thus, there are 3 free resources.) At time t0, the system is in a safe state. The sequence <T1, T0, T2> satisfies the safety condition T1 can immediately be allocated all its resources and then return them (the system will then have 5 available resources); then T0 can get all its resources and return them (the system will then have 10 available resources); and finally T2 can get all its resources and return them (the system will then have all 12 resources available). A system can go from a safe state to an unsafe state. Suppose that, at time t1, the T2 requests and is allocated 1 more resource. Then only T1 can be allocated all its resources. When it returns them, the system will have only 4 available resources. Since T0 is allocated 5 resources but has a maximum of 10, it may request 5 more resources. If it does so, it will have to wait, because they are unavailable.

Safe, Unsafe, Deadlock State 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. Safe, Unsafe, Deadlock State A safe state is not a deadlocked state. Equally, a deadlocked state is an unsafe state. Not all unsafe states are deadlocks As long as the state is safe, the operating system can avoid unsafe (and deadlocked) states Safe, Unsafe, Deadlock State

Avoidance Algorithms Single instance of a resource type Use a resource-allocation graph Multiple instances of a resource type Use the Banker’s Algorithm

Resource-Allocation Graph Scheme Claim edge Pi  Rj indicated that process Pj may request resource Rj; represented by a dashed line ----- Claim edge converts to request edge when a process requests a resource Request edge converted to an assignment edge when the resource is allocated to the process When a resource is released by a process, assignment edge reconverts to a claim edge Resources must be claimed a priori in the system

Resource-Allocation Graph Algorithm Suppose that process Pi requests a resource Rj The request can be granted only if converting the request edge to an assignment edge does not result in the formation of a cycle in the resource allocation graph Unsafe State In Resource-Allocation Graph

Deadlock Detection Single Instance of Each Resource Type Several Instances of a Resource Type Detection-Algorithm Usage

Deadlock Detection If a system does not employ either a deadlock-prevention or a deadlock avoidance algorithm, then a deadlock situation may occur Allow system to enter deadlock state. And the environment provide: Detection algorithm An algorithm that examines the state of the system to determine whether a deadlock has occurred Recovery scheme An algorithm to recover from the deadlock

Single Instance of Each Resource Type Maintain wait-for graph Obtain this graph from the resource-allocation graph by removing the resource nodes and collapsing the appropriate edges Nodes are processes Pi  Pj if Pi is waiting for Pj Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a deadlock An algorithm to detect a cycle in a graph requires an order of n2 operations, where n is the number of vertices in the graph (# of processes and/or threads)

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

Recovery from Deadlock Process and Thread Termination Resource Preemption

Recovery from Deadlock: Process Termination Abort all deadlocked processes Abort one process at a time until the deadlock cycle is eliminated In which order should we choose to abort? Priority of the process How long process has computed, and how much longer to completion Resources the process has used Resources process needs to complete How many processes will need to be terminated Is process interactive or batch?

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

End of Chapter 8