CS FundamentalsModerateGATE CS
Shared x = 0. Three threads each execute `x = x + 1;` once (load, add, store; no locking). What is the set of possible final values?
- A. {3}
- B. {0, 1, 2, 3}
- C. {2, 3}
- D. {1, 2, 3}
Correct answer: D. {1, 2, 3}
Each thread's store writes (value it loaded) + 1. If all three load 0 before any store, the result is 1; serial execution gives 3; partial overlap gives 2. 0 is impossible since every thread stores at least 1.
More Operating Systems questions
- 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…
- 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…