C语言 中断服务程序中会发生什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3392831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What happens in an interrupt service routine?
提问by mousey
Can someone please explain to me what happens inside an interrupt service routine (although it depends upon specific routine, a general explanation is enough)? This always used be a black box for me.
有人可以向我解释一下中断服务程序内部发生了什么(虽然它取决于特定的程序,但一般的解释就足够了)?这对我来说一直是一个黑匣子。
回答by Paul Rubel
There is a good wikipedia page on interrupt handlers.
关于中断处理程序有一个很好的维基百科页面。
"An interrupt handler, also known as an interrupt service routine (ISR), is a callback subroutine in an operating system or device driver whose execution is triggered by the reception of an interrupt. Interrupt handlers have a multitude of functions, which vary based on the reason the interrupt was generated and the speed at which the Interrupt Handler completes its task."
“中断处理程序,也称为中断服务例程 (ISR),是操作系统或设备驱动程序中的回调子例程,其执行由接收中断触发。中断处理程序具有多种功能,这些功能因产生中断的原因以及中断处理程序完成其任务的速度。”
Basically when a piece of hardware (a hardware interrupt) or some OS task (software interrupt) needs to run it triggers an interrupt. If these interrupts aren't masked (ignored) the OS will stop what it's doing and call some special code to handle this new event.
基本上,当一个硬件(硬件中断)或某个操作系统任务(软件中断)需要运行时,它会触发一个中断。如果这些中断没有被屏蔽(忽略),操作系统将停止它正在做的事情并调用一些特殊的代码来处理这个新事件。
One good example is reading from a hard drive. The drive is slow and you don't want your OS to wait for the data to come back; you want the OS to go and do other things. So you set up the system so that when the disk has the data requested, it raises an interrupt. In the interrupt service routine for the disk the CPU will take the data that is now ready and will return it to the requester.
一个很好的例子是从硬盘驱动器读取。驱动器很慢,你不希望你的操作系统等待数据回来;你想让操作系统去做其他事情。因此,您设置系统,以便当磁盘收到请求的数据时,它会引发中断。在磁盘的中断服务例程中,CPU 将获取现在准备好的数据并将其返回给请求者。
ISRs often need to happen quickly as the hardware can have a limited buffer, which will be overwritten by new data if it's now pulled off quickly enough. It's also important to have your ISR complete quickly as while the CPU is servicing one ISR other interrupts will be masked, which means if the CPU can't get to them quickly enough data can be lost.
ISR 通常需要快速发生,因为硬件可能有一个有限的缓冲区,如果现在足够快地拉下新数据,它将被新数据覆盖。让 ISR 快速完成也很重要,因为当 CPU 为一个 ISR 提供服务时,其他中断将被屏蔽,这意味着如果 CPU 不能足够快地访问它们,数据可能会丢失。
回答by Sagar
While the 8086 is executing a program an interrupt breaks the normal sequence of execution of instruction, divert its execution to some other program called interrupt service Routine (ISR). after executing, control return the back again to the main program.
当 8086 正在执行程序时,中断会中断指令的正常执行顺序,将其执行转移到称为中断服务程序 (ISR) 的其他程序中。执行完毕后,控制权又返回到主程序。
回答by Sagar
An interrupt is used to cause a temporary halt in the execution of program. Microprocessor responds to the interrupt service routine, which is short program or subroutine that instruct the microprocessor on how to handle the interrupt.
中断用于使程序的执行暂时停止。微处理器响应中断服务程序,它是指示微处理器如何处理中断的短程序或子程序。

