CS372H Spring 2011 Homework 5

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?

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?

Problem 3

True/False Starvation implies deadlock.

Problem 4

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

Problem 5

Define the term priority inversion.