Processes, Context Switches and What They Cost
A process is an active program instance with execution state and OS-managed resources. It may be ready, running or blocked. A process switch saves and restores CPU execution state and selects the next process's existing address-space context. Memory mappings and open-file records remain stored, rather than being copied into saved CPU state. Context handling takes time; added cache costs depend on hardware and workload.
What a learner can do afterwards
- Distinguish saved and restored CPU context from process memory and resource records that remain stored
- Follow a process through the ready, running and blocked states
- Explain why a switch costs more than the register copying suggests
1 · Read
You have followed requests into the kernel. The kernel also tracks processes: active instances of programs with execution state, memory and resources such as open files. A process still exists when it is not executing. One application may use several processes, and several processes may run the same program.
An address space is the memory-address view available to a process. In this simplified model of single-threaded processes, the kernel saves A's CPU execution state, selects B's existing address-space context, restores B's saved CPU state, and resumes B. Saved CPU state includes the instruction position and required register values. Its exact contents depend on the processor and OS. A blocking request can let the kernel schedule another ready process.
A switch need not clear caches. Some needed instructions or data may remain cached. A missing line causes a miss in that cache and a fetch from a lower memory level, which may be another cache or main memory. Competing working sets may evict useful lines. The added cost depends on the cache hardware, what remains cached and the incoming process's accesses.
Saving and restoring context and running scheduler code take time even when needed data remains cached. More frequent switches can leave less time for program work, and cache disruption can add further cost. The size of the effect depends on the hardware, workload and implementation. A switch is not necessarily harmful: it can let useful work proceed while another process waits.
A paused process retains its memory mappings and open-file records. A switch selects the next process's existing address-space context, which tells the hardware how to use that process's mappings. It does not copy every memory byte or recreate its open files. Saving CPU execution state differs from retaining these resources. Address-space selection can require hardware-control updates; the details and cost depend on the processor and OS.
For a register-copy count, specify how many individual copies each switch performs, including both saving and restoring. Multiply that total by the number of switches. In a toy model, two switches with twelve total register copies each perform 2 × 12 = 24 copies. This counts copies only, not scheduler time or cache costs.
A process can be ready, running or blocked. Switches save and restore CPU state, select address-space context and retain resources. Context work takes time; cache costs vary by hardware and workload.
Original lesson, questions and flashcards written for this dataset.
Original readings and flashcards written for this dataset.
2 · Watch
به انگلیسیTake it off screen
Where it sits
8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.