Interview frequency: …
Short answer
A process is an independent program with its own isolated virtual address space. Threads in the same process share memory, files, and the heap, while each has its own stack.
Explanation
Switching context between processes is expensive and slow because the OS kernel must switch page tables, flush the TLB (Translation Lookaside Buffer), and clear processor cache lines. Switching between threads in one process is much faster because the memory space and page tables do not change, but shared memory creates many opportunities for race conditions and data corruption.
Code example
import multiprocessing as mp
import threading
counter = 0
def increment():
global counter
counter += 1
if __name__ == "__main__":
t = threading.Thread(target=increment)
t.start(); t.join()
print(counter) # 1 — threads share the same memory
p = mp.Process(target=increment)
p.start(); p.join()
print(counter) # still 1 — the child process incremented its own isolated copyCommon mistakes
- Believing that creating thousands of threads always speeds up a program; too many threads cause Thread Thrashing, where the processor spends most of its time switching contexts instead of doing useful work.