IPC
Pipes, sockets, shared memory, message queues — how processes exchange data.
Level: intermediate
What is it?
Inter-process communication (IPC) comprises mechanisms for processes to exchange data or coordinate activity. Common mechanisms include pipes, Unix-domain and network sockets, shared memory, message queues, and signals; each offers different data semantics and synchronization costs.
Why does it matter?
Separate processes provide useful fault and privilege boundaries but cannot assume they share memory. IPC defines a controlled bridge between them and often becomes a major latency, reliability, and security boundary.
Mental model
Choose a communication shape first: byte stream, message, shared region, or one-bit notification. Then decide who owns the data, how peers synchronize, how failures are reported, and how backpressure is handled.
How it works
- Pipes provide byte streams often used between related processes; framing must be designed by the application when messages matter.
- Sockets can be local or networked and define endpoint communication semantics.
- Shared memory reduces data-copy overhead but requires explicit coordination to avoid races.
- Message queues preserve message boundaries under their API contract and may provide persistence or bounded capacity.
- Signals are primarily notifications, not a general-purpose bulk data channel.
Steps
- A producer serializes a message and writes it to a pipe or socket.
- The kernel buffers data according to the IPC mechanism's capacity and semantics.
- The receiver reads available data and reconstructs message boundaries if the channel is a stream.
- Both sides handle partial writes, closure, timeouts, and peer failure.
Real-world usage
- Decomposing services into separate processes.
- Building shell pipelines and local client-server processes.
- Choosing shared memory versus messages based on throughput, isolation, and complexity.
Common mistakes
- Assuming one read returns exactly one write's worth of data on a stream socket.
- Using shared memory without synchronization or ownership rules.
- Ignoring buffer limits and the need for backpressure.
Interview questions
When might shared memory outperform a socket?
For large amounts of local data, shared memory can avoid repeated copies, but it requires careful synchronization and lifecycle management.
Why does a stream socket need framing?
A stream preserves byte order but not application message boundaries; the receiver must know how many bytes make one message or use a delimiter/length prefix.