• Skip to main content
  • Skip to primary sidebar

Technipages

Tutorials and fixes for smartphone, gadget, and computer problems

  • Topics
    • Android
    • Browsers
    • Gaming
    • Hardware
    • Internet
    • iPhone
    • Linux
    • macOS
    • Office
    • Reviews
    • Software
    • Windows
    • Definitions
  • Product Reviews
  • Downloads
  • About
What Is a Context Switch?

What Is a Context Switch?

October 4, 2022 by Mel Hawthorne 1 Comment

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. While system RAM is fast, it’s still not as fast as the CPU, leaving it sitting idle, waiting for data until the response comes back from the RAM. The situation is even worse for data requested from the hard drive, a storage device much slower than RAM. 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 can’t run more than one at a time. It can, however, make it look like it by switching between them regularly to ensure they each get an appreciable amount of constant CPU time. The process of switching between threads is called a context switch.

How Does a Context Switch Work?

A context switch consists of two parts, switching out the previous thread and switching in the new one. To change the old thread, the CPU must save its current state to a Process Control Block or switch frame. This includes the values of any relevant CPU registers and always consists of the value of the program counter. Once the thread has been stored, a handle can be added to a ready queue to allow it to be restored when needed.

Switching in the following thread is the same process in reverse. A thread is selected either from the ready queue, depending on weighting. Alternatively, it can be chosen by an interrupt indicating that an event the thread was waiting on is now ready or complete. 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.

Performance Impact

The process of reading and writing data when switching a thread in or out takes some time, though not much, as the memory used is typically high-speed. There are, however, further performance costs. When switching threads, the data in the CPU caches and buffers from the previous thread, may not be relevant to the new thread. This can lead to a significant increase in TLB (Translation Lookaside Buffer), and cache misses.

This effect isn’t significant if the two threads were spawned by the same process, as they are likely to share considerable memory elements. The TLB must be flushed entirely when switching between threads from different methods. This leads to a 100% TLB miss rate while the hit rate of the CPU cache is also significantly reduced.

While CPUs offer hardware support for context switching, operating systems typically don’t 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 doesn’t 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.

Conclusion

A context switch is a process by which a modern CPU switches which thread it is running. The process involves storing the current thread’s relevant data and restoring the new thread’s pertinent data. Context switching comes with a performance cost related to the time needed to perform the switch, and the increased rate of cache and TLB misses as these are not stored. Context switches happen either to ensure that all threads have a good supply of CPU time or because of an interrupt indicating that an event the line was waiting on is complete.

You Might Also Like

  • Switch vs Switch OLED - Who Wins?
    Switch vs Switch OLED - Who Wins?
  • Switch vs Switch Lite - Which One Is Better?
    Switch vs Switch Lite - Which One Is Better?
  • Windows 11: How to Get Old Context Menu Back
    Windows 11: How to Get Old Context Menu Back
  • Disable "Share with Skype" from Context Menu
    Disable "Share with Skype" from Context Menu
  • MS Teams Search: Show Context Around Old Messages
    MS Teams Search: Show Context Around Old Messages
  • What is a VPN Kill Switch and Why You Have to Use One?
    What is a VPN Kill Switch and Why You Have to Use One?

Filed Under: Hardware

Reader Interactions

Comments

  1. Amir madadi says

    October 14, 2022 at 11:46 pm

    /a/my-domain.com

Did this help? Let us know!

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Recent Posts

  • How to Change the Profile Picture on Disney+
  • Local Security Policy: What Is It and How to Open It in Windows 11
  • How to Create and Erase an Alarm in Windows 11
  • 10 Best NAS Storage Device for Home and Professional Use
  • PS5 NVMe Requirements You Must Fulfill to Expand PS5 Storage
  • Steam Deck Won’t Turn On: 8 Possible Fixes You Can Try
  • How to Fix Two-Finger Scroll Not Working Issue on Windows 11
  • Fix: No Sound Coming From Windows 11 Computer

Who’s Behind Technipages?

Baby and Daddy My name is Mitch Bartlett. I've been working in technology for over 20 years in a wide range of tech jobs from Tech Support to Software Testing. I started this site as a technical guide for myself and it has grown into what I hope is a useful reference for all.

© Copyright 2023 Guiding Tech Media · All Rights Reserved · Privacy