## Description

Questions

1. (24 marks) This question will examine the concept of Locks.

1.1. Implement the Filter lock described in Chapter 2 of the course text.

1.2. Does the Filter lock allow some threads to overtake others an arbitrary number of times?

Explain.

1.3. Implement Lamport’s Bakery lock also described in Chapter 2.

1.4. Does the Bakery lock allow some threads to overtake others an arbitrary number of times?

Explain

1.5. Propose a test that verifies if a lock works, i.e., if it provides mutual exclusion.

1.6. Provide an implementation for the proposed test and verify if the implemented locks do

provide mutual exclusion.

2. (8 marks) Consider LockOne and LockTwo introduced in the text book; do they still satisfy twothread mutual exclusion if the shared atomic registers – “flag” in LockOne and “victim” in LockTwo

– are replaced by regular registers?

3. (12 marks) Programmers at the Flaky Computer Corporation designed the protocol shown in

Fig. 1 to achieve n-thread mutual exclusion. For each question, either sketch a proof, or display

an execution where it fails.

3.1. Does this protocol satisfy mutual exclusion? (Hint: Start the proof by assuming that two

threads A and B are in the critical section at the same time.)

3.2. Is this protocol deadlock-free? Explain.

3.3. Is this protocol starvation-free? Explain.

Page 2 of 4

1 class LockThree implements Lock {

2 private int turn;

3 private boolean busy = false;

4 public void lock() {

5 int me = ThreadID.get();

6 turn = me;

7 do {

8 busy = true;

9 } while ( turn = me || busy);

10 }

11 public void unlock() {

12 Busy = false;

13 }

14 }

Fig.1 LockThree Lock used in Question 2

4. (12 marks) For each of the histories shown in Figs. 2 and 3, are they sequentially consistent?

Linearizable? Justify your answer.

Fig. 2 History (a)

Fig. 3 History (b)

Page 3 of 4

5. (8 marks) Consider the class shown in Fig. 4. Suppose two threads A and B are concurrently

calling the methods writer and reader.

5.1. According to what you have been told about the Java memory model, will the reader method

ever divide by zero? If yes, describe the order in which writer and reader should be invoked

(by threads A and B) and take effect for a division by zero to happen.

5.2. Is division by zero possible if both x and v are volatile? What happens if neither x nor v are

volatile? Justify your answer.

Fig. 4 Volatile example

6. (8 marks) Consider the regular M-valued MRSW construction shown in Fig. 5; True or false:

6.1. If we change the loop at line 11 to “for (int i = x + 1; i < RANGE; i++)”, then the construction

is still a regular M-valued MRSW register. Justify your answer.

6.2. If we change the loop at line 11 to “for (int i = x + 1; i < RANGE; i++)”, then the construction

yields a safe M-valued MRSW register. Justify your answer.

Fig. 5 The regular M-valued MRSW class

Page 4 of 4

7. (4 marks) Show that if binary consensus using atomic registers is impossible for two threads, then

it is also impossible for n threads, where n > 2. (Hint: argue by reduction: if we had a protocol to

solve binary consensus for n threads, then we can transform it into a two-thread protocol.)

8. (4 marks) Show that if binary consensus using atomic registers is impossible for n threads, then

so is consensus over k values, where k > 2.

Total: 80 marks