GATE / CS ARCHIVE

SIMILAR QUESTIONS / OPERATING SYSTEMS

Construct a test and set spinlock

2 questions solved the same way, newest first. Back to Operating Systems

How these are solved

Atomic test-and-set returns the previous flag while setting it. Spin while the old value is occupied and clear the flag on exit.

Report issue

Test-and-set lock entry and exit

Synchronization & Concurrency

Question 18(b)(i), 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)(i): Fill the while condition in enter_cs(){while(blank);} and the statement in leave_cs(){blank;} using this instruction.
Question 18(b)(i), 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)(i): Fill the while condition in enter_cs(){while(blank);} and the statement in leave_cs(){blank;} using this instruction.
Question 18(b)(i), 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)(i): Fill the while condition in enter_cs(){while(blank);} and the statement in leave_cs(){blank;} using this instruction.
Question 18(b)(i), 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)(i): Fill the while condition in enter_cs(){while(blank);} and the statement in leave_cs(){blank;} using this instruction.
Tap to enlarge
Answer & solution

Answer: enter_cs: while (TEST_AND_SET(&mutex)) { } ; leave_cs: mutex=0; (no official key was published)

Use the shared lock variable mutex, initially zero:
void enter_cs(void) { while (TEST_AND_SET(&mutex)) { } }
void leave_cs(void) { mutex = 0; }
TEST_AND_SET atomically returns the old value and writes one. Exactly one contender observing the unlocked zero receives zero and leaves the loop; later contenders receive one and keep spinning. Releasing the lock restores zero for a future acquisition. This is pseudocode for the given atomic primitive and a shared lock, not a claim that an ordinary non-atomic C integer and a handwritten function implement modern concurrent-C synchronization. The source's hyphenated function names are rendered with underscores to make the names valid C identifiers.

Original paper, page 9

2 marks · Descriptive
Report issue

Implement a test-and-set spinlock

Synchronization & Concurrency

Question 20(a), GATE 1999: TSET register,flag atomically copies flag into register and sets flag=1. Give entry and exit pseudocode for a critical region.
Question 20(a), GATE 1999: TSET register,flag atomically copies flag into register and sets flag=1. Give entry and exit pseudocode for a critical region.
Tap to enlarge
Answer & solution

Answer: Spin until TSET returns 0; release by setting flag to 0. (no official key was published)

Initialize a shared flag to 0 before any competing process starts. Each process uses its own register r.

shared flag := 0

entry:
  repeat
    TSET r, flag
  until r = 0

critical_region()

exit:
  flag := 0

TSET must run on every retry; reading a stale r in a spin loop would never acquire the lock. When flag=0, exactly one atomic TSET can observe that 0 and simultaneously change flag to 1. That process enters; all later tests obtain 1 and continue waiting. Only the owner clears the flag on exit. This establishes mutual exclusion, assuming atomic release stores and the abstract sequentially consistent memory model of the question. Real machines additionally require appropriate acquire/release memory ordering. The scheme busy-waits and does not by itself guarantee bounded waiting or freedom from starvation.

Original paper, page 9