Skip to content

Volume ITechnology

2.2.4 — Inter-Process Communication ​

The photo-upload service and its thumbnail program are two separate processes. That separation is the point: if the thumbnailer crashes on a broken photo, the service survives (2.2.1). But it also creates a problem. The two need to talk. The service must tell the thumbnailer which photo to resize and how big to make it. The thumbnailer must answer whether it worked and where it saved the result.

They cannot just share a variable, because each process has its own private memory. A signal carries only a number (2.2.3). So the operating system provides a set of channels for exactly this job. Together they are called inter-process communication, or IPC. This page walks through each channel, shows the upload service using it, and ends with how to choose.

1. Every channel goes through the kernel ​

Here is the one idea that ties the whole page together. Process A cannot reach into process B's memory, and that is the wall that keeps them safe. So every IPC channel is something the kernel holds on their behalf. A writes into it, and the kernel lets B read from it. The kernel is the post office, and the channels differ in what kind of post they carry.

ChannelCarriesBetween
Pipea stream of bytes, one wayparent and child
Named pipe (FIFO)a stream of bytes, one wayany two processes
Unix domain socketbytes or messages, both waysany two on one machine
Shared memorynothing is sent; both see the same memoryany two on one machine
Message queueseparate messagesany two on one machine
Signaljust a numberany two
Filewhatever you writeanything, even later

The rest of the page takes these one at a time, in the order you will actually use them.

2. Pipes: a one-way stream of bytes ​

A pipe is the oldest and simplest channel. It is a small buffer inside the kernel with two ends. One end is for writing and the other is for reading. Bytes written into one end come out of the other end, in the same order. Each end is a file descriptor (2.1.2), so the processes use the ordinary read and write system calls on it.

Here is how a parent sets one up for a child, using the fork–exec pattern from 2.2.2:

  1. The parent calls pipe(). The kernel creates the buffer and returns two descriptors, one for each end.
  2. The parent calls fork(). The child gets a copy of the parent's descriptor table, so it has both ends too.
  3. In the gap before exec, the child moves the read end onto its descriptor 0, its standard input. It closes the ends it does not need.
  4. The child calls exec. The new program reads its standard input as usual, with no idea it is reading from a pipe.

The parent now writes into its end, and the child reads it as input. A second pipe in the other direction lets the child send results back on its standard output. Node does all of this for you.

ts
// ipc-pipes.ts — talk to the thumbnailer through its standard input and output
import { spawn } from "node:child_process";

const worker = `
  process.stdin.setEncoding("utf8");
  process.stdin.on("data", (chunk) => {
    for (const name of chunk.split("\\n").filter(Boolean))
      process.stdout.write("thumb-" + name + "\\n");
  });
`;
const child = spawn(process.execPath, ["-e", worker],
                    { stdio: ["pipe", "pipe", "inherit"] });     // (1)
child.stdout.setEncoding("utf8");
child.stdout.on("data", (text: string) =>
  process.stdout.write("parent got: " + text));                  // (2)
child.stdin.write("photo-001.jpg\n");                            // (3)
child.stdin.write("photo-002.jpg\n");
child.stdin.end();                                               // (4)

// Prints:
// parent got: thumb-photo-001.jpg
// thumb-photo-002.jpg
  • (1) The stdio list says what to connect to the child's descriptors 0, 1 and 2. "pipe" means "make a pipe". So the child's standard input and standard output each become one end of a pipe, and its standard error goes straight to our screen.
  • (2) Whatever the child writes to its standard output arrives here.
  • (3) Whatever we write here arrives on the child's standard input.
  • (4) end() closes our write end. The child's next read then returns 0 bytes, which means "end of input" (2.1.2). That is how a reader learns the writer has finished.

A pipe has no message boundaries ​

Look closely at the output. We wrote two separate lines, and the child wrote two separate lines back. But the parent received one chunk containing both, so parent got: appears only once. Nothing went wrong. A pipe carries a stream of bytes, not separate messages. The kernel keeps the bytes in order, but it does not remember where one write ended and the next began. A reader may receive two writes glued together, or one write split in half.

