Communication: IPC vs Shared Memory
What you'll learn: How processes and threads differ fundamentally in the way they exchange information with each other.
When multiple execution units need to communicate, the mechanism depends entirely on whether you're working with separate processes or threads within the same process.
Processes Use Inter-Process Communication (IPC)
Processes live in isolated memory spaces — they cannot directly access each other's variables or data structures. To communicate, they must use inter-process communication (IPC) mechanisms provided by the operating system, such as:
- Sockets (network or local)
- Pipes and named pipes
- Message queues
- Shared memory segments (ironically named, but still requires OS-mediated setup)
These mechanisms are heavyweight: data must be serialized, passed through OS system calls, and deserialized on the other side. Think of it like mailing a letter between two houses — you can't just hand something directly to your neighbor; you need the postal service as an intermediary.
Threads Share Memory Directly
Threads within the same process share the same heap memory space. They can directly read and write the same objects and variables — no OS intervention needed. This is shared memory communication at the simplest level.
When one thread modifies an object, another thread can immediately see that change (with proper synchronization, of course). It's like roommates in the same house who can leave notes on the kitchen counter for each other — instant, direct access.
The Trade-off
IPC is safe (strong isolation) but slow and complex. Shared memory is fast and simple but requires careful synchronization to avoid race conditions and memory visibility issues — problems you've been learning to manage with concurrency tools.
Key Takeaway:
Processes communicate through expensive IPC mechanisms requiring OS mediation, while threads communicate efficiently through shared memory, directly accessing the same objects and variables within their process.