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.
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.
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.
Where it shows up in interviews
Recognize it when: how does the server handle concurrent requests?
- Design a web server
- Explain Nginx vs Apache worker models
Recognize it when: one bad task must not crash others.
- Design a browser's tab architecture
- Design a code execution sandbox
Where it is used in real software
Chrome runs sites in separate renderer processes so a crash or exploit in one tab cannot read another site's memory.
Nginx uses a few worker processes with event loops; Java servers like Tomcat use thread pools; Gunicorn forks worker processes for Python.
Uses a process per connection, which is robust but memory-heavy, hence connection poolers like PgBouncer.
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.
How it works, step by step
- 1The OS starts a process
It allocates an address space, loads the program, and creates the main thread.
- 2The process creates threads
Threads share heap memory and open files but get their own stacks.
- 3The scheduler runs threads on CPU cores
It switches between them, giving the illusion that all run at once.
- 4Threads coordinate
Locks, atomics, and queues protect shared data.
- 5Processes communicate through IPC
Pipes, sockets, or message queues, because memory is not shared.
Process vs thread
Key differences
| Aspect | Process | Thread |
|---|---|---|
| Memory | Separate address space | Shared with other threads of the process |
| Creation cost | Higher (new address space) | Lower |
| Communication | IPC (slower) | Shared memory (fast) |
| Crash impact | Isolated to that process | Can take down the whole process |
| Context switch | More expensive | Cheaper |
| Example | Each Chrome tab | Worker 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).
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()); }}Complexity and performance
Cheaper than a process.
Higher for processes due to memory mapping changes.
Trade-offs
Processes contain crashes and security issues but cost more memory and slower communication; threads are efficient but share failure.
Thread-per-request is simple but memory-heavy at high concurrency; event loops or virtual threads handle more connections with fewer OS threads.
Variants and related techniques
Lightweight threads scheduled by the runtime (Java 21, Go) allow millions of concurrent tasks.
One thread multiplexes many I/O operations (Node.js, Nginx).
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.
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.
Practice problems
| Problem | Difficulty | What it trains |
|---|---|---|
| Compare thread pool and process pool for image resizing | Easy | CPU-bound vs I/O-bound work. |
| Design a web server's concurrency model | Medium | Processes, threads, and event loops. |