CS FundamentalsVery hardGATE CS
Shared int x = 0. Two threads each execute `x = x + 1;` TWICE, without synchronisation; each statement is a load into a private register, an add, and a store. What is the set of possible final values of x?
- A. {3, 4}
- B. {4}
- C. {2, 3, 4}
- D. {1, 2, 3, 4}
Correct answer: C. {2, 3, 4}
Final value 2 is possible: T1 loads 0, T2 runs its first increment (x = 1), T1 stores 1, T2 loads 1, T1 runs its second increment completely (x = 2), then T2 stores 2. A final value of 1 is impossible: the last store comes from a thread's second increment, whose load happens after that thread already stored at least 1. Enumerating all interleavings confirms {2, 3, 4}.
More Operating Systems questions
- Shared int x = 1. Thread T1 executes x = x 2; and T2 executes x = x + 3; concurrently with no locking (each is load, compute, store). How…
- Which property does Peterson's algorithm guarantee for two processes, assuming loads and stores are atomic and NOT reordered?
- A test-and-set spinlock: while (TestAndSet(&lock)); / CS / lock = false;. Which requirement of the critical-section problem does it NOT…
- A semaphore S is initialised to 3. The operations wait(S) ×6, signal(S) ×2, wait(S) ×1 are issued in that order by different processes…
- Binary semaphore mutex = 1 protects a counter. A programmer mistakenly writes signal(mutex); / critical section / wait(mutex); in every…
- In the dining philosophers problem with 5 philosophers, each picks up the LEFT fork and then the RIGHT fork. Which change by itself…