The Plain Picture#
A family rents one hotel room (a process). Inside, three family members each do their own thing — one reads, one packs, one makes calls. They share the room's wardrobe, fridge and suitcase (shared memory), but each has their own to-do notepad (their own stack and "where am I" bookmark).
Renting a second room for each person would give more privacy, but costs much more and makes sharing the suitcase awkward.
The Real Names#
A thread is one independent sequence of instructions inside a process — a "worker". Every process starts with one thread (the main thread) and can create more.
Threads in the same process share the heap, globals, code, and open files.
Each thread has its own stack, registers and program counter.
The scheduler (Ch 4) schedules threads, not just processes, so threads of one process can truly run in parallel on different cores.
Under the Hood#
ONE PROCESS (e.g. StudyBuddy's server) ┌─────────────────────────────────────────────────────────────┐ │ SHARED: code · globals · HEAP · open files/sockets │ │ │ │ ┌────────────┐ ┌────────────┐ ┌────────────┐ │ │ │ Thread 1 │ │ Thread 2 │ │ Thread 3 │ │ │ │ own stack │ │ own stack │ │ own stack │ │ │ │ own regs │ │ own regs │ │ own regs │ │ │ └────────────┘ └────────────┘ └────────────┘ │ └─────────────────────────────────────────────────────────────┘
Process vs thread — the classic interview question#
| Process | Thread | |
|---|---|---|
| Memory | own, private | shared with other threads in the same process |
| Creating one | heavier (new memory map, kernel records) | lighter |
| Switching between | slower (page tables change, TLB flushed) | faster (same memory map) |
| Communication | needs files, pipes, sockets, shared memory | just read/write the same variables |
| If one crashes | others keep running | usually the whole process dies |
| Safety | isolated by the OS | you must prevent them corrupting shared data (Ch 8) |
When to use which: processes when you want isolation (one crash shouldn't kill the others — e.g. a web server running 4 worker processes). Threads when tasks need to share a lot of data cheaply.
In Java#
Thread t = new Thread(() -> System.out.println("hello from a thread"));
t.start(); // ask the OS to run it concurrently
t.join(); // wait until it finishes(Python has threads too, but with an important twist called the GIL — covered in Python Concurrency (coming soon).)
Gotchas
Threads share everything on the heap — including bugs. Two threads writing the same variable without coordination produce wrong results. That's the next chapter.
One thread crashing (for example, with a segmentation fault) usually takes down the whole process.
Check yourself
3 questionsWhat do threads in the same process share, and what is private to each?
Show answerHide answer
Shared: heap, globals, code, open files. Private: stack, registers, program counter.
Give three differences between a process and a thread.
Show answerHide answer
Any three: processes have private memory vs threads share; threads are cheaper to create and switch; threads communicate through shared variables vs processes need IPC; one thread crash kills the process vs process crashes are isolated.
Why might a web server use several processes instead of several threads?
Show answerHide answer
Isolation — one worker crashing or leaking memory doesn't take down the others.