Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context switch in Interrupt handlers

Why can't a context switch happen when an interrupt handler is executing ? More specifically, in the linux kernel, interrupt handlers run in the context of the process that was interrupted. Why is it not possible to do a context switch in the interrupt handler to schedule another process ?

like image 961
Bandicoot Avatar asked Jan 19 '11 06:01

Bandicoot


2 Answers

On a multiprocessor, a context switch can certainly happen while an interrupt handler is executing. In fact, it would be difficult to prevent.

On a single-CPU machine, by definition it can only be running one thread of control at a time. It only has one register set, one ALU, etc. So if the interrupt handler is running there simply are no resources with which to execute a context switch.

Now, if you mean, can the interrupt handler actually call the context switch code and make one happen, well, I suppose on some systems that could be made to work. But for most, this wouldn't have much value and would be difficult to arrange. The CPU is running at elevated priority, and this priority cannot be lowered or synchronization between interrupt levels is lost. Critical sections in the OS are already synchronizing against interrupt execution and this would introduce complexities. Furthermore, a context switch happens by changing stacks, much like in a threaded user mode program, so it's hard to imagine how this might happen when the interrupt stack is needed for a return from the interrupt.

like image 101
DigitalRoss Avatar answered Sep 21 '22 05:09

DigitalRoss


A couple of reasons, I guess, depending on the meaning of your question:

  1. Q: Why would context switching during an interrupt be bad?

    A: Interrupts are generally for interacting with hardware. Hardware is typically time-sensitive so the OS can't just stop dealing with it in the middle of something and come back when it feels like it.

  2. Q: What stops a context switch from happening during an interrupt?

    A: An interrupt happens in a special interrupt context, not a regular process context. Since it's not in a process, it's not subject to context switching as a normal process would be.

There's probably a better, deeper explanation to be made, but that's the extent of my own understanding of the matter.

like image 29
Nate C-K Avatar answered Sep 22 '22 05:09

Nate C-K