CS FundamentalsHardTCS NQT · Infosys pattern
What does this program print? (Assume fork() succeeds; the order of the two lines may vary.)
What does this program print? (Assume fork() succeeds; the order of the two lines may vary.)
int x = 5;
int main() {
if (fork() == 0) x++;
else x--;
printf("%d ", x);
return 0;
}- A. 5 5
- B. 6 6
- C. 6 5
- D. 6 4
Correct answer: D. 6 4
After fork the child and parent have separate copies of x (copy-on-write). The child increments its copy to 6, the parent decrements its own to 4. Changes in one are not seen by the other, even though x is global.
More Operating Systems questions
- A resource-allocation graph contains a cycle. Which conclusion is correct?
- Reference string (times 1 to 14): 3, 5, 3, 1, 4, 4, 2, 1, 1, 6, 6, 6, 2, 5. With working-set window Δ = 4 references, what is the working…
- Processes: A (arrival 0, burst 5), B (arrival 1, burst 3), C (arrival 2, burst 8), D (arrival 3, burst 6). Priorities: A = 3, B = 1, C =…
- Cylinders 0–199, head at 72 moving towards higher cylinders. Requests: 110, 25, 160, 47, 130, 8, 95, 185. Under C-LOOK, counting the jump…
- In the first readers–writers problem (readers get priority: a reader never waits if the resource is already being read), which problem can…
- When is a spinlock (busy-waiting lock) preferable to a blocking lock?