Skip to main content

Process vs thread; context switching

Short answer

In process switching, the entire address space must be changed, flushing caches. Threads within the same process share address space and resources; switching among them only saves program counter and registers, avoiding memory-map switches and TLB flushes, greatly reducing overhead.

Difference between process and thread. Explain how thread based execution minimizes context switching problem of process based execution.

Process vs thread

BasisProcessThread
MeaningProgram in execution with its own resourcesUnit of execution inside a process
Address spacePrivateShared with other threads of the process
Creation and switch timeHighLow
CommunicationIPC mechanisms (pipes, messages)Shared memory, directly
ProtectionProtected from other processesNone among threads of the same process
Control blockOwn PCB with memory maps, filesSmall thread control block (PC, registers, stack)

How threads minimise context-switch cost

In process-based execution, a context switch must save the registers and PCB of the old process, switch the address space (memory maps, page tables) and usually flush the TLB and cache of the old process. This is heavy overhead and no useful work is done meanwhile.

In thread-based execution, the concurrent activities are threads of one process. When the CPU switches between threads of the same process:

  • only the program counter, registers and stack pointer must be saved and restored (a small thread control block);
  • the address space, open files and other resources are shared and unchanged, so no memory-map switch and no TLB/cache flush is needed;
  • with user-level threads the switch is done by a library procedure in user space without even trapping to the kernel.

Therefore thread switching is much faster and the system spends more time on useful work. Moreover, threads share data directly, so the number of expensive IPC-induced process switches is reduced (for example, a web server uses one thread per request instead of one process per request).

✓ Answer checked by Claude Opus 5.5 (high effort)

All Operating System chapter 2 questions →