Overview

Inter-Process Communication (IPC) is a set of mechanisms provided by the operating system to allow processes to communicate and synchronize their actions. Since processes have isolated memory spaces, they cannot simply read/write to each other’s variables.

Core Concepts

  • Shared Memory:
    • Two or more processes map the same region of physical memory into their own address spaces.
    • Pros: Fastest IPC method.
    • Cons: Requires synchronization (e.g., mutexes, semaphores) to avoid race conditions.
  • Message Passing:
    • Processes communicate by sending and receiving messages.
    • Pipes: Unidirectional communication channels (e.g., ls | grep).
    • Message Queues: Asynchronous communication using a queue.
    • Sockets: Bidirectional communication over a network or locally (Unix Domain Sockets).
  • Synchronization Primitives:
    • Mutex (Mutual Exclusion): A lock that only one process can hold at a time.
    • Semaphore: A counter that allows a limited number of processes to access a resource.
    • Condition Variable: Allows a process to sleep until a specific condition is met.
  • Deadlock: A state where two or more processes are blocked forever, each waiting for the other to release a resource.

Code Examples

# Example of Pipe IPC in Bash
ls -la | grep ".md"

L

Example of a local socket (conceptual)

server.sock = socket.create(“unix”)

server.bind(“/tmp/server.sock”)

server.listen()

client.connect(“/tmp/server.sock”)