CS372H Spring 2011 Homework 5 Solutions

Problem 1

Consider the following program fragment:

P(s1);
a++;
P(s2);
v++;
V(s2);
V(s1);

(s1, s2 are semaphores). All variables are automatic. Now, consider two threads running this fragment of code simultaneously, can there be a deadlock? Why, or why not?

Solution:

Both semaphores will be acquired in the same order by both threads. Since this is a linear order, there can be no situation where a deadlock could occur.

Problem 2

Consider the following program fragment:

if(a > 0)
    P(s1);
else
    P(s2);
b++;
P(s3);
if(b < 0 && a <= 0)
    P(s1);
else if(b >= 0 && a > 0)
    P(s2);
else
    P(s4);
a++;
V(s4);
V(s3);
V(s2);
V(s1);

s1, s2, s3 and s4 are semaphores. All variables are automatic. Now, consider two threads running this fragment of code simultaneously, can there be a deadlock? Why, or why not?

Solution:

Yes, there can be a deadlock. Consider the scenario where thread 1 starts by locking semaphore s1 then gets switched out while thread 2 runs. Thread 2 may start running with variable a set to 0 (remember, because the variables are automatic, each thread has its own independent set). Thread 2 will acquire semaphore s2, then proceeds to acquire s3. Then, if (b<0 && a <=0), it will try to acquire s1. This forces thread 2 to wait for thread 1 which holds this semaphore. Now, thread 1 may proceed but will block when it tries to acquire semaphore s3. This forms a cyclical wait, and a deadlock occurs.

Problem 3

True/False Starvation implies deadlock.

Solution:
False.

Problem 4

In the context of deadlock prevention, how does a safe state differ from an unsafe state?

Solution:
In a safe state there exists a safe sequence -- an ordered sequence of resource grants that would allow each process that currently holds any resources to acquire that process's maximum resource allocation. In an unsafe state, no such sequence exists. Thus, an OS can always prevent deadlock when a system is in a safe state by only granting resources that keep the system in a safe state. Conversely, if the system is in an unsafe state, there exists at least one sequence of requests that will deadlock the system regardless of what the OS does.

Problem 5

Define the term priority inversion.

Solution:
Priority inversion is a program bug in which a high priority thread waits for a lock that is held by a low priority thread and the low priority thread is unable to release the lock because a medium priority thread is using the CPU (or other required resources). In effect, the high priority thread is waiting for the medium priority thread.