Home › GATE CSE › computerscience › Operating Systems › The COMPARE-AND-SWAP (CAS) instruction is more p…
The COMPARE-AND-SWAP (CAS) instruction is more powerful than $TAS$ because it
Awrites only if current value matches expected
Balways succeeds in writing
Cdoesn't require any hardware support
Dblocks indefinitely on contention
Answer & Solution
Correct answer: A. writes only if current value matches expected
1. OSTEP §28.10 describes CAS: $CAS(\&L, expected, new)$:
- Reads the current value at $\&L$.
- If it equals $expected$, writes $new$ and returns TRUE.
- Otherwise leaves it unchanged and returns FALSE.
2. This conditional swap enables LOCK-FREE programming: many threads can attempt CAS, and only one will succeed at a time (whoever's $expected$ matches).
3. CAS is the foundation for lock-free data structures (queues, stacks, hash tables) — used widely in Linux kernel and JVM.
4. Options B, C, D misdescribe CAS's behavior.
_Source: OSTEP Ch 28, §28.10 (Compare-And-Swap), p. 7-8._
Related questions
On a MULTI-CORE system, a SPIN LOCK is generallyDisabling INTERRUPTS as a way to implement mutual exclusion on a single-CPU system isA LIVELOCK differs from a DEADLOCK in thatSuppose thread A acquires lock L1, then thread B acquires lock L2. Now A tries to acquire Without atomic hardware support, building a correct mutual-exclusion lock for $N \geq 2$ tA CRITICAL SECTION isLinux's `pthread_mutex_lock()` is typically implemented as a HYBRID:What is the PRIMARY problem with using a SPIN LOCK on a SINGLE-CPU uniprocessor system (wi