Practice free →
HomeGATE CSEcomputerscienceOperating Systems › Linux's `pthread_mutex_lock()` is typically impl…

Linux's `pthread_mutex_lock()` is typically implemented as a HYBRID:

Apure spin only
Bpure blocking only
Cspin briefly, then sleep if still contended
Drandomly choose between spin and block per call
Answer & Solution
Correct answer: C. spin briefly, then sleep if still contended
1. OSTEP §28.13 describes the modern Linux approach (the FUTEX system call). 2. The idea: short critical sections benefit from spinning (cheap); long ones benefit from blocking (saves CPU). 3. HYBRID: try to acquire with TAS/CAS. If unsuccessful, spin briefly (often $\sim 50$-$100$ iterations). If still unsuccessful, call into the kernel (FUTEX_WAIT) to block the thread until the lock holder calls FUTEX_WAKE. 4. This gives the best of both worlds: low overhead for short contention, no CPU waste for long contention. 5. Options A, B, D describe simpler (and worse) strategies. _Source: OSTEP Ch 28, §28.13 (Using Queues: Sleeping vs Spinning), p. 8-10 + §28.14 FUTEX, p. 10._
Solve this in the app — GATE CSE practice & 24k+ MCQs →
Related questions