So any program that sends messages through a pipe needs its own way to mark where each message ends. The simplest is a newline after each message, and the reader splits on newlines, which our thumbnailer does. Another way is to send each message's length before it. The same is true of network connections (Part 5), and forgetting it is one of the most common bugs in networking code.

When the pipe is full, or nobody is reading ​

A pipe's buffer is small. On Linux it holds 64 KB by default. Two rules follow, and both matter.

If the buffer is full, the writer waits. A fast writer cannot flood a slow reader, because the kernel puts the writer to sleep until the reader has taken some bytes out. This automatic slowing-down is called backpressure, and 2.7.4 shows why every well-built data pipeline needs it. You get it free with a pipe.

If the reader has gone, the writer is told. When every read end of a pipe is closed and a process still writes into it, the kernel sends the writer the signal SIGPIPE, whose default action ends the process. That is usually right. In a shell command such as cat huge.log | head -5, once head has printed five lines and exited, SIGPIPE stops cat from pointlessly reading the rest of a huge file.

Node chooses differently. It ignores SIGPIPE, so the write call fails with the error code EPIPE, "broken pipe", and the program can decide what to do. You can see it by piping a chatty Node program into head:

text
$ node upload-report.ts | head -3
(first three lines of the report)
Error: EPIPE: broken pipe, write

The fix is to handle the error event on process.stdout, or to stop writing when a write fails.

Named pipes ​

An ordinary pipe has no name. Only the process that created it, and its children that inherited the descriptors, can use it. A named pipe, also called a FIFO (first in, first out), is the same kind of byte stream, but it appears as a special file in the file system. The command mkfifo /tmp/thumb-jobs creates one. Any process that can open that file can write into it or read from it, even two processes that are not related. The file holds no data on disk. It is just a name the kernel uses to connect a writer to a reader.

3. Node's message channel ​

Pipes carry bytes, and turning bytes into messages is your job. For the common case of a Node program talking to another Node program, Node offers a ready-made message channel.

ts
// ipc-channel.ts — the service sends a job object and gets a result object back
import { fork } from "node:child_process";

// thumb-worker.mjs contains:
//   process.on("message", (job) => {
//     process.send({ photo: job.photo, ok: true, bytes: 18_432 });
//     process.disconnect();
//   });

const w = fork("thumb-worker.mjs");                              // (1)
w.on("message", (reply) => console.log("reply:", reply));        // (2)
w.send({ photo: "photo-003.jpg", width: 200 });                  // (3)

// Prints:
// reply: { photo: 'photo-003.jpg', ok: true, bytes: 18432 }
  • (1) Node's fork is not the Unix fork from 2.2.2. It does not copy our process. It starts a brand-new Node process running the given file, and opens an extra channel between the two. The shared name is unfortunate but common.
  • (2) Each message from the child arrives as a whole object, never half of one.
  • (3) send turns the object into JSON text, marks where the message ends, and sends it. The other side waits for a complete message and turns it back into an object.

Underneath, this channel is an ordinary IPC channel from this page: a Unix domain socket on Linux and macOS, and a named pipe on Windows. Node adds the message boundaries for you. That is the whole convenience, and it is why this channel is the easy choice when both sides are Node.

4. Unix domain sockets: two-way, between any two processes ​

Pipes go one way, and plain pipes only work between relatives. A Unix domain socket removes both limits. It is a two-way connection between two processes on the same machine, and it is found through a name in the file system, such as /tmp/thumbs.sock.

It uses exactly the same programming interface as a network connection (Part 5). A server process listens on the path. A client process connects to it. Then both can read and write. But because both ends are on the same machine, the kernel skips everything a network needs. There is no packet routing and no network protocol, just bytes copied from one process's buffer to the other's. That makes it noticeably faster than connecting to yourself over the network.

Here the upload service sends jobs to a separate thumbnail service through a local socket. The path is different on Windows, which has its own equivalent, so the code picks the right one.

ts
// ipc-socket.ts — a thumbnail service listening on a local socket
import { createServer, connect } from "node:net";

