OOPs HandbookProcess vs Thread in Operating Systems: Memory, Context Switching and When to Use Each
Operating SystemsIntermediate
8 min read

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.

TM
TestMeMan SDE Curriculum
Updated Sept 2026
Verified for FAANG Interviews
FROM TESTMEMAN•TestMeMan Campus

Coding problems with instant test-case feedback

Write Python, C++, C or JavaScript in your browser and see which hidden tests pass.

Sponsored Publisher Placement•
ID: ad-top-leaderboard
Executive Summary

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.

ProcessThread
MemorySeparate address spaceShares the process’s address space
Creation costHigher (new address space, page tables)Lower
CommunicationIPC: pipes, sockets, shared memory, message queuesRead and write shared variables (with locks)
Context switchSlower: address space changes, TLB is flushed or taggedFaster within one process
Fault isolationA crash usually affects only that processA crashing thread can bring down the whole process
Sponsored ContentTestMeMan Campus

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.
Why switches cost more than they look

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.

1import java.util.concurrent.atomic.AtomicInteger;
2
3public 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}
Analysis: count++ is a read, an add and a write. Without an atomic operation or a lock, two threads can read the same value and one update is lost.

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

Diagnostic Check
Question 1 of 2
Which of these is NOT shared between threads of the same process?
Question 2 of 2
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.

Take an aptitude mock