Difference Between Trap and Interrupt

The main difference between trap and interrupt is that trap is triggered by a user program to invoke OS functionality while interrupt is triggered by a hardware device to allow the processor to execute the corresponding interrupt handler routine.

An operating system is event-driven. An event can occur suddenly while executing a program. It will trigger the operating system to execute. This will change the user mode into kernel mode. After the execution of the OS, the control is passed back to the original program.  Traps and interrupts are two types of events. A trap is raised by a user program whereas an interrupt is raised by a hardware device such as keyboard, timer, etc. A trap passes the control to the trap handler and the interrupt passes the control to an interrupt handler. After executing the handler, the control switches back to the original program.

Key Areas Covered

1. What is Trap
     – Definition, Functionality
2. What is Interrupt
     – Definition, Functionality
3. Relationship Between Trap and Interrupt
     – Outline of Association
4. Difference Between Trap and Interrupt
     – Comparison of Key Differences

Key Terms

Interrupt, Operating system, Trap

Difference Between Trap and Interrup - Comparison Summary

What is Trap

Traps are raised by the user program to invoke a functionality of the operating system. Assume that the user program requires printing something to the screen. It would invoke a trap and the operating system will perform writing that data to the screen. Traps are mainly used to implement system calls.

Difference Between Trap and Interrupt

Figure 1: Types of Events

An example is as follows. Assume that there is a statement as printf (“%s\n”, str); It is will invoke the write function to print the output to the standard output which is the monitor. This will invoke a trap and it will pass the control to the trap handler. Then, the user mode changes to kernel mode and the OS executes the write call. After completing the task, the control is transferred back to the user mode from the kernel mode.

What is Interrupt

An interrupt is raised by a hardware device. USB device, NIC card, keyboard can cause interrupts. Interrupts are asynchronous. Therefore, they can occur at any time.

A processor has a dedicated pin called interrupt pin. It is also called an INT pin. Devices such as keyboards are connected to the processor via the interrupt pin. When a key is pressed, it will generate an interrupt. The processor will switch from currently running process into an Interrupt Handler Routine. In this scenario, the keyboard interrupt handler routine is invoked. After completing the interrupt handler routine, the processor switches back to the original program that has being running. Basically, when an interrupt occurs, the processor switches the context and executes the interrupt handler. After completion, it switches back to the previous state.

A processor has a single interrupt pin but there are multiple hardware devices. The interrupt controller helps to share the single interrupt pin between multiple pins. Processor will communicate with the interrupt controller to determine which device had actually generated the interrupt. Depending on that, the processor will execute the corresponding interrupt handler routine. It can be timer, USB, or Keyboard interrupt handler routine. 

In addition to traps and interrupt, there is another type of event called exceptions. They are generated by the processor automatically. Exceptions further divides into faults and aborts. A fault is a recoverable error while an abort is an error that is difficult to handle.

Relationship Between Trap and Interrupt

  • Trap and Interrupt are types of events.

Difference Between Trap and Interrupt

Definition

The trap is a signal raised from a user program that indicates the operating system to perform on some functionality immediately while interrupt is a signal to the processor emitted by hardware indicating an event that needs immediate attention.

Method of Generating

A trap is generated by an instruction in the user program while an interrupt is generated by hardware devices.

Main Functionality

Further, a trap invokes OS functionality. It transfers the control to the trap handler. An interrupt triggers the processor to execute the corresponding interrupt handler routine.

Occurrence

Also, a trap is synchronous and can arrive after the execution of any instruction while an interrupt is asynchronous and can occur at the execution of any instruction.

Synonyms

A trap is also called a software interrupt while an interrupt is also called a hardware interrupt.

Conclusion

Trap and Interrupt are two types of events. The difference between trap and interrupt is that the trap is triggered by a user program to invoke OS functionality while the interrupt is triggered by a hardware device to allow the processor to execute the corresponding interrupt handler routine.

Reference:

1. Operating System #14 What Is an Interrupt? Types of Interrupts, Xoviabcs, 31 Aug. 2017, Available here.
2. Operating System #16 Software Interrupts | System Calls in xv6, Xoviabcs, 1 Sept. 2017, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply