Question
Consider the following two threads T1 and T2 that update two shared variables a and b. Assume that initially a = b = 1. Though context switching between threads can happen at any time, each statement of T1 or T2 is executed atomically without interruption.
| T1 | T2 |
|---|---|
| a = a + 1; | b = 2 * b; |
| b = b + 1; | a = 2 * a; |
Which one of the following options lists all the possible combinations of values of a and b after both T1 and T2 finish execution?
Official answer and solution
- Official answer
- A
- From
- Official GATE 2024 answer key
Solution
Label the four atomic statements (in T1) and (in T2) and evaluate all valid topological interleavings preserving and .
Let T1 consist of a = a + 1; followed by b = b + 1;, and T2 consist of b = 2 * b; followed by a = 2 * a;.
- For variable
a(initially ): if , then ; if , then . - For variable
b(initially ): if , then ; if , then .
Notice that requires both and . Combined with the thread program orders and , this would create a cycle , which is impossible! The other three outcomes are all realizable:
Option (A)
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2024 Computer Science and Information Technology - Set 1 (CS1)
- Question
- 40 of 65
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.