Notes › Operating Systems
Process vs Thread
Process
A process is a program in execution. The program is a file on disk; when you run it, the OS creates a process with its own memory and resources. Opening Chrome three times creates three processes from one program.
Memory layout of a process
High address ┌───────────────┐
│ Stack │ ← function calls, local variables (grows down)
│ ↓ │
│ ↑ │
│ Heap │ ← dynamic memory: new / malloc (grows up)
│ Data │ ← global and static variables
│ Text (code) │ ← the program's instructions
Low address └───────────────┘
Process states
new → ready ⇄ running → terminated
↑ ↓
└─ waiting (blocked on I/O)
- Ready: waiting for the CPU. Running: on the CPU. Waiting: waiting for I/O or an event.
Process Control Block (PCB)
The OS keeps a PCB per process: process id, state, program counter, CPU registers, scheduling info (priority), memory info (page tables), open files, parent/child. The PCB is what gets saved and restored during a context switch.
Thread
A thread is the smallest unit of execution inside a process — a "lightweight process". One process can have many threads that run at the same time.
Threads of a process share: code, data (globals), heap, open files. Each thread has its own: stack, registers, program counter, thread id.
Process
├── code | data | heap | open files (shared)
├── Thread 1: stack + registers + PC
├── Thread 2: stack + registers + PC
└── Thread 3: stack + registers + PC
Process vs thread
| Process | Thread | |
|---|---|---|
| Definition | program in execution | unit of execution inside a process |
| Memory | own address space | shares the process's memory |
| Creation | slow (copy/allocate memory) | fast |
| Context switch | expensive (change memory map, flush TLB) | cheap |
| Communication | needs IPC (pipes, sockets, shared memory) | directly through shared variables |
| Isolation | one crash doesn't affect others | one thread crashing can kill the whole process |
| Example | each browser tab in Chrome | UI thread + network thread + rendering thread in one app |
Context switch
Switching the CPU from one process/thread to another:
- Save the state (registers, PC) of the current one into its PCB/TCB.
- Load the state of the next one.
- For processes, also switch the address space (page tables) — this is why process switches cost more. No useful work happens during a switch — it's overhead.
Creating processes and threads
// a new process in Unix: fork() copies the current process
pid_t pid = fork();
if (pid == 0) printf("child\n");
else printf("parent of %d\n", pid);
// a new thread in Java
Thread t = new Thread(() -> System.out.println("hello from " + Thread.currentThread().getName()));
t.start();
t.join(); // wait for it to finish
fork() returns 0 in the child and the child's PID in the parent. A process with n fork() calls in a row creates 2ⁿ processes in total.
Why multithreading?
- Responsiveness: the UI stays smooth while a background thread downloads.
- Speed on multi-core CPUs: threads run truly in parallel.
- Resource sharing: no need to copy data between workers.
- Economy: cheaper to create and switch than processes.
Problems with threads
- Race conditions: two threads update shared data at the same time.
- Deadlocks: threads wait for each other forever.
- Harder debugging: bugs depend on timing. Solutions: locks, semaphores, atomic variables — see Process Synchronization.
User-level vs kernel-level threads
| User-level | Kernel-level | |
|---|---|---|
| Managed by | a library in user space | the OS |
| Switching | very fast (no system call) | slower |
| Blocking I/O | blocks the whole process | blocks only that thread |
| True parallelism | no (OS sees one thread) | yes |
Mapping models: many-to-one, one-to-one (Linux, Windows), many-to-many.
Concurrency vs parallelism
- Concurrency: dealing with many tasks by switching between them (possible on one core).
- Parallelism: actually doing many tasks at the same instant (needs multiple cores).
Inter-process communication (IPC)
Pipes, named pipes (FIFOs), message queues, shared memory (fastest), sockets, signals.
Special processes
- Zombie: finished, but the parent hasn't read its exit status (
wait()) — the PCB entry remains. - Orphan: the parent died first;
init/systemdadopts it. - Daemon: a background service process.
Interview one-liners
- Process = program in execution with its own memory; thread = unit of execution sharing the process's memory.
- Threads share code, data, heap and files; each has its own stack and registers.
- Thread switches are cheaper than process switches (no address-space change).
fork()returns 0 to the child and the child's PID to the parent.- Related: CPU Scheduling Algorithms, Multithreading in Java.