Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8: Deadlocks.

Similar presentations


Presentation on theme: "Chapter 8: Deadlocks."— Presentation transcript:

1 Chapter 8: Deadlocks

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

3 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

4 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.” 

5 System Model

6 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

7 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()

8 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)

9 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:

10 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

11 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.

12 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 )

13 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

14 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

15 Methods for Handling Deadlocks

16 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

17 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

18 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

19 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.

20 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

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

22 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

23 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

24 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.

25 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

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

27 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

28 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

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

30 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

31 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)

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

33 Recovery from Deadlock
Process and Thread Termination Resource Preemption

34 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?

35 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

36 End of Chapter 8


Download ppt "Chapter 8: Deadlocks."

Similar presentations


Ads by Google