Single Processor Cache and Context Switching
Context Switching in Operating Systems
Illustration of Context Switching
Operating system swaps execution between processes P1 and P2 to share CPU resources.
Process Control Blocks (PCBs) for P1 and P2 reside in memory.
CPU values change depending on the currently executing process.
Definition of Context Switch
A context switch is a mechanism used by the operating system to switch execution from one process to another.
An example of this occurs when the OS switches from executing process P1 to process P2, and vice versa.
Cost of Context Switching
Context switching can be expensive due to:
Direct Costs
These involve the number of CPU cycles needed to load and store values of the PCBs to and from memory.
Indirect Costs
While process P1 runs, its data is stored in the processor cache.
Modern CPUs utilize a multi-level cache hierarchy (L1, L2, etc.), increasing in size but potentially slower at each level.
Access to cache is significantly faster compared to memory.
Example: Access time for cache is measured in a few cycles, while memory access can take hundreds of cycles.
Cache Behavior During Context Switching
When P1 executes, its data is likely to reside in the cache, resulting in a "hot" cache situation.
Upon switching to P2, P1's data may be replaced in the cache to accommodate P2’s data.
When P1 is scheduled to execute again, its data will not be in the cache, leading to longer access times from memory known as a "cold cache" situation.
Implications of Cold Cache
Running with a cold cache results in increased latency for data accesses due to slower memory access times.
Increased latency negatively impacts the execution speed of processes.
It is essential to limit the frequency of context switches to reduce the overhead associated with cold caches.