๐Ÿง  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?

ReasonExplanation
๐Ÿ”— Data SharingProcesses often need to exchange results or inputs
๐Ÿง  CoordinationSynchronize actions (e.g., client-server model)
๐Ÿ›‘ Resource SharingHandle shared access to devices, files, etc.
๐Ÿšฆ SynchronizationEnsure correct execution order (avoid race conditions)

๐Ÿงฐ Types of IPC Mechanisms

1. ๐Ÿ“จ Message Passing (No shared memory)

MethodDescription
๐Ÿ“ฎ PipesUnidirectional byte stream between related processes
๐Ÿ”„ Named Pipes (FIFOs)Bidirectional and can work between unrelated processes
โœ‰๏ธ Message QueuesKernel-managed message buffers
๐ŸŒ SocketsFor network-based communication (e.g., client-server)

2. ๐Ÿง  Shared Memory (with synchronization)

MethodDescription
๐Ÿ“ฆ Shared Memory SegmentCommon memory region mapped into multiple processes
๐Ÿงต SemaphoresUsed to synchronize access to shared memory
โ›“๏ธ Mutexes / LocksPrevent concurrent writes/read-conflicts
๐Ÿชข Memory-Mapped FilesShared memory via mapped files (e.g., mmap())

๐Ÿ” Message Passing vs Shared Memory

FeatureMessage PassingShared Memory
๐Ÿ”— SpeedSlower (involves kernel)Faster (direct memory access)
๐Ÿ” ComplexityEasier (OS manages sync)Needs manual synchronization
๐Ÿ“š Use CaseDistributed systemsHigh-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.