Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 66 ยท Processes

Inter-Process Communication (IPC)

Learn how processes exchange data: shared memory, message passing, pipes, FIFOs, message queues, sockets and signals, with runnable pipe and mailbox demos.

The OS deliberately isolates processes so they cannot damage each other. But real applications need cooperation: a browser talks to its renderer processes, a shell connects ls to grep, a web server hands work to a database. Inter-process communication (IPC) is the set of mechanisms the OS provides to let isolated processes exchange data and coordinate.

There are two fundamental models, shared memory and message passing, and a handful of concrete tools built from them: pipes, FIFOs, message queues, sockets, signals and memory-mapped files. This lesson explains each model, compares them, and runs small examples of both.

Two models of IPC

Shared memory Processes read and write one region.
Message passing Processes send and receive messages via the kernel.

Shared memory is fast but needs synchronization; messages are simpler.

1 / 2

Shared memory

With shared memory, the kernel maps the same physical memory region into the address spaces of two or more processes. Once set up, communication needs no system calls: a write by one process is immediately visible to the others. That makes it the fastest IPC method, ideal for large data such as video frames.

The price is synchronization. The kernel does not tell you when the data is ready or stop two writers colliding, so processes need their own coordination (semaphores or mutexes, covered in the Synchronization chapter). Typical APIs: POSIX shm_open and mmap, System V shmget, Windows file mappings.

Message passing

With message passing, processes never share memory. They use two operations, send(message) and receive(message), and the kernel carries the data. It is slower (data is copied into and out of the kernel) but simpler and safer, and it also works between machines. Messages can be:

  • Synchronous (blocking): the sender waits until the message is received; the receiver waits until one arrives.
  • Asynchronous (non-blocking): send returns immediately; receive returns a message or "nothing yet".
  • Buffered: the link holds a queue with limited capacity. When it is full, a sender blocks.

Communication may be direct (name the receiver) or indirect through a mailbox (port) that many processes can use.

Pipes

A pipe is a one-way byte stream: bytes written at one end come out of the other end in order. A shell builds ls | grep txt by creating a pipe and connecting ls's standard output to one end and grep's standard input to the other. An anonymous pipe exists only between related processes (parent and child). A named pipe (FIFO, made with mkfifo) has a file name, so unrelated processes can use it.

Python A real pipe

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In a real program the writer would be a different process (after fork, the child writes and the parent reads), but the mechanics (two descriptors, an in-kernel buffer, end-of-data once the writer closes) are the same.

Message queues and mailboxes

A message queue is a list of messages kept by the kernel. Senders append; receivers remove, often selecting by type or priority. Unlike a pipe, message boundaries are preserved: each message arrives whole. POSIX offers mq_open/mq_send/mq_receive; Windows has mailslots.

The following demo builds a bounded mailbox and shows blocking behavior: a send into a full mailbox must wait, and a receive from an empty mailbox must wait. (A cooperative simulation stands in for real blocking.)

Python A bounded mailbox

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Sockets

A socket is an endpoint for two-way communication. Local (UNIX domain) sockets connect processes on one machine; TCP and UDP sockets connect across networks. Sockets are the basis of web servers, databases and microservices. Their handling is covered in computer networks.

Signals

A signal is a short notification delivered to a process: SIGINT (Ctrl+C), SIGTERM (please quit), SIGKILL (cannot be ignored), SIGSEGV (bad memory access), SIGCHLD (a child ended). A process can install a handler, ignore most signals, or accept the default action. Signals carry almost no data (just the number), so they suit events, not data transfer.

Python Handling a signal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Other mechanisms

  • Memory-mapped files: two processes map the same file; changes are shared (similar to shared memory).
  • Remote procedure calls (RPC): call a function that runs in another process or on another machine as if local.
  • Files and databases: the simplest IPC, though slow and requiring locks.
  • D-Bus, Binder, XPC: higher-level IPC buses used by Linux desktops, Android and macOS.

Comparing the options

Mechanism Direction Speed Needs sync Works across machines
Shared memory Two-way Fastest Yes No
Pipe / FIFO One-way Fast Built in No
Message queue Two-way Medium Built in No
Socket Two-way Medium Built in Yes
Signal Notification Fast n/a No

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which IPC method is generally the fastest, and why?

    Once mapped, processes read and write the same memory directly, with no system call per transfer.
  2. 2. A shell runs 'ls | grep txt'. What connects the two commands?

    The shell creates a pipe and wires ls's output to grep's input.
  3. 3. What is the main extra responsibility when using shared memory?

    The kernel does not coordinate access to shared memory; processes must use their own synchronization.
  4. 4. Which signal cannot be caught or ignored?

    SIGKILL (and SIGSTOP) are handled only by the kernel.

Summary

  • IPC lets isolated processes cooperate. The two models are shared memory and message passing.
  • Shared memory is fastest but needs synchronization; message passing is simpler and also works across machines.
  • Tools: pipes (one-way byte streams), FIFOs, message queues, sockets, signals, memory-mapped files and RPC.
  • Blocking and buffering rules decide how send and receive behave.

Next: What Is a Thread?