Chapter 7: Deadlocks Silberschatz, Galvin and Gagne 2013
Chapter 7: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock 7.2 Silberschatz, Galvin and Gagne 2013
Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks To present a number of different methods for preventing or avoiding deadlocks in a computer system 7.3 Silberschatz, Galvin and Gagne 2013
An Example 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. A law passed by the Kansas legislature early in the 20 th century. 7.4 Silberschatz, Galvin and Gagne 2013
System Model System consists of resources Resource types R 1, R 2,..., R m CPU cycles, memory space, I/O devices Each resource type R i has W i instances. Synchronization tools, such as mutex locks and semaphores are also considered system resources. Each process utilizes a resource as follows: request use release 7.5 Silberschatz, Galvin and Gagne 2013
Deadlock with Mutex Locks Deadlocks can occur via system calls, locking, etc. The locking tools (e.g., mutex locks, semaphores) are designed to avoid race conditions. However, inappropriate usage of them can lead to deadlocks. 7.6 Silberschatz, Galvin and Gagne 2013
Example Deadlock two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes Let S and Q be two semaphores initialized to 1 P 0 P 1 wait(s); wait(q); wait(q); wait(s);...... signal(s); signal(q); signal(q); signal(s); 7.7 Silberschatz, Galvin and Gagne 2013
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 {P 0, P 1,, P n } 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 n is waiting for a resource that is held by P 0. 7.8 Silberschatz, Galvin and Gagne 2013
Resource-Allocation Graph A set of vertices V and a set of edges E. V is partitioned into two types: P = {P 1, P 2,, P n }, the set consisting of all the processes in the system R = {R 1, R 2,, R m }, the set consisting of all resource types in the system request edge directed edge P i R j assignment edge directed edge R j P i 7.9 Silberschatz, Galvin and Gagne 2013
Resource-Allocation Graph (Cont.) Process Resource Type with 4 instances P i requests instance of R j P i P i is holding an instance of R j R j P i R j 7.10 Silberschatz, Galvin and Gagne 2013
Example of a Resource Allocation Graph Do we have a deadlock here? It can be shown that if the graph contains no cycles, then no process in the system is deadlocked. If the graph does contain a cycle, then a deadlock may exist. 7.11 Silberschatz, Galvin and Gagne 2013
Resource Allocation Graph With A Deadlock 7.12 Silberschatz, Galvin and Gagne 2013
Graph With A Cycle But No Deadlock 7.13 Silberschatz, Galvin and Gagne 2013
Basic Facts 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 Draw a resource-allocation graph to illustrate the deadlock problem on slide 7. Draw a resource-allocation graph for the dinning philosophers problem. 7.14 Silberschatz, Galvin and Gagne 2013
Methods for Handling Deadlocks Ensure that the system will never enter a deadlock state: Deadlock prevention Deadlock avoidence Allow the system to enter a deadlock state and then recover Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX 7.15 Silberschatz, Galvin and Gagne 2013
Deadlock Prevention Restrain the ways request can be made Mutual Exclusion not required for sharable resources (e.g., read-only files); must hold for non-sharable resources Hold and Wait must guarantee that whenever a process requests a resource, it does not hold any other resources Method 1: Require process to request and be allocated all its resources before it begins execution, Method 2: allow process to request resources only when the process has none allocated to it. Consider a process that copies data from a DVD driver to a file on disk, sorts the file, and then prints the results to a printer. Method 1: request all three resources in the beginning. Method 2: request DVD and file first; release both DVD and file; and then request file and printer. Low resource utilization; starvation possible (a process that needs several popular resources may have to wait indefinitely). 7.16 Silberschatz, Galvin and Gagne 2013
Deadlock Prevention (Cont.) 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 Preempted resources are added to the list of resources for which the process is waiting Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting 7.17 Silberschatz, Galvin and Gagne 2013
Deadlock Prevention (Cont.) Circular Wait impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration A process can initially request any number of instances of a resource type. After that, the process can request instances of resource type if and only if the order of the new resource is greater than the existing resource. Alternatively, we can require that a process requesting an instance of resource type must have released any resources that have smaller order numbers. Using this idea to solve the dinning philosopher problem 7.18 Silberschatz, Galvin and Gagne 2013
Deadlock Example /* 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) { } pthread_mutex_lock(&second_mutex); pthread_mutex_lock(&first_mutex); /** * Do some work */ pthread_mutex_unlock(&first_mutex); pthread_mutex_unlock(&second_mutex); pthread_exit(0); 7.19 Silberschatz, Galvin and Gagne 2013
Deadlock Example with Lock Ordering void transaction(account from, Account to, double amount) { } mutex lock1, lock2; lock1 = get_lock(from); lock2 = get_lock(to); acquire(lock1); acquire(lock2); withdraw(from, amount); deposit(to, amount); release(lock2); release(lock1); Transactions 1 and 2 execute concurrently. Transaction 1 transfers $25 from account A to account B, and Transaction 2 transfers $50 from account B to account A Reason: the locks are acquired dynamically. 7.20 Silberschatz, Galvin and Gagne 2013
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 7.21 Silberschatz, Galvin and Gagne 2013
Deadlock Detection Allow system to enter deadlock state Detection algorithm Recovery scheme 7.22 Silberschatz, Galvin and Gagne 2013
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? 1. Priority of the process 2. How long process has computed, and how much longer to completion 3. Resources the process has used 4. Resources process needs to complete 5. How many processes will need to be terminated 6. Is process interactive or batch? 7.23 Silberschatz, Galvin and Gagne 2013
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 7.24 Silberschatz, Galvin and Gagne 2013
End of Chapter 7 Silberschatz, Galvin and Gagne 2013