CS FundamentalsVery hardGATE CS
compare_and_swap(&v, expected, new) atomically sets v = new only if v == expected and returns the old value. A lock-free increment is written as `do { old = v; } while (CAS(&v, old, old + 1) != old);`. Which problem can affect CAS-based algorithms on pointers (e.g. a lock-free stack)?
- A. Deadlock between two CAS loops
- B. Priority inversion caused by CAS
- C. The ABA problem: a value changes from A to B and back to A, so CAS wrongly succeeds
- D. CAS cannot be implemented on multiprocessors
Correct answer: C. The ABA problem: a value changes from A to B and back to A, so CAS wrongly succeeds
CAS only checks equality, not history. If a node is popped, freed and a new node is allocated at the same address, a stale CAS succeeds and corrupts the structure; tagged pointers or hazard pointers fix it.
More Operating Systems questions
- Which condition makes disabling interrupts an INSUFFICIENT way to implement a critical section?
- Two threads run on different cores. Thread 1: data = 42; ready = 1; Thread 2: while (!ready); print(data); Both variables are plain…
- How many of these statements about the critical-section problem are correct? 1. Progress means only processes not in their remainder…
- In Lamport's bakery algorithm, why does a process pick a number with number[i] = max(number) + 1 while choosing[i] is true, and break ties…
- A barrier for N threads is implemented with count (protected by mutex) and a semaphore barrier = 0: each thread does lock; count++; if…
- Processes (arrival, burst): P1 (0, 12), P2 (2, 4), P3 (3, 6), P4 (8, 5), P5 (10, 2). Under SRTF, which process waits the longest, and for…