CMU 15-671Models of Software SystemsFall 1995
Shared Resources
Garlan & Wing Homework 9 Due: 1 November 1995
The first problem is very easy. The second problem tests whether you've been keeping up with the reading. Read it now and start thinking about it early!
Problem 1.
Suppose P and Q are sequential processes that share the integer variable x, initialized to 0. P's code looks like:
x := 1;
x := x + 5
and Q's code looks like:
x := 2;
x := 2x
Suppose P and Q are run in parallel. Consider each assignment statement to be atomic.
a. Give all the possible interleavings of P's and Q's statements. What are all the possible final values of x?
b. Suppose each of P's and Q's code is atomic:
P's code:
<<x := 1;
x := x + 5>>
and Q's code:
<<x := 2;
x := 2x>>
Now what are the possible final values of x?
Problem 2.
A local bar has has only one bathroom and patrons are asked to obey the following rules of etiquette for using it:
Besides being a full-time student, you moonlight as the barkeeper. You just learned about mutexes and condition variables in 15-671 and decide to design a Unisex Bathroom Protocol that satisfies the above rules. In addition, you attempt to ensure that the following safety and liveness properties hold:
a. Write out pseudo-code (don't feel obligated to write Modula-3 code!) that implements your Unisex Bathroom Protocol. You may introduce any kind and as many auxiliary data structures (possibly protected by mutexes) as you want, e.g., queues of waiting ladies (gentlemen) (or a single queue of both sexes) and/or queues of exiting ladies (gentlemen). Stick to just mutexes and condition variables with Modula-3 semantics as your sychronization primitives. You may use the LOCK statement of Modula-3 if you find it convenient. Assume a strong fairness scheduler. For example, your solution may (but need not) have a general structure that looks like:
m: mutexgentsOut, ladiesOut: condition
ladiesWaiting, gentsWaiting: int
procedure LadyEnter();
begin
lock m DO
fill in this bit
end
end
procedure LadyExit();
begin
lock m DO
fill in this bit
end
end
(et cetera)
Hint: This problem is similar to the readers-writers problem except more than one ``writer'' may access the resource at the same time.
b. Argue informally that your solution satisfies the rules of etiquette.
c. Argue informally that the additional safety and liveness properties hold.
d. Can deadlock occur in your solution? Why or why not?
e. Does your solution allow maximal concurrency (e.g., if ladies (gentlemen) are
allowed
in then no ladies (gentlemen) are waiting outside)?