const path = process.platform === "win32"
  ? "\\\\.\\pipe\\thumbs"                                        // Windows: a named pipe
  : "/tmp/thumbs.sock";                                          // (1) Linux/macOS: a socket file

const server = createServer((sock) => {                          // (2)
  sock.setEncoding("utf8");
  sock.on("data", (name: string) => sock.end(`queued ${name.trim()}\n`));
});
await new Promise<void>((resolve) => server.listen(path, resolve));

const client = connect(path, () => client.write("photo-004.jpg\n")); // (3)
client.setEncoding("utf8");
client.on("data", (t: string) => {
  process.stdout.write("socket said: " + t);
  server.close();
});

// Prints:
// socket said: queued photo-004.jpg
  • (1) On Linux and macOS, the socket appears as a file at this path. ls -l /tmp/thumbs.sock shows it with an s at the start of its permissions.
  • (2) The thumbnail service listens on the path, exactly as a web server listens on a port.
  • (3) The upload service connects using the path instead of an address and port. After that, it is an ordinary two-way connection.

You use Unix domain sockets every day without knowing:

  • The Docker command-line tool talks to the Docker background service through /var/run/docker.sock.
  • When a program on a database server connects to PostgreSQL on the same machine, it usually connects through a socket file such as /var/run/postgresql/.s.PGSQL.5432, not over the network.

They can also do two things no network connection can. A process can pass an open file descriptor to another process through one, so the receiver gets its own working ticket to the same open file. And the receiver can ask the kernel who is on the other end, meaning which user and which process. That second feature is how a local service can check permissions without any password at all: the kernel itself vouches for the caller.

5. Shared memory: the fastest channel, and the most dangerous ​

Every channel so far copies bytes. The sender's bytes are copied into the kernel, then copied out again into the receiver's memory. For small messages that is cheap. But imagine the upload service wants to hand the thumbnailer a 20 MB raw image. Copying 20 MB in and 20 MB out, for every photo, starts to cost real time.

Shared memory avoids copying altogether. The two processes ask the kernel to map the same piece of physical memory into both of their address spaces. 2.5.1 explains how one piece of physical memory can appear at an address in two processes, and copy-on-write in 2.2.2 already used the same machinery. Once it is set up, process A writes bytes into the region, and process B can read them immediately. Nothing is sent and nothing is copied. B simply looks at the same memory.

pipe or socket: two copiesprocess Aprocess Bkernel buffercopy 1copy 2shared memory: no copiesprocess Aprocess Bone region of RAMin both address spacesA writes, B reads the same bytes
Figure 1 — Copying versus sharing. Left: a pipe or socket copies the bytes from A into the kernel, then from the kernel into B. Right: with shared memory, one region of RAM appears inside both processes, so nothing is copied at all.

That speed has a price. The kernel no longer stands between the two processes. With a pipe, the kernel makes sure B never reads half of what A wrote. With shared memory, nothing does. If A is halfway through writing an image when B starts reading it, B reads half an old image and half a new one. The two processes must coordinate themselves, with locks and flags that both of them respect. 2.4.2 explains how those locks are built, and why getting them right is hard.

A real example: PostgreSQL, the database, runs one separate process for each client connection. All those processes share one large region of shared memory, the database's cache of disk pages (7.3.1). That way every connection sees the same cached data without copying it, and PostgreSQL's own locks keep them from treading on each other.

Inside a single Node program, the closest relative is a SharedArrayBuffer, a block of memory that several threads of one process can see at once. 2.4.1 uses one to show exactly how two writers can corrupt shared data.

6. Message queues, signals and files ​

Three more channels round out the list. You will use them less often, but you should recognise them.

Message queues. A message queue is a list of separate messages held by the kernel. A sender adds a whole message, and a receiver takes a whole message off. Unlike a pipe, messages keep their boundaries, and each can carry a priority, so urgent ones jump the line. POSIX message queues, created with mq_open, are the standard kind. In practice they are rarely used directly today. Programs on one machine usually prefer Unix domain sockets. Programs on different machines use a message broker, a separate service that holds queues across a network, which 10.8.1 covers. The idea is the same: a queue that decouples the sender's pace from the receiver's.

