10 points per answer for 1, 4ac, 6ab, 15 points per answer for 2ab and 30 points each for
questions 3, 5 (150 max). Due Wednesday February 22.
Shortanswer questions (just a sentence or two each,please).
1. In class, we saw that an atomic
test
and
set
operation can be used to implement critical sections on
a machine supporting parallelism (with multiple physical CPUs). But suppose that you were given a
diﬀerent atomic instruction called
decrement
. This instruction decrements a variable and leaves the
initial value in register 0. Could
decrement
be used to implement critical sections? Show us how or
explain why not.
Solution
Yes. We can try and mimic
test
and
set
with decrement.
free = 1; (initially)
CSEnter:
while(decrement(free)
<
=0)
{
continue;
}
CSExit:
free= 1;
This gives us mutual exclusion and progress. We can extend this solution using similar constructs as
we did in
test
and
set
(the waiting[] variables), to ensure bounded waiting.
Comments
Many students gave solutions assuming atomic
clear(). It is possible to have bakery style
algorithms without any atomic instructions. If we use busy waiting like in the solution above, there is
an overﬂow problem since the value of free could overﬂow and wrap back to 0.
2. Consider the Nprocess Bakery Algorithm:
CSEnter(i):
chosing[i] = true;
number[i] = max(number[0], .
.., number[N1])+1;
chosing[i] = false;
for(j = 0; j
<
N; j++)
{
while(chosing[j]) continue;
while(number[j]
>
<
(number[i],i)) continue;
}
CSExit(i):
number[i] = 0;
(a) Suppose that process 2 is trying to enter the critical section and has already picked a number
value and set chosing[2] to false.
. In a ”worst case scenario”, how many times could process 0
”sneak in” before process 2 gets its turn? Explain.
Solution
The following solution interprets “sneaking in” as “getting to enter the Critical Section”.
If it is interpreted as “entering CSEnter and then getting to enter the Critical Section”, the answers
would change(reduce by 1 each). Since this is a worstcase scenario, assume process 0 (P0) has a
lower id than does process 2 (P2).
If P0 is now inside CSEnter, and has already picked up a lower number(or the same number), P0
would get to go before P2. In any other case, P2 gets to go earlier. So the answer here is 1.
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '06
 BIRMAN
 CSEnter

Click to edit the document details