Operating systems / Processes vs threads

Processes vs threads

Two ways to run things at the same time: processes that are walled off from each other, and threads that share one memory. What each protects, what each costs, and when to pick which.

Beginner7 steps~7 min
Process — a running program behind its own walls
Thread — a line of execution inside a process
Memory — code and data the process owns
Operating system — keeps the walls, carries messages
// THE SHORT VERSION

In 30 seconds

A process is a running program with memory of its own. The operating system walls processes off from each other, so one cannot read or damage another, and a crash stays contained.

A thread is a line of execution inside a process. Threads of one process share its memory, which makes working together fast and cheap, but lets them corrupt shared data unless they take turns.

Processes are heavier to create and must exchange copies of data through the operating system. Choose processes for isolation, threads for close teamwork.

Processes vs threads, step by step

STEPS01 / 07

Simplified: processes can also ask the operating system for a region of shared memory, and the exact costs of creating and switching differ from one system to another.

STEP 01 / 07 A process: its own world
THE MATH
process = private memory + at least one thread
  1. Step 1: A process: its own world. When you start a program, the operating system creates a process for it: a private block of memory holding the program’s code and data, plus a thread, which is the sequence of instructions being carried out. The thread keeps its working notes on a stack. No other process can see into this memory.
  2. Step 2: Processes are walled off. Run two programs and you get two processes, each with its own memory. The operating system and the processor enforce the wall between them: process B cannot read or change anything in process A, even by mistake. If B crashes, A carries on. This protection is called isolation.
  3. Step 3: Threads share one process. A process can run several threads at once. They all live inside the same walls, so they see the same code, the same data and the same open files. What each thread has to itself is small: its own stack and its own place in the program.
  4. Step 4: Sharing is fast but risky. Shared memory needs no copying, which makes threads quick to work together. It also lets them collide. Here two threads each add 1 to count. Both read 5 before either writes back, both write 6, and one update vanishes. Threads must guard shared data with a lock; processes cannot have this accident with each other’s memory.
  5. Step 5: Processes talk by message. Because processes share nothing, they have to ask the operating system to carry data across: through a pipe, a network socket or a file. Process A writes the bytes, the system copies them, process B reads its own copy. That is slower than a thread reading a shared variable, but nothing is exposed by accident.
  6. Step 6: What each one costs. A new thread needs little: a stack and a saved set of processor registers. A new process needs those plus its own memory map and bookkeeping in the operating system. Switching the processor between two threads of one process only swaps registers; switching to another process also swaps the memory map, so it costs more.
  7. Step 7: When to use which. Use separate processes when a failure or a security hole must stay contained: browsers put tabs in different processes so that one crash does not take the rest down. Use threads when the pieces work on the same data and must be cheap to start and switch, like the workers of an image editor or a web server.
VALUES SO FAR
processown memoryisolated
thread····shared
stack····private
count····race
message····safe
create····cost
switch····cost
choose····rule

Spotted a mistake on this page?