What Is the Difference Between a Process and a Thread?
Understand the difference between a process and a thread in operating systems, including memory isolation, creation cost, and how each is scheduled by the OS.
Expected Interview Answer
A process is an independent program in execution with its own private memory space, while a thread is a lightweight unit of execution inside a process that shares that memory with other threads of the same process.
The operating system allocates each process its own address space, file handles, and resources, so one process crashing does not directly corrupt another. Threads live inside a process and share its code, heap, and open files, but each thread keeps its own stack, program counter, and registers. Because threads share memory, communication between them is fast (just read/write shared variables) but requires synchronization to avoid race conditions. Creating and switching processes is expensive since the OS must set up a new address space; creating and switching threads is cheaper because most of that context is already shared.
- Processes give strong isolation and fault containment
- Threads give lightweight, low-overhead concurrency
- Threads share memory for fast inter-thread communication
- Processes protect the whole system from a single crash
- Choosing the right one balances isolation against performance
AI Mentor Explanation
A process is like a full playing eleven with its own dressing room, kit, and strategy meeting, completely separate from the opposition's. A thread is like one bowler in that same team's attack, sharing the dressing room, the team plan, and the scoreboard with the other bowlers, so swapping bowlers between overs costs almost nothing compared to fielding an entirely new team.
Step-by-Step Explanation
Step 1
Process isolation
The OS gives each process its own virtual address space, so one process cannot directly read or write another's memory.
Step 2
Thread shares the process
All threads in a process share its code, heap, and open file descriptors, but each keeps a private stack and register set.
Step 3
Creation cost
fork() or CreateProcess duplicates or sets up a full address space; creating a thread just allocates a stack inside the existing one.
Step 4
Context switch cost
Switching processes flushes the TLB and swaps page tables; switching threads within a process avoids most of that work.
Step 5
Communication
Threads communicate via shared variables (needing locks); processes need IPC mechanisms like pipes, sockets, or shared memory segments.
What Interviewer Expects
- Defines process as an isolated unit with its own address space
- Defines thread as a lightweight unit sharing a process's memory
- Explains why thread creation/switching is cheaper than process creation/switching
- Mentions synchronization needs (locks) for shared thread state
- Names IPC mechanisms processes use to communicate
Common Mistakes
- Saying threads and processes are basically the same thing
- Claiming threads have completely separate memory like processes
- Forgetting that threads still need their own stack and registers
- Ignoring the need for synchronization when threads share data
Best Answer (HR Friendly)
“A process is like a separate running program with its own private workspace, while a thread is a smaller task inside that program that shares the same workspace with other tasks. Threads are quicker to create and switch between because they don't need a whole new workspace set up, but they do need care so they don't step on each other's shared data.”
Code Example
import threading, multiprocessing
def work(n):
return sum(range(n))
# Thread: shares memory with the main process
t = threading.Thread(target=work, args=(1_000_000,))
t.start()
t.join()
# Process: gets its own isolated memory space
p = multiprocessing.Process(target=work, args=(1_000_000,))
p.start()
p.join()
Follow-up Questions
- What is a race condition and how do you prevent one?
- Why does Python's GIL limit true multithreaded CPU parallelism?
- When would you choose multiprocessing over multithreading?
- What system calls create a new process on Linux?
- How does the OS schedule threads versus processes?
MCQ Practice
1. What does a thread share with other threads in the same process?
Threads within a process share the code segment, heap, and file descriptors, while each keeps its own stack and registers.
2. Why is creating a new process more expensive than creating a new thread?
A new process requires a fresh address space, page tables, and resource handles, which is far more work than allocating a thread's stack.
3. How do processes typically communicate with each other?
Because processes have isolated memory, they rely on IPC mechanisms such as pipes, sockets, or shared memory segments to exchange data.
Flash Cards
What is a process? — An independent, isolated program in execution with its own address space and resources.
What is a thread? — A lightweight unit of execution inside a process that shares its memory but has its own stack.
Which is cheaper to create, a process or a thread? — A thread — it reuses the process's existing address space instead of setting up a new one.
How do threads communicate? — Directly through shared memory, which requires synchronization like locks to avoid race conditions.