Loading…
Loading…
Charting your lesson…
Opening your lesson…
Process vs Thread
Lesson 1 of 4
6 steps•about 12 min•12 flashcards
Step 1/ 61 min
"What is the difference between a process and a thread?" is perhaps the single most-asked OS interview question. The answer hinges on one idea: what memory is shared and what is private.
Core idea: a process is a program in execution with its own isolated address space (code, heap, data, stack). A thread is a unit of execution inside a process; threads of the same process share the address space (code, heap, globals) but each has its own stack and registers.
Step 2/ 61 min
A process is the "container"; threads are the "workers" inside it. Every process has at least one (the main) thread. Multiple threads let one process do several things at once — e.g. a browser tab rendering while another downloads.
| Resource | Across processes | Across threads (same process) |
|---|---|---|
| Address space / heap | isolated | shared |
| Global/static data | isolated | shared |
| Open files, sockets | isolated | shared |
| Stack | separate | separate (one per thread) |
| Registers / program counter | separate | separate |
Sharing the heap is the double-edged sword: threads communicate cheaply (just read/write shared memory) but require synchronisation (locks) to avoid races. Processes are isolated, so a crash in one cannot corrupt another — safer, but inter-process communication (IPC) is heavier.
Creating a process is expensive (the OS allocates a new address space, often copying page tables via copy-on-write fork). Creating a thread is cheap (share the address space, allocate a stack). A context switch between threads of the same process is faster than between processes, because the memory map (page tables, TLB) need not be swapped.
// Threads share globals -> a race without a lock:
int counter = 0;
// thread A: counter++ | thread B: counter++ -> may lose an update
IPC between processes uses pipes, sockets, shared memory or message queues; threads just share variables (with locks).
Step 4/ 61 min
Guaranteed question: "process vs thread". Nail these points: separate vs shared address space; threads are lighter to create and switch; threads need synchronisation because they share memory; a process crash is isolated while a thread crash can take down the whole process. Follow-ups: "why are threads called lightweight?" and "multiprocessing vs multithreading trade-offs".
Step 5/ 61 min
Believing threads have fully separate memory like processes do. Threads of one process share the heap, globals and open files — only the stack and registers are private. Assuming isolation leads to unguarded shared state and race conditions; assuming full sharing (including stacks) leads to confusion about why locals are thread-safe.
Step 6/ 6· Worked examples2 min
Problem
A program forks a child process AND, separately, spawns a thread. A global variable g = 0 is incremented in each. Explain what the parent sees in both cases and why, using the process/thread memory model.
int g = 0;
// Case 1: fork
pid_t pid = fork();
if (pid == 0) { g = 42; _exit(0); } // child
wait(NULL);
printf("after fork: g=%d\n", g); // (A)
// Case 2: pthread
void* worker(void* _) { g = 99; return NULL; }
pthread_t t; pthread_create(&t, NULL, worker, NULL);
pthread_join(t, NULL);
printf("after thread: g=%d\n", g); // (B)
Try it on paper first.
Recall
Keep this
"Difference between a process and a thread" is an OS interview certainty. It underpins concurrency, so panels use it to gate deeper questions on synchronization and parallelism. Know exactly what is shared and what isn't.
A process is a program in execution with its own address space (code, data, heap, stack) and OS resources. A thread is the smallest unit of execution within a process — a lightweight "thread of control" that shares the process's address space with sibling threads.
Threads of one process share code, data (globals), heap, and open files, but each thread has its own stack, registers, and program counter (PC). Processes share nothing by default — they are isolated.
| Aspect | Process | Thread |
|---|---|---|
| Address space | own, isolated | shared with siblings |
| Owns | code/data/heap/stack | own stack + registers + PC |
| Creation cost | high | low |
| Communication | IPC (pipes, sockets, shared mem) | shared memory directly |
| Crash impact | isolated | can crash whole process |
"Process vs thread", "what do threads share?", "why is thread context switch cheaper?", "multithreading vs multiprocessing", and "why do threads need synchronization?".
Process = isolated program in execution with its own address space; Thread = lightweight unit inside a process sharing code/data/heap/files but with its own stack/registers/PC. Threads are cheaper to create/switch and communicate via shared memory (needing synchronization); processes need IPC and are isolated.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.