中断服务程序会发生什么?

时间:2010-08-03 00:35:39

标签: c unix

有人可以向我解释一下中断服务程序中发生了什么(虽然这取决于具体的例行程序,一般的解释就足够了)?对我来说这总是一个黑盒子。

4 个答案:

答案 0 :(得分:22)

interrupt handlers上有一个很好的维基百科页面。

  

“中断处理程序,也称为中断服务程序(ISR),是操作系统或设备驱动程序中的回调子程序,其执行由接收中断触发。中断处理程序具有多种功能,根据产生中断的原因以及中断处理程序完成任务的速度而变化。“

基本上,当需要运行硬件(硬件中断)或某些OS任务(软件中断)时,它会触发中断。如果没有屏蔽(忽略)这些中断,操作系统将停止它正在做的事情并调用一些特殊代码来处理这个新事件。

一个很好的例子是从硬盘读取。驱动器速度很慢,您不希望操作系统等待数据返回;你希望操作系统去做其他事情。因此,您需要设置系统,以便在磁盘具有请求的数据时,它会引发中断。在磁盘的中断服务程序中,CPU将获取现在准备好的数据并将其返回给请求者。

ISR通常需要快速发生,因为硬件可能具有有限的缓冲区,如果现在足够快地将其拉出,将被新数据覆盖。 让ISR快速完成也很重要,因为当CPU为一个ISR服务时,其他中断将被屏蔽,这意味着如果CPU无法快速到达它们,那么数据可能会丢失。

答案 1 :(得分:2)

Minimal 16-bit example

The best way to understand is to make some minimal examples yourself.

First learn how to create a minimal bootloader OS and run it on QEMU and real hardware as I've explained here: https://stackoverflow.com/a/32483545/895245

Now you can run in 16-bit real mode:

    movw $handler0, 0x00
    mov %cs, 0x02
    movw $handler1, 0x04
    mov %cs, 0x06
    int $0
    int $1
    hlt
handler0:
    /* Do 0. */
    iret
handler1:
    /* Do 1. */
    iret

This would do in order:

  • Do 0.
  • Do 1.
  • hlt: stop executing

Note how the processor looks for the first handler at address 0, and the second one at 4: that is a table of handlers called the IVT, and each entry has 4 bytes.

Minimal example that does some IO to make handlers visible.

Protected mode

Modern operating systems run in the so called protected mode.

The handling has more options in this mode, so it is more complex, but the spirit is the same.

Minimal example

See also

Related question: What does "int 0x80" mean in assembly code?

答案 2 :(得分:0)

中断用于在程序执行中导致暂时停止。微处理器响应中断服务程序,该服务程序是简短的程序或子程序,可指示微处理器如何处理中断。

答案 3 :(得分:0)

8086在执行程序时,中断中断了正常的指令执行顺序,将其执行转移到称为中断服务程序(ISR)的其他程序中。执行后,控制再次返回到主程序。