UNIVERSITY OF WISCONSIN-MADISON
Computer Sciences Department
CS 537
Spring 2001
A. Arpaci-Dusseau
Quiz #5: March 21st -- Deadlock
Name: Student ID #:

Problem 1: Conditions for Deadlock (35 points)

There are four conditions that are necessary for deadlock to occur: mutual exclusion, hold and wait, no preemption, and circular wait. With deadlock prevention, the system ensures that deadlock does not occur by preventing one of these conditions from holding. Match each of the following techniques with the one deadlock condition that it prevents.

a.  Mutual exclusion
b.  Hold and wait
c.  No preemption
d.  Circular wait

_d_ Impose a total ordering (or ranking) on how resources are acquired

_c_ When a process requests a resource that is already held, 
      force the process holding the resource to release it

_b_ Only allow a process to request a resource when the process has none

_a_ Allow all processes to access the resource simultaneously

_b_ Require each process to grab all desired resources at once

Problem 2: Existance of Deadlock (45 points)

Can running each of the following code segments lead to deadlock of process 0 and process 1? Simply answer "yes" or "no".

 Code Sample 1

Process 0:                Process 1:


lock1.acquire();          lock1.acquire();
lock2.acquire();          lock2.acquire();
lock1.release();          lock1.release();
lock2.release();          lock2.release();

No deadlock possible, because there is no circularity of requests. Both processes grab the locks in the same order.
 Code Sample 2

Process 0:                Process 1:


lock1.acquire();          lock2.acquire();
lock2.acquire();          lock1.acquire();
lock1.release();          lock1.release();
lock2.release();          lock2.release();

Deadlock is possible, because there is circularity. If process 0 acquires lock1 and then process 1 acquires lock2, both will be stuck holding one lock while waiting for the other.
 Code Sample 3

Process 0:                Process 1:


lock1.acquire();          lock2.acquire();
lock2.acquire();          lock2.release();
lock1.release();          lock1.acquire();
lock2.release();          lock1.release();

No deadlock possible, because process 1 does not hold and wait. Even if process 0 acquires lock1 and process 1 acquires lock2, eventually process 1 will release lock2, at which point process 0 will be able to acquire lock 2.

Problem 3: Resource-Allocation Graph (20 points)

Running which of the above code samples could lead to the situation shown in the following resource-allocation graph? Indicate all of the possible code samples that apply.

Resource allocation graph

Possible with Code Sample 1 given the following execution:

Process 0:                Process 1:

lock1.acquire();          
lock2.acquire();          
                          lock1.acquire(); // waits!

Not possible with Code Sample 2; if Process 1 is waiting for lock1, it must hold lock2, which isn't shown in the figure.

Possible with Code Sample 3 given the following execution:

Process 0:                Process 1:

                          lock2.acquire();
                          lock2.release();
lock1.acquire();          
lock2.acquire();          
                          lock1.acquire(); // waits!