Question: Consider a program P that contains two threads of execution. In a simplistic way, it means that two routines Thread1 and Thread2 in Program P
Consider a program P that contains two threads of execution. In a simplistic way, it means that two routines Thread1 and Thread2 in Program P can be running concurrently. The integer variable Turn is initialized to 0 by Program P. The variable Turn is shared by the threads Thread1 and Thread2. Below are the codes for Thread1 and Thread2.
| Thread 1 | Thread 2 |
| while (1) { while (Turn == 0) ; //loop here while Turn == 0 Code A Turn = 0; } | while (1) { while (Turn == 1) ; //loop here while Turn == 1 Code B Turn = 1; } |
Code A and Code B are blocks of multiple instructions.
Pay attention: Code A and Code B are not part of the while (Turn ==..) loops. For example, if (Turn == 1) for Thread 2, this while loop keeps looping and Code B will not run unless the variable Turn becomes 0.
We assume that Code A and Code B do not modify the variable Turn. Answer the following questions:
- When instructions of Code A are running, what is the value of Turn?
- When instructions of Code B are running, what is the value of Turn?
- Can Code A and Code B be running simultaneously? Answer and justify your answer
- Use a proof by contradiction to show that Code A and Code B CANNOT be running simultaneously. For this proof, I suggest to follow these steps:
- What will be your starting assumption?
- Can you infer from this assumption some contradiction?
- After you show the contradiction, what will be your conclusion?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
