CS FundamentalsVery hardAmazon · Microsoft MCQ pattern
Two threads each run the loop `for (i = 0; i < 5; i++) count++;` on a shared int count = 0, with no synchronisation. count++ is compiled to load, add, store. What is the MINIMUM possible final value of count?
- A. 5
- B. 0
- C. 1
- D. 2
Correct answer: D. 2
Interleaving: T1 loads 0; T2 runs 4 full iterations (count = 4); T1 stores 1; T2 loads 1; T1 runs its remaining 4 iterations; T2 stores 2. Final = 2. It cannot be lower: the last store comes from some thread's 5th iteration, whose load happens after that thread's own 4th store, so it reads at least 1 and writes at least 2.
More Operating Systems questions
- How many times is "x" printed? (fork() returns 0 in the child and a positive PID in the parent; assume it always succeeds.) c int main() {…
- How many times is "x" printed? (Assume every fork() succeeds.) c int main() { fork(); fork() || fork(); printf("xn"); return 0; }
- How many lines are printed in total? (Assume every fork() succeeds.) c int main() { for (int i = 0; i < 3; i++) { if (fork() == 0)…
- A 64-bit machine uses 48-bit virtual addresses, 4 KB pages and 8-byte page-table entries. Every page table occupies exactly one page. How…
- A system uses two-level paging and a TLB. TLB access = 10 ns, memory access = 100 ns, TLB hit ratio = 90%. The TLB is searched first, and…
- TLB access = 20 ns, memory access = 100 ns, single-level page table, TLB searched first. What is the minimum TLB hit ratio for the…