Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
1  
Gratuitious advertising: Proposed site on operating system development –  Gilles Jun 23 '11 at 23:11
    
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. –  Tobu Jun 24 '11 at 0:54
add comment

1 Answer

up vote 3 down vote accepted

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|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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