Signals. A signal is IPC too, just a very poor one, because it carries only a number. Two signals, SIGUSR1 and SIGUSR2, exist purely for programs to use however they like. A common use is "write your current statistics to the log" or "rotate your log file now". If you need to send any actual data, use another channel.

Files. The simplest channel of all is a file. One process writes it and another reads it later, even after a reboot. The hard parts are knowing when the file is complete, and stopping two processes from writing it at the same moment. 2.6.2 shows the standard trick for the first: write to a temporary file, then rename it. The rename happens in one step, so a reader sees either the old file or the whole new one, never half of it.

7. Choosing a channel ​

Most choices come down to three questions. Are the two processes related? Do they need to send separate messages, or a stream? And how much data moves?

SituationUse
Parent sends input to a child it startedpipe on stdin/stdout
Node parent and Node child exchange objectsNode's fork channel
Two unrelated programs on one machineUnix domain socket
Large data, many times a second, on one machineshared memory plus locks
Programs on different machinesnetwork socket or a message broker
"Do this now", with no dataa signal
Data must survive a restarta file, written atomically

For our photo-upload service, the honest answer is a pipe or Node's channel. The thumbnailer is a child the service starts itself, the messages are small, and both channels are simple. Shared memory would be faster for very large raw images, but it would add locking bugs to save a few milliseconds per photo. Choose the simplest channel that does the job, and move to a faster one only when you can measure that you need it.

What the interviewer will push on ​

"What ways can two processes communicate?" They are checking breadth and, more importantly, whether you can say when to use each. A good answer lists pipes, named pipes, sockets, shared memory, message queues, signals and files, then picks one for a concrete case. The common weak answer lists names with no trade-offs.

"What is the difference between a pipe and a message queue?" The tell is knowing that a pipe is a byte stream with no message boundaries, while a queue keeps each message whole. Strong candidates mention that network connections behave like pipes here, which is why protocols need their own framing.

"Why is shared memory the fastest, and what is the catch?" Fastest because nothing is copied: both processes map the same physical memory. The catch is that the kernel no longer protects the data, so the processes must synchronise with their own locks. Naming PostgreSQL's shared buffer cache shows you have seen it used for real.

"What happens if you write to a pipe nobody reads?" If the pipe is full, the writer blocks, which is backpressure. If the reader has exited, the writer gets SIGPIPE, or EPIPE if it ignores that signal, as Node does.

"What is special about a Unix domain socket compared with TCP on localhost?" It skips the network stack, so it is faster, and it can pass open file descriptors and tell the receiver which user is connecting. That last point is how a local service can trust the caller without a password.

Something to volunteer that nobody asks for: every IPC channel except shared memory goes through the kernel, and that is a feature. The kernel keeps the bytes whole and in order, puts a fast writer to sleep when the reader falls behind, and tells the writer when the reader has gone. Shared memory gives up all of that for speed.

Recall

  • Processes cannot see each other's memory, so every inter-process communication channel is something the kernel holds for them.
  • A pipe is a one-way kernel byte buffer between related processes. It has no message boundaries, a full pipe blocks the writer (backpressure), and writing to a pipe with no reader raises SIGPIPE (or EPIPE in Node). A named pipe is the same thing with a file-system name.
  • Node's fork starts a new Node process with a message channel. It is not the Unix fork.
  • A Unix domain socket is a fast two-way connection between any two local processes, found by a file path. It can pass file descriptors and identify the caller.
  • Shared memory maps one region into both processes, so nothing is copied, but the processes must coordinate with their own locks. A message queue keeps whole messages; signals carry only a number; files survive restarts.

Self-test: Why must every IPC channel except shared memory go through the kernel? Why did our parent receive two lines in one chunk? What happens when a pipe is full, and when its reader has gone? What can a Unix domain socket do that a network connection cannot? When would you accept the risk of shared memory?

Quiz Bank ​

FoundationalWhat is a pipe, and how does a parent give one to a child?

A pipe is a small buffer inside the kernel with a write end and a read end, each a file descriptor. Bytes written into one end come out of the other, in order.

