CS FundamentalsVery hardGATE CS
Shared x = 0. Thread A executes `x = x + 1;` twice and thread B executes `x = x - 1;` twice, with no synchronisation (each statement is load, compute, store). What are the minimum and maximum possible final values of x?
- A. −2 and 2
- B. 0 and 0
- C. −1 and 1
- D. −4 and 4
Correct answer: A. −2 and 2
Lost updates let one thread's effects vanish entirely: if B's final store overwrites everything A did, x can end at −2, and symmetrically at +2. Every value from −2 to 2 is reachable (verified by enumerating all interleavings).
More Operating Systems questions
- Semaphores a = b = c = 0. P1: print A; signal(a); wait(b); wait(c); print X. P2: print B; signal(b); signal(b); wait(a); print Y. P3…
- Semaphore s = 0. P1: print('a'); signal(s); print('b'). P2: wait(s); print('c'); print('d'). How many different output strings are possible?
- Binary semaphores S = Q = 1. P1: wait(S); wait(Q); print 1; signal(Q); signal(S). P2: wait(Q); wait(S); print 2; signal(S); signal(Q)…
- A producer runs repeat 3 { wait(empty); print('p'); signal(full); } and a consumer runs repeat 3 { wait(full); print('c'); signal(empty)…
- mutex m = 1. P1: wait(m); print A; print B; signal(m). P2: wait(m); print C; print D; signal(m). P3: print E (no synchronisation). How…
- Shared x = 0, mutex m = 1. T1: wait(m); x = x + 1; signal(m). T2: wait(m); x = x + 2; signal(m). T3: x = x + 4 (no lock). Each assignment…