Process vs Thread in Operating Systems: Memory, Context Switching and When to Use Each
What a process owns, what threads share, why context switches cost time, and the questions interviewers ask.
Coding problems with instant test-case feedback
Write Python, C++, C or JavaScript in your browser and see which hidden tests pass.
A process is a running program with its own address space. A thread is a unit of execution inside a process. Every process has at least one thread, and threads of the same process share its memory. Most OS interview questions start from this distinction.
1. What a process owns#
The OS keeps a Process Control Block (PCB) for each process: process ID, state (new, ready, running, waiting, terminated), program counter, CPU registers, scheduling information, the memory map and the list of open files.
A process’s memory has a code (text) segment, a data segment for globals, a heap for dynamic allocation and a stack for function calls. Another process cannot read this memory directly; the OS enforces the separation.
2. What threads share and what they keep#
Threads in one process share the code, data, heap and open files. Each thread has its own stack, program counter and registers, kept in a Thread Control Block. Sharing memory makes communication fast, and also makes data races possible.
| Process | Thread | |
|---|---|---|
| Memory | Separate address space | Shares the process’s address space |
| Creation cost | Higher (new address space, page tables) | Lower |
| Communication | IPC: pipes, sockets, shared memory, message queues | Read and write shared variables (with locks) |
| Context switch | Slower: address space changes, TLB is flushed or tagged | Faster within one process |
| Fault isolation | A crash usually affects only that process | A crashing thread can bring down the whole process |
Practise placement aptitude on the real test pattern
TCS NQT, Infosys, Wipro, Accenture and Cognizant style mocks with section-wise analysis.
3. What happens in a context switch#
A context switch happens when the scheduler stops one thread and runs another, because of a timer interrupt, a blocking I/O call or a higher-priority thread becoming ready.
- Save the current thread’s registers and program counter into its control block.
- Pick the next thread to run (scheduling).
- If the new thread belongs to a different process, switch the address space (page table base register) and invalidate or tag TLB entries.
- Restore the new thread’s registers and jump to its program counter.
The direct cost is saving and restoring registers. The bigger indirect cost is cold caches and TLB misses after switching processes, which is why thread switches inside one process are cheaper.
4. Threads in code#
Two threads incrementing a shared counter need a lock, or updates are lost.
| 1 | import java.util.concurrent.atomic.AtomicInteger; |
| 2 | |
| 3 | public class Counter { |
| 4 | public static void main(String[] args) throws InterruptedException { |
| 5 | AtomicInteger count = new AtomicInteger(); |
| 6 | Runnable work = () -> { |
| 7 | for (int i = 0; i < 100_000; i++) count.incrementAndGet(); |
| 8 | }; |
| 9 | Thread t1 = new Thread(work), t2 = new Thread(work); |
| 10 | t1.start(); t2.start(); |
| 11 | t1.join(); t2.join(); |
| 12 | System.out.println(count.get()); // always 200000 |
| 13 | } |
| 14 | } |
5. Choosing between them#
Chrome runs each site in a separate process so a crashing or compromised tab cannot read another tab’s memory. A web server typically uses a pool of threads (or async I/O) inside one process because requests share caches and connection pools. Use processes for isolation and safety, threads for cheap sharing and low overhead.
Test Your Concept Mastery
Verify your retention on: Process vs Thread in Operating Systems: Memory, Context Switching and When to Use Each
Which of these is NOT shared between threads of the same process?
Why is a context switch between processes usually slower than between threads of one process?
Ready for a full proctored assessment?
Take a free aptitude mock on a real company pattern, with a worked solution for every question.