Pointer Walk

Processes and Threads

A process has its own private memory. Threads inside it share that memory. That one difference decides what is safe, what is fast and what a crash takes down.

Operating SystemsBeginner9 min read

Two ways to do several things at once

Your computer runs many things together: a browser, a music player, an editor. Inside the browser, a page loads while a video plays and you scroll. The operating system offers two building blocks for this, and they are easy to confuse.

  • A process is a running program, with its own private memory.
  • A thread is a line of execution inside a process. Threads in the same process share its memory.

Nearly everything about how they differ follows from that one fact about memory.

Two processes, three threadsInteractive

Process A

Shared memory

count = 0

Thread 1

own stack

i = 0

Thread 2

own stack

j = 0

. →via the OS

Process B

Shared memory

count = 0

Thread 1

own stack

k = 0

Start. Two programs are running. Each is a process: the operating system gives it its own private region of memory. Process A has two threads inside it. Process B has one.

A process: a program in its own room

When you start a program, the operating system creates a process. It gets:

  • its own region of memory, which no other process can see,
  • its own list of open files and network connections,
  • at least one thread, to actually run code.

The isolation is enforced by the hardware. A process that tries to read another's memory is stopped. So one program cannot corrupt another, even if it is buggy or hostile.

A thread: a worker inside the room

A thread is what executes instructions. A process starts with one and can create more.

Each thread has its own:

  • stack, holding its local variables and where it is in the code,
  • position in the program.

All threads in a process share:

  • the program's code,
  • the heap, where objects and shared data live,
  • open files and connections.

So two threads can work on the same data structure with no copying at all. That is their great strength and their great danger.

What follows from sharing

Separate processesThreads in one process
MemoryPrivate to eachShared
Passing dataMust be sent as a message through the OSJust read the same variable
Cost to createHigher: a whole new memory spaceLower: a new stack
Switching between themSlowerFaster
A crashKills only that processKills every thread in the process
A bug with shared dataNot possible: nothing is sharedPossible: race conditions

Example

Why browsers use processes for tabs

Early browsers ran every tab as a thread in one process. One bad page could crash the whole browser, and a page could in principle peek at another tab's data.

Modern browsers give each site its own process. It costs more memory. In exchange, a crashing tab shows a sad face and the rest keep working, and the operating system itself keeps sites apart.

When threads share, they can collide

Two threads that update the same variable can interleave in ways that lose an update. Nothing warns you. See Race Conditions. The tools for preventing it are in Mutex, Semaphore, Channel.

Processes cannot have that bug with each other, because they have nothing in common to fight over. The trade is convenience against safety.

Talking between processes

Since processes cannot see each other's memory, they communicate through the operating system:

  • Pipes: one process writes, the other reads. cat log.txt | grep error connects two processes this way.
  • Sockets: the same mechanism as network connections, which also works between processes on one machine.
  • Files, and shared memory that both explicitly ask for.

Each message has to be copied, and often converted to bytes and back. That is the cost of isolation.

Which to use

  1. Need isolation? Use processes

    Untrusted code, plugins, anything where one failure must not spread.

  2. Need to share a lot of data cheaply? Use threads

    Work on one large in-memory structure, such as an image or a game world.

  3. Mostly waiting on the network? Consider neither

    One thread with an event loop can handle thousands of connections. See The Event Loop.

Key takeaways

  • A process is a running program with its own private memory.
  • A thread runs code inside a process and shares that process's memory with its other threads.
  • Sharing makes threads cheap and fast, and opens the door to race conditions.
  • A crash in one thread takes down its whole process. A crash in one process leaves others untouched.
  • Processes must send each other messages through the operating system.

Your progress

Check your progress

0%

  1. Read the page50% of this page

    When you have gone through the page and its animations, tick it off.

  2. Take the quiz50% of this page

    7 questions, from easy to hard. Take it as often as you like: the questions change, and only your best score counts.

Saved on this device only. See all your progress