The parent calls pipe() to create it and gets both descriptors. It then calls fork(), so the child inherits both. Before calling exec, the child moves the read end onto its standard input and closes the ends it does not need. After exec, the new program reads its standard input as usual, not knowing it is a pipe. The shell builds a | b exactly this way.

FoundationalWhy does shared memory need locks when pipes do not?

With a pipe, every byte goes through the kernel, which keeps the bytes whole and in order and never lets the reader see half of a write in progress. With shared memory, both processes look directly at the same physical memory, and the kernel is no longer in between. If one process is halfway through writing when the other reads, the reader sees a mixture of old and new data. So the processes must agree on their own locks or flags to take turns.

AppliedYour program sends two JSON messages through a pipe, and the receiver sometimes fails to parse them. Why?

A pipe is a byte stream with no message boundaries. The kernel keeps the bytes in order, but it does not remember where one write ended. The receiver's read may return both messages glued together, or one message cut in half, so parsing each chunk as one JSON value fails.

The fix is framing. Put a newline after each message and split the received text on newlines, keeping any incomplete last piece for the next read. Or send each message's length first. Network connections have the same property, which is why every network protocol defines its own framing.

InterviewWhat happens when a process writes into a pipe and the reader is slow, or has exited?

If the reader is slow and the pipe's buffer fills up, 64 KB by default on Linux, the kernel puts the writer to sleep until the reader takes bytes out. That is backpressure, and it stops a fast writer from flooding a slow reader.

If every read end has been closed, the kernel sends the writer SIGPIPE, which ends it by default. That is why cat big.log | head -5 stops cat promptly once head exits. A program that ignores SIGPIPE, as Node does, instead gets the error EPIPE, "broken pipe", from the write and must handle it.

InterviewWhat is a Unix domain socket, and when would you use one instead of TCP on localhost?

A Unix domain socket is a two-way connection between two processes on the same machine, found through a path in the file system rather than an address and a port. It uses the same programming interface as a network socket.

Prefer it for local communication, for four reasons:

  1. It skips the network stack, so it is faster.
  2. Ordinary file permissions on the socket file control who may connect.
  3. The receiver can ask the kernel which user and process is connecting.
  4. It can pass open file descriptors between processes. The Docker command-line tool and local PostgreSQL connections both use one.
StaffA video service passes 50 MB raw frames from a decoder process to an encoder process, 30 times a second, through a pipe, and CPU use is far higher than expected. What would you change and what new risks would you take on?

At 50 MB times 30 frames a second, the pipe moves 1.5 GB a second, and every byte is copied twice: from the decoder into the kernel and from the kernel into the encoder. That copying, plus the many system calls needed to move it in 64 KB pieces, is where the CPU goes.

Switch to shared memory: map a set of frame-sized buffers into both processes, have the decoder write a frame into a free buffer, and send only a tiny message, such as "frame 812 is in buffer 3", over a socket or pipe. The bulk data is never copied.

The new risks: the kernel no longer protects the buffers, so you need a clear rule about who owns each buffer at each moment, or the encoder will read half-written frames. You also need to handle one side crashing while holding a buffer, and to size the pool so the decoder waits, rather than overwriting, when the encoder falls behind. That last part recreates the backpressure the pipe used to give you for free.

Scenario Drill ​

DrillThe photo-upload service will run its thumbnail program as a separate long-running service on the same machine, instead of starting it once per photo. Choose an IPC channel for the jobs and results, and justify it using this page.

The two programs are no longer parent and child. The thumbnail service will be started separately, perhaps by systemd. So a plain pipe does not fit, because pipes need a parent to set them up, and they go only one way.

A Unix domain socket fits best:

  • either program can start first, and the upload service connects to a known path such as /run/thumbs.sock;
  • it is two-way, so jobs go one way and results come back on the same connection;
  • it is faster than connecting over the network to the same machine;
  • the socket file's permissions control who may send jobs, and the thumbnail service can ask the kernel which user is connecting.

