Trap和中断的区别是什么?

如果术语对于不同的系统是不同的,那么它们在x86上意味着什么?


当前回答

一般来说,像异常、错误、中止、陷阱和中断这样的术语都是指同样的事情,都被称为“中断”。

来看看Trap和Interrupt的区别:

陷阱:程序员发起并期望将控制转移到一个特殊的处理程序例程。(例如:80x86 INT指令是一个很好的例子)

中断(硬件):是基于CPU外部硬件事件的程序控制中断(例如:按键盘上的一个键或计时器上的超时 芯片)

其他回答

中断是系统内硬件生成的流变化。一个中断 处理器被调用来处理中断的原因;控件然后返回到 中断的上下文和指令。trap是软件生成的中断。中断可以 用于标志一个I/O的完成,以避免设备轮询的需要。陷阱可以是 用于调用操作系统例程或捕捉算术错误。

A Trap can be identified as a transfer of control, which is initiated by the programmer. The term Trap is used interchangeably with the term Exception (which is an automatically occurring software interrupt). But some may argue that a trap is simply a special subroutine call. So they fall in to the category of software-invoked interrupts. For example, in 80×86 machines, a programmer can use the int instruction to initiate a trap. Because a trap is always unconditional the control will always be transferred to the subroutine associated with the trap. The exact instruction, which invokes the routine for handling the trap is easily identified because an explicit instruction is used to specify a trap. Trap Vs Interrupt

Interrupts are hardware interrupts, while traps are software-invoked interrupts. Occurrences of hardware interrupts usually disable other hardware interrupts, but this is not true for traps. If you need to disallow hardware interrupts until a trap is served, you need to explicitly clear the interrupt flag. And usually the interrupt flag on the computer affects (hardware) interrupts as opposed to traps. This means that clearing this flag will not prevent traps. Unlike traps, interrupts should preserve the previous state of the CPU.

我认为陷阱是由当前指令的执行引起的,因此它们被称为同步事件。中断是由处理器中运行的与外部事件相关的独立指令引起的,因此被称为异步中断。

陷阱是一种特殊的中断,通常被称为软件中断。中断是一个更通用的术语,它包括硬件中断(来自硬件设备的中断)和软件中断(来自软件的中断,如陷阱)。