GATE / CS ARCHIVE

SIMILAR QUESTIONS / OPERATING SYSTEMS

Assess test and set spinlock guarantees

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

How these are solved

A standard atomic TAS spinlock ensures exclusion and system-wide progress when released, but does not bound an individual contender or enforce FIFO. Preserve scheduling/fairness qualifications; excludes disabled-context-switch spinning and the faulty fetch-add release race.

2 marks · MCQ
Report issue

Test-and-set spinlock guarantees

Synchronization & Concurrency

Question 33, GATE 2009: Shared lock X starts 0. enter_CS spins while atomic test-and-set(X) returns true; leave_CS writes X=0. Assess I deadlock-free; II starvation-free; III FIFO entry; IV multiple simultaneous entrants. A I only; B I/II; C II/III; D IV only.
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Atomic test-and-set allows exactly one contender to observe 0 and enter, preserving mutual exclusion. When the current holder eventually leaves and resets X, some contender can acquire it, so the lock is deadlock-free under ordinary progress assumptions. There is no FIFO order or bounded-wait guarantee: the same process can repeatedly win and starve another. Only I holds, A. X denotes shared lock storage in this pseudocode, not an ordinary pass-by-value C local.

The atomic operation returns the old flag and sets it to 1 as one indivisible action. Only one contender can see old 0, so IV is false. Once the holder releases, some waiting process can succeed, assuming the holder and scheduler make ordinary progress; this supplies deadlock freedom. But no queue orders contenders. A newly arriving or repeatedly scheduled process can keep winning before another waiter, defeating both FIFO and starvation freedom. Therefore only I is justified.

Original paper, page 8

Report issue

Progress guarantees of a test-and-set spinlock

Synchronization & Concurrency

Question 18(b)(ii), 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)(ii): For the resulting spinlock entry/exit solution, determine whether it is deadlock-free and starvation-free.
Question 18(b)(ii), 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)(ii): For the resulting spinlock entry/exit solution, determine whether it is deadlock-free and starvation-free.
Question 18(b)(ii), 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)(ii): For the resulting spinlock entry/exit solution, determine whether it is deadlock-free and starvation-free.
Question 18(b)(ii), 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)(ii): For the resulting spinlock entry/exit solution, determine whether it is deadlock-free and starvation-free.
Question 18(b)(ii), 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)(ii): For the resulting spinlock entry/exit solution, determine whether it is deadlock-free and starvation-free.
Tap to enlarge
Answer & solution

Answer: Deadlock-free under the usual progress assumptions; not starvation-free. (no official key was published)

With atomic TEST_AND_SET, one contender acquires any free lock, and a holder that finishes its finite critical section releases the lock. Under the usual assumptions that a holder continues to receive execution time and eventually exits, the lock is deadlock-free: processes do not remain mutually blocked with no one able to proceed. It is not starvation-free, since a waiting process is not assigned a turn. Each time the lock is released another contender can acquire it first, so one process can lose indefinitely even while other processes keep making progress. As with any spinlock, a scheduler that prevents the holder from running can itself defeat progress; deadlock-freedom is not an unconditional scheduling guarantee.

Original paper, page 9