Because a socket is a byte stream, frame each message, for example one JSON object per line. Keep messages small: send the path of the photo on disk, not the photo's bytes. If the thumbnail service ever moved to another machine, the same design would carry over to a network socket or a message broker (10.8.1) with little change. Shared memory would be the wrong choice here: it saves copying a short file path, and costs a whole layer of locking.

Next: processes are safe because they share nothing, and this page showed how much work it takes to make them share anything at all. Often you want the opposite: several strands of work inside one program, all seeing the same memory on purpose. Those strands are threads. 2.3.1 shows how the kernel builds them, and 2.3.2 shows how the scheduler decides which one runs next.

Combine chapters into one PDF

Pick the chapters you want. They print in the order you pick them.

Volume I — Technology
Part 0 — Retention & Notation
Part 1 — Foundations of Computation
Part 2 — Operating Systems
2.1 The Kernel
2.2 Processes
2.3 Threads & Scheduling
2.4 Concurrency Under the Hood
2.5 Memory Management
2.6 File Systems
2.7 Input/Output
2.8 Linux for Engineers
2.9 Windows & macOS in Practice
Part 3 — Languages, Compilers & Runtimes
3.6 JavaScript & V8
3.7 TypeScript
3.8 Node.js Internals
3.13 Python & CPython
3.14 FastAPI & Production Python
Part 4 — Data Structures & Algorithms
4.0 Classic Problems — Before NeetCode
4.4 Arrays & Hashing — Problems
4.5 Two Pointers
4.6 Sliding Window
4.8 Stack — Problems
4.9 Linked List — Problems
4.11 Binary Search — Problems
4.13 Trees
4.14 Tree — Problems
4.15 Trie — Problems
4.17 Heap — Problems
4.18 Backtracking — Problems
4.19 Graphs
4.20 Graph — Problems
4.21 Advanced Graph — Problems
4.23 1-D DP — Problems
4.24 2-D DP — Problems
4.26 Greedy — Problems
4.27 Interval — Problems
4.28 Math & Geometry — Problems
4.30 Bit Manipulation — Problems
Part 5 — Networking & the Web
5.3 IP
5.4 Transport
5.6 HTTP
Part 6 — Frontend Engineering
6.1 Browser Internals
6.2 HTML & CSS Mastery
6.3 JavaScript in the Browser
6.4 React, from First Component to Full Shop
6.5 Frameworks & Rendering
6.8 UI Engineering & Patterns
Part 7 — Databases & Storage
7.2 SQL Mastery
7.3 Storage Engine Internals
7.4 Transactions
7.5 NoSQL Taxonomy
7.8 Analytics & Big Data
Part 8 — Security, Identity & Cryptography
8.2 Cryptography
8.3 Protocols of Trust
8.4 Identity: AuthN & AuthZ
8.5 Application Security
8.6 Infrastructure Security
Part 9 — Software Design (LLD)
9.2 OOP Foundations
9.3 Design Principles
9.4 Design Patterns
9.5 Concurrency
9.6 API Design
9.7 LLD Interview Machines
9.9 Node/Express Production Playbook
Part 10 — Distributed Systems & HLD
10.7 Consistency & Consensus
10.8 Messaging & Microservices
10.14 Caching
Building blocks from the design seat
Part 11 — System Design Case Studies
Part 12 — AI & ML Engineering
12.1 What AI Engineering Is
12.2 The Math You Need
12.3 Classical Machine Learning
12.4 Data for ML
12.5 Deep Learning
12.6 Embeddings & Vector Search
12.7 Transformers & LLMs
12.8 Building with LLMs
12.9 Retrieval-Augmented Generation
12.10 Agents
12.11 Adapting Models
12.12 Production AI
12.13 Multimodal AI
12.14 AI Security & Governance
12.15 Case Studies & Capstone
Part 13 — Cloud, DevOps & Infrastructure
13.11 Git, from First Commit to Team Workflow
Part 14 — Containers: Docker & Kubernetes
14.1 Foundations
14.2 Docker
14.3 Kubernetes
14.4 Kubernetes on Azure
Part 15 — The Craft & Career
Nothing picked yet