Process vs Thread: Differences Explained

A process is a running program with its own private address space and resources. A thread is a unit of execution inside a process. All the threads of one process share its memory, which makes them cheap to create and fast to communicate, but also means that one bug or crash can affect the whole process. Separate processes are isolated from each other, which is safer but costs more to create and to talk across.

AspectProcessThread
What it isA running program with its own address space, files and other resourcesA path of execution inside a process, with its own stack and registers
MemorySeparate virtual address space, isolated from other processesShares the code, heap and global data of its process; only the stack is private
Creation costHeavier: a new address space must be set up (copy-on-write reduces the cost)Lighter: no new address space
Context switchMore expensive: the address space changes, and cached translations (TLB) may be lostCheaper: the address space and caches stay the same
CommunicationNeeds inter-process communication such as pipes, sockets or shared memoryReads and writes shared memory directly, which is fast
Isolation and faultsA crash usually stays inside the processA crash in one thread can bring down the whole process
SynchronizationNeeded when processes share memory or filesAlways needed for shared data: mutexes and atomics, or the program has data races and deadlocks
SchedulingContains one or more threadsThe unit that the operating system actually schedules onto CPU cores
Typical usesIsolation and security: browser tabs, services, shell pipelinesParallel work inside one program: servers handling many connections, background tasks, parallel computation

Figures are typical orders of magnitude and vary by process generation and product.

When to use which

Process
When you want isolation, robustness or security, or when pieces of software are written by different parties and should not share memory.
Thread
When tasks need to share data quickly and you want to use several CPU cores inside one program.
Care needed
Threads sharing memory is both their strength and their danger. Protect shared data with locks or atomic operations, and watch out for deadlock.

FAQ

Do threads share memory?

Yes. Threads in one process share its code, heap and global data. Each thread has its own stack and registers.

Why are threads cheaper to create than processes?

A new process needs its own address space, while a thread reuses that of its process. Switching between threads also avoids changing the address space, so it costs less.

What happens if one thread crashes?

It can bring down the whole process, because the threads share memory. A crash in one process usually does not affect other processes.

When should I use processes instead of threads?

When you want isolation and robustness, such as browser tabs or independent services, or when different parts should not be able to corrupt the memory of the other parts.

Check your understanding

What is the advantage of using copy-on-write when duplicating a process?

  1. AThe copy and the original always share whatever either one writes
  2. BOnly pages that get written are copied later, so it is fast and saves memory
  3. CEvery page is copied on each write, improving data safety
Show answer and explanation

Answer: B. Only pages that get written are copied later, so it is fast and saves memory

Copying all memory immediately when duplicating a process is slow and memory-hungry, and wasted if a new program is loaded right after. With copy-on-write, both page tables point to the same physical pages marked read-only; when either side writes, a page fault occurs and the OS copies just that page. Written data is not shared but kept separately in each process, and pages are not all copied.

During a context switch, what must be saved for the process being switched out?

  1. AThe contents of the disk files the process is using
  2. BCPU execution state such as register values and the program counter
  3. CThe entire contents of the process's physical memory
Show answer and explanation

Answer: B. CPU execution state such as register values and the program counter

A context switch saves CPU state, such as general registers, the program counter, the stack pointer and the operating mode, so the process can later resume exactly where it left off, and loads the state of the next process. When switching processes, the register pointing to the page table is also switched. Memory and file contents remain in place, so they need not be saved on each switch.

Why can a process context switch degrade performance more than the time spent saving and restoring registers would suggest?

  1. AThe CPU clock frequency drops on every switch
  2. BThe program is reloaded from disk on every switch
  3. CCache and TLB misses rise after the switch
Show answer and explanation

Answer: C. Cache and TLB misses rise after the switch

Saving and restoring registers is quick, but after the switch the caches and TLB hold the previous process's contents, so the new process misses until its data and translations are reloaded. On CPUs without ASIDs, the TLB must also be flushed when changing address spaces. This indirect cost can exceed the direct switching time. The clock does not slow down, and programs are not reloaded from disk.

  1. What is the advantage of tagging each TLB entry with an ASID (address space identifier)?
  2. Which correctly compares a mutex (a lock that sleeps while waiting) with a spinlock?
  3. Which is an example of deadlock?

Practice OS & Hardware questions

Key terms explained

Context switch
Switching the process or thread running on a CPU. Registers, the program counter and other state are saved and restored, and switching processes also switches address spaces.
Mutex
A lock that only one thread can hold at a time. Typically a thread that cannot get it sleeps and yields the CPU, and the OS wakes it when the lock is released.
Deadlock
A state in which threads each hold a resource while waiting for one held by another, so none can proceed. Fixing a global lock order prevents it.

Related comparisons