FOUNDATIONS / SYSTEM CONCEPT BRIEF

Processes vs threads

A process is a running program with its own isolated memory space, file handles, and resources.

BeginnerPhase 01 / Topic 16 of 17RequirementsTrade-offsFailure modes
01

Overview

A process is a running program with its own isolated memory space, file handles, and resources. A thread is a unit of execution inside a process; threads of the same process share memory and resources but each has its own stack and registers. Starting a Chrome tab creates a process; the tab's rendering and networking work may run on several threads.

Isolation is the key trade-off. Processes protect each other (a crash or memory bug stays contained) but communicating between them requires inter-process communication and is more expensive. Threads communicate cheaply through shared memory, which is fast but requires synchronization to avoid race conditions.

Restaurants and cooks

Each process is a separate restaurant with its own kitchen and pantry; a fire in one does not spread to the next. Threads are cooks in the same kitchen sharing the pantry; they work together efficiently but can bump into each other and grab the same ingredient.

02

When to use it

  • Explaining how servers handle many requests at once.
  • Choosing between multi-process and multi-threaded architectures.
  • Reasoning about crashes, isolation, and memory usage.
  • Classic operating systems interview questions.
03

Where it shows up in interviews

Concurrency model choice

Recognize it when: how does the server handle concurrent requests?

  • Design a web server
  • Explain Nginx vs Apache worker models
Isolation and fault tolerance

Recognize it when: one bad task must not crash others.

  • Design a browser's tab architecture
  • Design a code execution sandbox
04

Where it is used in real software

Chrome site isolation

Chrome runs sites in separate renderer processes so a crash or exploit in one tab cannot read another site's memory.

Web servers

Nginx uses a few worker processes with event loops; Java servers like Tomcat use thread pools; Gunicorn forks worker processes for Python.

PostgreSQL

Uses a process per connection, which is robust but memory-heavy, hence connection poolers like PgBouncer.

05

Key terms

Process
Running program with its own virtual address space.
Thread
Execution path within a process sharing its memory.
Context switch
The OS saving one task's state and loading another's.
IPC
Inter-process communication (pipes, sockets, shared memory).
Race condition
Bug where threads interleave unexpectedly on shared data.
06

How it works, step by step

  1. 1
    The OS starts a process

    It allocates an address space, loads the program, and creates the main thread.

  2. 2
    The process creates threads

    Threads share heap memory and open files but get their own stacks.

  3. 3
    The scheduler runs threads on CPU cores

    It switches between them, giving the illusion that all run at once.

  4. 4
    Threads coordinate

    Locks, atomics, and queues protect shared data.

  5. 5
    Processes communicate through IPC

    Pipes, sockets, or message queues, because memory is not shared.

07

Process vs thread

Key differences

Step 1 / 6
AspectProcessThread
MemorySeparate address spaceShared with other threads of the process
Creation costHigher (new address space)Lower
CommunicationIPC (slower)Shared memory (fast)
Crash impactIsolated to that processCan take down the whole process
Context switchMore expensiveCheaper
ExampleEach Chrome tabWorker threads in a Java server

NOWAspect: Memory | Process: Separate address space | Thread: Shared with other threads of the process

Choose processes for isolation and threads for cheap sharing, often combining both (several processes, each with a thread pool).

08

Implementation

import java.util.concurrent.*;import java.util.concurrent.atomic.AtomicInteger; public class ThreadsDemo {    public static void main(String[] args) throws Exception {        AtomicInteger counter = new AtomicInteger();          // shared memory between threads        ExecutorService pool = Executors.newFixedThreadPool(4);        for (int i = 0; i < 1000; i++) pool.submit(counter::incrementAndGet);        pool.shutdown();        pool.awaitTermination(5, TimeUnit.SECONDS);        System.out.println(counter.get());                    // 1000         // A separate process: its own memory, communicates through pipes        Process child = new ProcessBuilder("echo", "hello from another process").start();        System.out.println(new String(child.getInputStream().readAllBytes()).trim());    }}
09

Complexity and performance

Thread creation~tens of microseconds

Cheaper than a process.

Context switch~1-5 microseconds

Higher for processes due to memory mapping changes.

10

Trade-offs

Isolation vs efficiency

Processes contain crashes and security issues but cost more memory and slower communication; threads are efficient but share failure.

Many threads vs event loops

Thread-per-request is simple but memory-heavy at high concurrency; event loops or virtual threads handle more connections with fewer OS threads.

11

Variants and related techniques

Virtual threads / goroutines

Lightweight threads scheduled by the runtime (Java 21, Go) allow millions of concurrent tasks.

Event loop

One thread multiplexes many I/O operations (Node.js, Nginx).

12

Common mistakes

  • Assuming threads make CPU-bound code faster in every language.

    Fix: Python's GIL limits CPU parallelism in threads; use processes for CPU-bound Python work.

  • Sharing mutable state without synchronization.

    Fix: Use locks, atomics, immutable data, or message passing.

13

Interview questions

What is the difference between a process and a thread?

A process has its own isolated memory and resources; a thread runs inside a process and shares its memory with other threads. Processes are safer and isolated but heavier; threads are lighter and communicate faster but need synchronization.

Why does Chrome use a process per tab?

For isolation and security. A crash or exploit in one site's renderer cannot corrupt or read memory from other tabs, at the cost of higher memory usage.

14

Practice problems

ProblemDifficultyWhat it trains
Compare thread pool and process pool for image resizingEasyCPU-bound vs I/O-bound work.
Design a web server's concurrency modelMediumProcesses, threads, and event loops.