

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A part of the lecture notes for cmpsci 377 operating systems course, fall 2008. The twelfth lecture covers the topic of deadlocks, including their definition, conditions for occurrence, detection, and prevention. The lecture also discusses resource reservation and increasing concurrency to prevent deadlocks.
Typology: Papers
1 / 3
This page cannot be seen from the preview
Don't miss anything!


CMPSCI 377 Operating Systems Fall 2008
Lecturer: Prashant Shenoy Scribe: Shashi Singh
A deadlock is a situation wherein two or more competing actions are waiting for the other to finish, and thus neither ever does. It is a logical error that can occur when programming with threads. A deadlock (also known as “the deadly embrace”) happens when two things (threads, processes, etc) wait on each other. For example:
thread A printer.wait disk.wait
thread B disk.wait print.wait
Yet another example of a deadlock is known as the Dining Philosophers problem. In this abstract problem, philosophers alter between thinking and eating. Each philosopher needs two forks to eat with. The problem is that each philosopher only gets one fork at a time; if one philosopher gets one of the forks, the next of gets the other, etc, in a circular way, we get a deadlock. The philosophers will starve! One of the main aspects to notice about this problem is that we must necessarily have threads competing for a finite number of resources; if we have had an infinite amount of forks, there would be no deadlock. We now enumerate the conditions needed for a deadlock to occurs; notice that all of them are necessary, and none is sufficient:
Deadlock can only occur in systems where all 4 conditions hold true.
Deadlocks can be detected on-the-fly, by running cycle detection algorithms on the graph that defines the current use of resources. Let the graph being discussed have one vertex for each resources (r 1... rm) and one for each thread (t 1... tn). We say that there is an edge from a thread to a resource if that thread is using
12-2 Lecture 12: October 9
that resource; if there is an edge from a resource to a thread, that resource is owned by the thread. Given this graph, we can run any cycle detection algorithm. If a cycle is found, we have a deadlock; we might then either kill all threads in the cycle, or kill threads one at a time (thus forcing them to give up resourcers) and hope that we will need to kill few threads before the deadlock is resolved.
Preventing deadlocks is fairly easy. Remember that the list presented before enumerates conditions which are all necessary for a deadlock to occur; therefore, it suffices that at least one of those conditions does not hold.
Threads provide advance information about the maximum resources they may need during execution. A sequence of threads t 1 , ..., tn is safe if for each ti, the resources that ti can still request can be satisfied by the currently available resources plus the resources held by all tj , j < i. A safe state is a state in which there is a safe sequence for the threads. An unsafe state is not equivalent to deadlock, it just may lead to deadlock, since some threads might not actually use the maximum resources they have declared. We grant a resource to a thread only if the resulting new state is safe. If the new state is unsafe, the thread must wait even if the resource is currently available. This alagorithm ensures no circular-wait condition exist, and hence no deadlock.