Context Switch in Interrupt Handlers

Why can't the context transition occur when executing the interrupt handler? More specifically, in the linux kernel, interrupt handlers are executed in the context of the process that was interrupted. Why is it impossible to execute the context switch in the interrupt handler to schedule another process?

+4
source share
2 answers

In multiprocessor mode, the context switch can certainly occur during the execution of the interrupt handler. In fact, it will be difficult to prevent.

On a single processor machine, by definition, only one control thread can run at a time. It has only one set of registers, one ALU, etc. Therefore, if the interrupt handler works, there simply are no resources to execute the context switch.

Now, if you mean, can the interrupt handler actually call the context switch code and do this, well, I suppose, on some systems that can be made to work. But for most, this would not be of great value and would be difficult to organize. The CPU operates with a higher priority, and this priority cannot be reduced or the synchronization between interrupt levels is lost. Critical sections in the OS are already synchronized with the execution of interrupts, and this will create difficulties. In addition, the context switch occurs by changing the stacks, as in a program with streaming user mode, so it is difficult to imagine how this can happen when the interrupt stack is needed to return from the interrupt.

+7
source

Several reasons, I think, depending on the meaning of your question:

  • Interrupts are usually for interacting with equipment. Hardware usually depends on the time, so the OS cannot just stop communicating with him in the middle of something and returns when he likes it.

  • The interrupt occurs in the special context of the interrupt, and not in the normal context of the process. Since this is not a process, it is not subject to context switching, as there will be a normal process.

There should probably be a better, deeper explanation, but as far as I understand it myself.

+3
source

Source: https://habr.com/ru/post/1336092/


All Articles