GATE / CS ARCHIVE

GATE CSE 2002 / QUESTION 18(b)(iii)

Why non-atomic test-and-set fails mutual exclusion

Operating Systems · Synchronization & Concurrency · Full GATE CSE 2002 paper

GATE 2002, Q18(b)(iii) Descriptive
Report issue

Synchronization & Concurrency

Question 18(b)(iii), GATE 2002: Atomic TEST_AND_SET(int*x) performs A1:y=*x; A2:*x=1; A3:return y. Shared mutex initially 0. Entry/exit templates: void enter_cs(){while(blank);} void leave_cs(){blank;} Original18(b)(iii): If the three TEST_AND_SET steps were not atomic, give an interleaving violating mutual exclusion.
Question 18(b)(iii), GATE 2002: Atomic TEST_AND_SET(int*x) performs A1:y=*x; A2:*x=1; A3:return y. Shared mutex initially 0. Entry/exit templates: void enter_cs(){while(blank);} void leave_cs(){blank;} Original18(b)(iii): If the three TEST_AND_SET steps were not atomic, give an interleaving violating mutual exclusion.
Question 18(b)(iii), GATE 2002: Atomic TEST_AND_SET(int*x) performs A1:y=*x; A2:*x=1; A3:return y. Shared mutex initially 0. Entry/exit templates: void enter_cs(){while(blank);} void leave_cs(){blank;} Original18(b)(iii): If the three TEST_AND_SET steps were not atomic, give an interleaving violating mutual exclusion.
Question 18(b)(iii), GATE 2002: Atomic TEST_AND_SET(int*x) performs A1:y=*x; A2:*x=1; A3:return y. Shared mutex initially 0. Entry/exit templates: void enter_cs(){while(blank);} void leave_cs(){blank;} Original18(b)(iii): If the three TEST_AND_SET steps were not atomic, give an interleaving violating mutual exclusion.
Question 18(b)(iii), GATE 2002: Atomic TEST_AND_SET(int*x) performs A1:y=*x; A2:*x=1; A3:return y. Shared mutex initially 0. Entry/exit templates: void enter_cs(){while(blank);} void leave_cs(){blank;} Original18(b)(iii): If the three TEST_AND_SET steps were not atomic, give an interleaving violating mutual exclusion.
Tap to enlarge
Answer & solution

Answer: Both processes can read mutex=0 before either sets it, so both enter. (no official key was published)

Initially mutex=0. Split TEST_AND_SET into the source steps A1: y=*x; A2: *x=1; A3: return y. Interleave two processes as follows:
1. P executes A1 and stores local yP=0.
2. Q executes A1 and stores local yQ=0.
3. P executes A2, writing mutex=1.
4. Q executes A2, also writing mutex=1.
5. P executes A3 and returns its saved zero; its while condition is false and it enters the critical section.
6. Q executes A3 and returns its saved zero; it also enters before P leaves.
The final shared value one does not distinguish the two prior reads. Atomicity must cover the read and write as one indivisible operation.

Original paper, page 9