CS FundamentalsVery hardGATE CS · Advanced

Two threads share an integer x initialised to 0. Thread 1 executes the loop body x = x + 1 three times; Thread 2 executes it twice. Each execution of the body compiles to three separate steps: load x into a private register, add 1 to the register, store the register back into x. With arbitrary interleavings of these steps, what is the smallest final value of x that can occur?

  1. A. 5
  2. B. 1
  3. C. 2
  4. D. 3

Correct answer: C. 2

Lost updates can wipe out almost all work: the minimum is 2. Thread 1 loads 0 and pauses; thread 2 completes one iteration (x = 1); thread 1 stores 1; thread 2 loads 1 and pauses; thread 1 finishes its other two iterations (x = 3); thread 2 stores 2. All reachable finals: [2, 3, 4, 5].

More Operating Systems questions

Exams that ask this topic