CS FundamentalsVery hardProduct company interview · GATE level
Shared int x = 1. Thread T1 executes `x = x + 1;` twice; thread T2 executes `x = x * 2;` once. Each statement compiles to a load of x into a private register, the arithmetic, and a store back to x; only individual loads and stores are atomic. Which set contains exactly the possible final values of x?
- A. {4, 6}
- B. {2, 3, 4, 5, 6}
- C. {3, 4, 5, 6}
- D. {2, 3, 4, 5, 6, 8}
Correct answer: B. {2, 3, 4, 5, 6}
Serial orders give 6 (T2 first: 2, 3, 4), 5 (T2 between: 2, 4, 5) and 4 (T2 last: 3, 6). Lost updates add more. If T2 loads 1, T1 does both increments (x = 3) and T2 then stores 2, the result is 2. If T1's first increment loads 1, T2 runs completely (x = 2), and T1 stores 2 and increments to 3, the result is 3. Nothing can exceed 6, since that needs both increments before the doubling. So the set is {2, 3, 4, 5, 6}; 8 would need a value that was never computed.
More Operating Systems questions
- Semaphores a = 0 and b = 0. Three processes run concurrently: P: print('A'); signal(a); wait(b); print('B'); Q: wait(a); print('C')…
- Two processes (i = 0, 1; j = 1 - i) share bool flag[2] = {false, false} and int turn, and each runs entry code, then its critical section…
- How many lines print the value 3, and what is the sum of all the values printed? (Assume every fork() succeeds.) c int main() { int c = 0…
- A buddy allocator manages 1024 KB and splits blocks in powers of two, always using the lowest-addressed free block of the needed size…
- A disk has cylinders 0-499. The head is at 237 and is moving towards cylinder 0. Pending requests: 312, 45, 401, 198, 260, 12, 477, 150…
- A disk spins at 12,000 RPM, has 250 sectors of 512 bytes per track, an average seek time of 4 ms and a track-to-track seek time of 0.5 ms…