2. Difference between interrupt and trap
An interrupt is a hardware-generated event, usually caused by an I/O device requesting service. The interrupt signal causes a change of °ow within the system: an interrupt service routine (ISR) saves the CPU state, services the interrupt, then restores control to the interrupted process.
A trap, on the other and, is a software-generated interrupt. Traps are caused by 1) system calls and 2) exceptional software conditions (e.g. divide by 0). Other than the source and function, processing of traps and interrupts is the same.
Typical uses of interrupts include the following: system timers, disks I/O, power-off signals, and traps. Other interrupts exist to transfer data bytes using UARTs or Ethernet; sense key-presses; control motors; or anything else the equipment must do. A classic system timer interrupt interrupts periodically from a counter or the power-line. The interrupt handler counts the interrupts to keep time. The timer interrupt may also be used by the OS's task scheduler to reschedule the priorities of running processes. Counters are popular, but some older computers used the power line frequency instead, because power companies in most Western countries control the power-line frequency with a very accurate atomic clock. A disk interrupt signals the completion of a data transfer from or to the disk peripheral. A process waiting to read or write a file starts up again. A power-off interrupt predicts or requests a loss of power. It allows the computer equipment to perform an orderly shutdown. Interrupts are also used in typeahead features for buffering events like keystrokes.
The main purpose of a trap is to provide a fixed subroutine that various programs can
call without having to actually know the run-time address. MS-DOS is the perfect example.
The int 21h instruction is an example of a trap invocation. Your programs do not have
to know the actual memory address of DOS’ entry point to call DOS. Instead, DOS patches
the interrupt 21h vector when it loads into memory. When you execute int 21h , the 80x86 automatically transfers control to DOS’ entry point, whereever in memory that happens to