Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For the multi-threaded program below, what are all the valid outputs (printed va

ID: 3698037 • Letter: F

Question

For the multi-threaded program below, what are all the valid outputs (printed value of A) for a sequentially consistent execution of the program? Before these threads start executing, A and B are both initialized to zero. Assume that each line of code below corresponds to one assembly instruction.

Thread 1

B = 10;

A = 30;

Thread 2

if (B-A > 5) then A = A + B;

print A;

What are possible valid outputs if the code in each thread is encapsulated within lock and unlock statements as shown below (again assuming a sequentially consistent processor)?

Thread 1

lock(L1);

B = 10;

A = 30;

unlock(L1);

Thread 2 lock(L1);

if (B-A > 5) then A = A + B;

print A;

unlock(L1);

Explanation / Answer

Since we are assuming a sequentially consistent processor, so at a time only one thread will execute

Case 1:

IF lock L1 is picked by Thread 1 first:

THREAD 1: A = 30, B = 10

THREAD 2: (B-A) is not > 5 so, print A   

// OUTPUT: 30

Case 2:

  IF lock L1 is picked by Thread 2 first:

A = 0; B = 0;

B-A is not > 5

print A

OUTPUT: 0