Operating Systems Practice Question 1: Process vs Thread
Operating Systems · Test 1 · question 1 of 10
An image-processing program splits its work between two workers. In version 1 each worker is a separate process created with fork(); in version 2 each worker is a thread created in the same process. In both versions, each worker writes its results into a global array results[].
In version 1 the parent never sees the workers' results in its copy of results[], but in version 2 it does. What explains the difference?
Show answer and explanation
Answer:
A thread is much like a separate process except for one key difference: all threads of a process share the same address space, and therefore the same code, heap and global data. Each thread has its own program counter, registers and stack. After fork(), the child gets its *own* copy of the parent's address space, so a child writing to results[] changes only its copy. That is why version 2 sees the results and version 1 does not (without explicit IPC such as shared memory or a pipe).
Option A is wrong: threads are not pinned to their parent's core, and a CPU cache is kept coherent by hardware; caching is not what makes memory shared or private.
Option C is wrong: process scheduling priority has nothing to do with whether writes are visible; the child's writes do reach memory, just the child's own memory.
Option D is wrong: global variables of a user program live in user-space memory in both cases. The difference is whether that memory is shared (threads) or copied (processes).
Reference: OSTEP - Concurrency: An Introduction