Assignment 2

ECSE 420: Parallel Computing

Submission instructions: Students are to work in groups of two on the assignment.

Students must also submit a pdf file that contains the following information: student’s names,

student ids, instructions on how to run each file and the associated question solved. Students

are also expected to submit a zip file containing their source code. This code must compile and

run without error. Code must be well formatted, commented, and follow the google java style

guide. For more information, see the ECSE420AssignmentSubmissionInstructions.pdf in

the Assignment section on myCourses.

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 Chapter 2 of the course text; do they

still satisfy two-thread mutual exclusion if the shared atomic registers – “flag” in LockOne and

“victim” in LockTwo – are replaced by regular registers?

3. (12 marks) Consider the protocol shown in Fig. 1 which is supposed 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.

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 exercise 3

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)

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 none of x and 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 Boolean MRSW register. Justify your answer.

Fig. 5 The regular M-valued MRSW class

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

## Reviews

There are no reviews yet.