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?


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).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.