Course map

Topic 005 / Core

Processes and threads

Chapter 1.2 · The Operating System and the Machine

What and why

A process is the OS's unit of isolation — its own address space and resources; a thread is its unit of execution — a path of instructions that can run concurrently with others. The practical question this topic answers is cheap-but-unsafe versus expensive-but-safe: threads share memory with their process siblings (fast communication, no isolation) while processes don't (isolation, at the cost of expensive creation and explicit communication). Builds on: → 4 (operating system fundamentals).

Diagram

Process Thread
Address space own, isolated shared with sibling threads
Creation cost high low
Crash isolation a crash doesn't affect other processes a crash can take down the whole process
Communication IPC, sockets, pipes direct — shared memory

How it works

  1. Creating a process gives it a fresh address space and its own copy of OS resources (file handles, etc.) — the OS does real work to set this up, which is why it's comparatively slow.
  2. Creating a thread inside an existing process reuses that process's address space — only a stack and some bookkeeping are new, which is why it's comparatively fast.
  3. A context switch between two threads of the same process is cheaper than a switch between two different processes, because the address-space mapping doesn't need to change.
  4. Because threads share memory directly, communication between them is just reading and writing shared variables — fast, but unsafe without coordination (→ 7).
  5. A crashed process doesn't damage its siblings; a crashed thread can corrupt or kill the whole process it belongs to — this is why some servers isolate risky work into separate worker processes rather than threads.

Practical example

(illustrative). A web server choosing between N worker processes and N worker threads in one process, for the same N: the process model costs more memory (each worker has its own full address space) and more creation time, but one worker crashing never affects another. The thread model is lighter and shares an in-memory cache naturally across workers, but one worker's unhandled memory corruption can take all of them down at once. Neither is "correct" — the choice trades isolation for efficiency.

When to use it

  • Use multiple processes when: fault isolation matters, or you need to exceed a single process's resource limits.
  • Use threads when: the work needs to share data and isolation between units of work isn't required.
  • Avoid: creating a new OS thread or process per unit of work when that work is small and frequent — the creation cost dominates; use a pool instead.
  • Failure scenarios: unbounded thread creation exhausting memory or hitting an OS limit; processes left as zombies because their exit status was never collected; a thread pool sized so small it serializes work that should run concurrently.
  • Simpler alternative: if the workload is I/O-bound rather than CPU-bound, a single-threaded event loop (→ 6) can avoid the thread-creation question, and the synchronization problems of topic 7, entirely.

Practice

(mechanism — trace by hand; 15–20 min, no lab services). Measure, in your own program, the time to create and join 1,000 OS threads that each do essentially no work, and compare it to the time for 1,000 plain function calls that do the same no-work. Compute the ratio. Done when you have a measured number for "thread creation cost relative to a function call" on your own machine, not an assumed one.

Key takeaway

Processes trade creation cost for isolation; threads trade isolation for cheap, fast, unsafe sharing. Done when you can: justify, for a given service, whether its workers should be processes or threads, and name what you're giving up either way. Used next in → 6 (concurrency and parallelism), → 7 (thread safety), → 8 (memory management).

References

None — this topic makes no external claims.

Source directory

Search the guide