In the early days of computing, CPUs were purely sequential machines.
This helped to keep designs simple.
However, it also limited performance.
Many processes will need to request data from system RAM or the hard drive.
Here the CPU can be idle for significant periods, waiting for a response.
Unfortunately, with sequential processors, this issue is simply unavoidable.
Thankfully, modern CPUs are no longer sequential.
They offer many advanced features, such as out-of-order execution and multiple threads.
Out-of-order execution allows the CPU to analyze upcoming instructions and reorder them to maximize efficiency.
Multi-threading allows the CPU to have numerous different threads or processes running.
Outside of having multiple cores, the CPU cant run more than one at a time.
The process of switching between threads is called a context switch.
Contents
How Does a Context Switch Work?
A context switch consists of two parts, switching out the previous thread and switching in the new one.
This includes the values of any relevant CPU registers and always consists of the value of the program counter.
Switching in the following thread is the same process in reverse.
A thread is selected either from the ready queue, depending on weighting.
The data for the thread is then copied into the correct registers, and the thread is restored.
At this point, the new thread is ready to continue operation from where it stopped.
There are, however, further performance costs.
This can lead to a significant increase in TLB (Translation Lookaside Buffer), and cache misses.
The TLB must be flushed entirely when switching between threads from different methods.
While CPUs offer hardware support for context switching, operating systems typically dont use this.
Hardware context switching lacks awareness of the relevance of data.
Therefore it needs to store and restore all registers, increasing the time taken and the storage space required.
Additionally, hardware context switching doesnt store the data from floating point registers, functionality that may be necessary.
Software context switching is, therefore, generally used.
It allows keeping the data from all registers, including floating point registers.
Software context switches do have an understanding of the relevancy of data.
This means it can pick and choose which ones to store as needed.
The process involves storing the current threads relevant data and restoring the new threads pertinent data.