Search code examples
clinuxkernelinterruptcontext-switch

Why processes cannot preempt interrupts?


I know when an interrupt occurs the process running is put on hold, and the Interrupt Service Routine is called. The current pointer is pointing to the process that was interrupted and I was told that when an interrupt occurs it is not linked to a specific process. So my question is why only another interrupt can preempt an existing interrupt routine?

Also, when a process(p2) preempts another process(p1), who is calling the schedule() method?


Solution

  • the first two answers both show some significant misunderstanding about interrupts and how they work

    Of particular interest,

    for the CPUs that we are usually using

    ( 86x.., power PC, 68xxx, ARM, and many others)

    each interrupt source has a priority.

    sadly, there are some CPUs, for instance the 68HC11, where all the interrupts, except the reset interrupt and the NMI interrupt, have the same priority so servicing any of the other interrupt events will block all the other (same priority) interrupt events.

    for our discussion purposes, a higher priority interrupt event can/ will interrupt a lower priority interrupt handler.

    (a interrupt handler can modify the appropriate hardware register to disable all interrupt events or just certain interrupt events. or even enable lower priority interrupts by clearing their own interrupt pending flag (usually a bit in a register)

    In general, the scheduler is invoked by a interrupt handler,

    (or by a process willingly giving up the CPU)

    That interrupt is normally the result of a hardware timer expiring/reloading and triggering the interrupt event.

    A interrupt is really just an event where the event is waiting to be serviced.

    The interrupt event, when allowed, for instance by being the highest priority interrupt that is currently pending, will cause the PC register to load the first address of the related interrupt handler.

    the act of diverting the PC register to the interrupt handler will (at a minimum) push the prior PC register value and status register onto the stack. (in some CPUs, there is a special set of save areas for those registers, so they are pushed onto the special area rather than on the stack.

    The act of returning from an interrupt, for instance via the RTI instruction, will 'automatically' cause the prior PC and status register values to be restored.

    Note: returning from an interrupt handler does not clear the interrupt event pending indication, so the interrupt handler, before exiting needs to modify the appropriate register otherwise the flow of execution will immediately reenter the interrupt handler.

    The interrupt handler has to, upon entry, push any other registers that it modifies and, when ready to exit, restore them.

    Only interrupts of a lower priority are blocked by the interrupt event diverting the PC to the appropriate interrupt handler. Blocked, not disabled.

    on some CPUs, for instance most DSPs, there are also software interrupts that can be triggered by an instruction execution. This is usually used by hardware interrupt handlers to trigger the data processing after some amount of data has been input/saved in a buffer. This separates the I/O from the processing thereby enabling the hardware interrupt event handler to be quick and still have the data processed in a timely manner

    The above contradicts much of what the comments and other answers state. However, those comments and answers are from the misleading view of the 'user' side of the OS, while I normally program right on the bare hardware and so am very familiar with what actually happens.