CodePark/ questions / Processes & threads
START PRACTICINGPRACTICE
Backend question · OS, networking & Linux › Processes & threads

Why is switching between two threads of the same process usually cheaper than switching between two separate processes?

Threads of one process share an address space, so the kernel doesn't have to switch page tables

Threads of one process share an address space, so the kernel doesn't have to switch page tables. Page tables map the memory addresses a program uses to real memory.

Both kinds of switch save and restore the registers and the stack pointer. A process switch also changes the address space. This means the kernel loads different page tables. It usually also loses the cached address translations, so the next memory accesses are slower.


"Threads are scheduled by the application, so the kernel is not involved" is wrong. Linux threads are scheduled by the kernel, so the kernel is involved in both cases. "Threads have no registers of their own, so there is nothing to save" is also wrong. Each thread has its own registers.