How Do Threads and Processes Differ in Memory Sharing?
How threads share memory with sibling threads while processes stay isolated — explained with an OS interview question and code example.
Expected Interview Answer
Threads within the same process share the same address space — code, heap, and global data — and each only gets its own private stack and register set, whereas separate processes each get a fully isolated address space that cannot be accessed by another process without explicit inter-process communication.
A process is the unit of resource ownership: it owns its own page table, heap, open file descriptors, and address space, which the OS strictly isolates from every other process using hardware memory protection, so one process crashing or corrupting memory cannot directly corrupt another. A thread is the unit of CPU scheduling within a process; every thread created inside a process shares that same page table, heap, global variables, and file descriptors with its sibling threads, and only gets a private stack and its own set of CPU registers (program counter, stack pointer) so each thread can execute independently. This shared address space is what makes threads communicate cheaply — writing to a shared variable is instantly visible to other threads — but it also means one thread can corrupt data another thread relies on, requiring synchronization primitives like mutexes to protect shared state. Because processes do not share memory by default, they must use explicit mechanisms — pipes, shared memory segments, sockets, or message queues — to exchange data, which is safer but slower than a thread simply reading a shared variable.
- Clarifies why threads communicate faster but less safely than processes
- Explains the isolation guarantee processes give for fault containment
- Motivates why synchronization primitives are needed for threads
- Foundation for choosing multithreading vs multiprocessing in design
AI Mentor Explanation
A process is like an entire national team with its own dedicated dressing room, kit, and equipment that no other team can touch without formal arrangement. Threads are like individual players within that same team sharing one dressing room, one kit bag, and one team strategy board — any player can read or scribble on the shared strategy board instantly, but each player still has their own personal locker (private stack) for their own gear. Two separate teams must formally exchange information through team managers (inter-process communication), while teammates just shout across the shared room.
Step-by-Step Explanation
Step 1
Process creation
The OS allocates a fresh, isolated address space and page table; nothing is shared with other processes by default.
Step 2
Thread creation
A new thread within that process reuses the same page table, heap, and globals, getting only a new private stack and register set.
Step 3
Shared access
Threads read and write shared heap/global data directly and instantly; processes cannot, without explicit IPC.
Step 4
Synchronization need
Because threads share memory, mutexes/semaphores are needed to prevent races on shared data.
What Interviewer Expects
- Clear statement that threads share address space, processes do not
- What exactly is private to a thread (stack, registers) vs shared
- Why processes need explicit IPC to exchange data
- Awareness of the safety vs speed trade-off this creates
Common Mistakes
- Saying threads have no private state at all
- Claiming processes can read each other’s memory by default
- Confusing the heap (shared among threads) with the stack (private per thread)
- Not mentioning that synchronization is needed because threads share memory
Best Answer (HR Friendly)
“Threads inside the same program share the same memory — code, data, and heap — so they can pass information to each other instantly by just reading a shared variable, though each thread keeps its own private call stack. Separate processes, on the other hand, each get their own sealed-off memory, so if one crashes it cannot corrupt another, but they have to use explicit tools like pipes or shared memory segments to talk to each other.”
Code Example
#include <pthread.h>
int shared_counter = 0; /* visible to every thread in this process */
void *worker(void *arg) {
shared_counter++; /* direct access, no IPC needed */
return NULL;
}
int main(void) {
pthread_t t1, t2;
pthread_create(&t1, NULL, worker, NULL);
pthread_create(&t2, NULL, worker, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
/* shared_counter is visible and modified by both threads directly */
/* A separate process, by contrast, would need fork() + a pipe or
shared memory segment to see any change to shared_counter at all. */
return 0;
}Follow-up Questions
- What exactly does a thread keep private versus share with sibling threads?
- What inter-process communication mechanisms exist, and when would you pick each?
- Why is a thread crash more dangerous to the whole process than a process crash is to the OS?
- How does fork() differ from creating a new thread in terms of memory?
MCQ Practice
1. What do threads within the same process share by default?
Threads share the process’s address space, including heap and global/static data, keeping only their own stack and registers private.
2. How must two separate processes exchange data?
Processes have isolated address spaces, so exchanging data requires explicit IPC such as pipes, sockets, or shared memory segments.
3. What is private to each individual thread?
Each thread gets its own stack and CPU register state; the page table, heap, and globals are shared with sibling threads.
Flash Cards
Do threads share memory with sibling threads? — Yes — the same address space, heap, and globals; only the stack and registers are private per thread.
Do processes share memory by default? — No — each process has an isolated address space and must use explicit IPC to exchange data.
What is private to a thread? — Its own stack and its own CPU register set (program counter, stack pointer).
Why is thread communication faster than process communication? — Threads read/write shared memory directly; processes need pipes, sockets, or shared memory segments.