Trap和中断的区别是什么?

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


当前回答

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

其他回答

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

trap是用户进程中的异常。这是由除零或无效内存访问引起的。这也是调用内核例程(系统调用)的常用方法,因为内核例程的优先级高于用户代码。处理是同步的(因此用户代码被挂起,然后继续)。在某种意义上,它们是“活跃的”——大多数时候,代码期望陷阱发生并依赖于这一事实。

中断是由硬件(硬盘、显卡、I/O端口等设备)产生的。它们是异步的(即它们不会发生在用户代码中可预测的位置)或“被动的”,因为中断处理程序必须等待它们最终发生。

你也可以把一个陷阱看作是一种cpu内部中断,因为陷阱处理程序的处理程序看起来像一个中断处理程序(寄存器和堆栈指针被保存,有一个上下文切换,在某些情况下可以继续执行)。

A trap is a software interrupt.If you write a program in which you declare a variable having divide by zero value then it is treated as a trap.Whenever you run this program it will throw same error at the same time.System call is a special version of trap in which a program asks os for its required service. In case of interrupt(a general word for hardware interrupts)like an i/o error,the cpu is interrupted at random time and off course it is not the fault of our programmers.It is the hardware that brings them up.

中断是系统内硬件生成的流变化。一个中断 处理器被调用来处理中断的原因;控件然后返回到 中断的上下文和指令。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