๐ง What is IPC?
IPC (Inter-Process Communication) is a set of mechanisms provided by the operating system that allows processes to communicate and coordinate with each other โ either on the same machine or across a network.
Since each process has its own private memory space, they canโt directly access each otherโs data โ IPC provides the controlled pathways for that.
๐ Why is IPC Needed?
| Reason | Explanation |
|---|---|
| ๐ Data Sharing | Processes often need to exchange results or inputs |
| ๐ง Coordination | Synchronize actions (e.g., client-server model) |
| ๐ Resource Sharing | Handle shared access to devices, files, etc. |
| ๐ฆ Synchronization | Ensure correct execution order (avoid race conditions) |
๐งฐ Types of IPC Mechanisms
1. ๐จ Message Passing (No shared memory)
| Method | Description |
|---|---|
| ๐ฎ Pipes | Unidirectional byte stream between related processes |
| ๐ Named Pipes (FIFOs) | Bidirectional and can work between unrelated processes |
| โ๏ธ Message Queues | Kernel-managed message buffers |
| ๐ Sockets | For network-based communication (e.g., client-server) |
2. ๐ง Shared Memory (with synchronization)
| Method | Description |
|---|---|
| ๐ฆ Shared Memory Segment | Common memory region mapped into multiple processes |
| ๐งต Semaphores | Used to synchronize access to shared memory |
| โ๏ธ Mutexes / Locks | Prevent concurrent writes/read-conflicts |
| ๐ชข Memory-Mapped Files | Shared memory via mapped files (e.g., mmap()) |
๐ Message Passing vs Shared Memory
| Feature | Message Passing | Shared Memory |
|---|---|---|
| ๐ Speed | Slower (involves kernel) | Faster (direct memory access) |
| ๐ Complexity | Easier (OS manages sync) | Needs manual synchronization |
| ๐ Use Case | Distributed systems | High-speed local communication |
๐งช Example (Linux)
// Example: Anonymous pipe (Parent-child)
int fd[2];
pipe(fd);
fork(); // Child process
// One writes to fd[1], the other reads from fd[0]
โ๏ธ Real-World Analogy
Think of processes like people in separate rooms.
IPC is the phone line, shared whiteboard, or message tube between them โ they canโt see each otherโs notes, but they can send messages or share a board.
๐ง Interview-Ready Definition
Inter-Process Communication (IPC) is a collection of mechanisms provided by the OS to allow processes to communicate, coordinate, and share data. It includes message-passing tools like pipes, sockets, and message queues, as well as shared memory with synchronization primitives like semaphores and mutexes.