What happens the most, context switches or mode switches?

5,592

A context switch between processes always involves entering supervisor mode at the processor level. The scheduler needs to access the process table and the next process's memory map, both of which are not accessible to the old process and therefore require privilege elevation; and then the scheduler needs to point the MMU to the new process's memory map, which still requires elevated privileges.

A context switch between threads of the same process needn't involve the kernel at all.

So which one happens most often depends on whether you have many lightweight threads on your system.

Background reading: beyond Wikipedia, this article (context switches at the Linux Information Project). And of course Understanding the Linux Kernel (chapter 3).

Share:
5,592

Related videos on Youtube

Lucas Kauffman
Author by

Lucas Kauffman

I'm a Belgian security consultant living in Singapore, I'm here to learn and help others out. Opinions are my own. Advice provided with no warranty. Find me on http://cloud101.eu Sometimes you can have a craving only hands can satisfy!

Updated on September 18, 2022

Comments

  • Lucas Kauffman
    Lucas Kauffman over 1 year

    Which happens the most, context switches or mode switches?

    I have two answers myself, but I do not know which one is correct:

    1. Context switches happen in user mode, but this does not mean that a system call is needed; therefore, mode changes do not happen when a context switch occurs.

    2. Context switches mean a dispatch is needed. I think this is privileged, so a mode change from user to kernel mode is needed to do a context switch. Which means a context switch goes along with a mode switch.

    Anyone have a definite answer to this?

    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' almost 13 years
    • Tobu
      Tobu almost 13 years
      Don't know if that's how you intend the question, but if there's a little bit of syscalls (say, io that won't block), the mode switches for those should dominate.