2

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Roxana Istrate
  • 632
  • 1
  • 6
  • 18
  • Whatever `schedule()` is it is quite probably called by a timer interrupt, say `p3`. If the timer interrupt has a higher priority than your `p2` it will interrupt `p2` and that too will be put on hold. If not, then `p3` cannot start until `p2` has finished, and finally control will be returned to `p1`. – Weather Vane Mar 29 '15 at 09:50
  • 1
    who told you that when an interrupt occurs it is not linked to a specific process ? – stdcall Mar 29 '15 at 10:01
  • @stdcall depends what is meant by 'interrupt' and 'linked'. A hardware interrupt has no process state while it executes; an Excel recalcuation may be interrupted by a NIC that signals the completion of a network read for a BitTorrent client. An interrupt-handler must make no assumptions about what process it has interrupted. I suspect that this is what was implied by the OP. – Martin James Mar 29 '15 at 10:47

3 Answers3

2

So my question is why only another interrupt can preempt an existing interrupt routine?

A hardware interrupt usually puts the processor hardware in an interrupt state where all interrupts are disabled. The interrupt-handler can, and often does, explicitly re-enable interrupts of a higher priority. Such an interrupt can then preempt the lower-priority interrupt. That is the only mechanism that can interrupt a hardware interrupt.

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

That depends somewhat on whether the preemption is initiated by a syscall from a thread already running, or by a hardware interrupt that causes a handler/driver to run and subsequently enter the kernel to request a reschedule. The exact mechansims, (states, stacks etc), used are architecture-dependent.

Martin James
  • 24,453
  • 3
  • 36
  • 60
2

Regarding your first question: While an interrupt is running, interrupts are disabled on that processor. Therefore, it cannot be interrupted.

Regarding your second question: A process never preempts another process, it is always the OS doing that. The OS calls the scheduler routine regularly, where it decides which process will run next. So p2 doesn't say "i want to run now", it just has some attributes like a priority, remaining time slot, etc., and the OS then decides whether p2 should run now.

Philipp Murry
  • 1,660
  • 9
  • 13
  • 'The OS calls the scheduler routine regularly' -well not wrong usually, but somewhat economical with the truth. The scheduler may be run by a syscall that changes process/thread state or by a hardware interrupt and driver that requests a scheduling run, The timer interrupt you reference is just one of those interrupt sources, Arguably, disk, NIC KB, mouse, memory-management driver interrupts are more important. – Martin James Mar 29 '15 at 10:52
  • 'While an interrupt is running, interrupts are disabled on that processor' -- is it so? I thought that it's only line for running interrupt is disabled, and if another interrupt happens to occur -- previous interrupt will be stopped and new interrupt will be handled (at least on ARM, if second interrupt has greater priority). Read [this](http://stackoverflow.com/questions/11403915/can-an-interrupt-handler-be-preempted-by-the-same-interrupt-handler#comment15062053_11411460). – Sam Protsenko Mar 29 '15 at 11:03
  • Seems like you are right. Interrupt disabling behavior was changed in [this](http://git.kernel.org/cgit/linux/kernel/git/tip/tip.git/commit/?id=6932bf37bed45ce8ed531928b1b0f98162fe6df6) patch. Now we don't have **fast** and **slow** interrupts. All interrupts are treated as **fast**, and slow ones must be implemented as threaded (or enable interrupts manually in ISR using `local_irq_enable_in_hardirq()`). So yes, all interrupts are disabled while interrupt is running. Details [here](http://lwn.net/Articles/380931/). – Sam Protsenko Mar 29 '15 at 11:27
  • Yes, all interrupts are disabled while one interrupt is running, that's what the processor does, but in the interrupt handler you can re-enable some of them (not the current running one) – Roxana Istrate Mar 29 '15 at 11:36
2

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.

user3629249
  • 16,402
  • 1
  • 16
  • 17
  • 'That interrupt is normally the result of a hardware timer expiring/reloading and triggering the interrupt event.' - really? The interrupts from disk, NIC, USB etc. drivers are abnormal? – Martin James Mar 30 '15 at 14:56
  • Thank you very much for the details explained above! It was very clear for me. Still I have one more question. You said '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. '. Whose stack? Kernel's stack? The prior process' stack? – Roxana Istrate Mar 31 '15 at 19:45