CS FundamentalsHardGATE CS
A spinlock is held for about 50 ns on average, and a context switch costs about 5 µs. On a multicore machine, which is better for a thread that finds the lock busy?
- A. Block immediately, because spinning always wastes CPU
- B. Spin, because the expected wait is far shorter than two context switches
- C. Disable interrupts on all cores
- D. Spin only on a single-core machine
Correct answer: B. Spin, because the expected wait is far shorter than two context switches
Blocking costs at least a switch out and a switch back (~10 µs), much more than a 50 ns wait. On a single core spinning is useless, because the holder cannot run while the waiter spins.
More Operating Systems questions
- compareandswap(&v, expected, new) atomically sets v = new only if v == expected and returns the old value. A lock-free increment is…
- 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…