Difference between trap and interrupt

The main purpose of the interrupt is to bring attention to the CPU to some high priority events that have to be executed immediately. The trap is the same as the interrupt, its purpose is to bring attention to the CPU to execute system calls that are required in the execution of the user code. 

You can understand the difference as Interrupt being the topic of the books and Trap is a small chapter in that book. An interrupt is a vast topic which has interrupt handlers, masked interrupts, unmasked interrupts, traps, exceptions, faults, etc. The Trap is a part of Interrupt but specifically deals with system calls for user code execution. 

In my research, for the article, I have found in many websites which make the difference between the trap and interrupt look like the difference between the hardware and the software interrupt and which is very misleading. The only differentiation which I can say is TRAPS are very specific INTERRUPTS.  

 

What is an interrupt? 

Interrupts are the signals that are emitted by the software or the hardware when any process or an event needs immediate attention. These signals are generated from the hardware and the software and therefore they are called the hardware and the software interrupts. 

These signals, when generated by the hardware or the software have to be attended by the operating system. The operating system has some processes that are operating and these processes are present in the queue, so when the operating system receives the interrupt signals all the processes in the queue that were running are made to halt and CPU is made to attend to the signal of the interrupt. 

The reason why these interruptions are so useful to the operating system is that it is a way the hardware and software can communicate to the operating system. 

The operating system’s nature is interrupt driven. If you move your mouse on the screen an interrupt is getting generated, if you press any key on a keyboard an interrupt is getting generated, if you print a document an interrupt is getting generated, if you power off the monitor screen an interrupt is getting generated, there are many such occasion such that operating system is waiting for some interrupts to take place and for attending the interrupts. You can also conclude here that interrupts are a higher priority process which requires the interruption of the current working process. 

 

Interrupt Mechanism

Let’s see how the interrupt actually happens, in a program, there are various instructions which are getting executed, and the time it takes for one program to execute one instruction of the program is called the instruction cycle. 

While the CPU is executing the instruction of the program, the interrupts may be generated by the I/O devices or user program, these interrupts will be provided to the CPU as an input while it is executing the instruction of the program. 

The method by which the CPU handles the interrupt is that the CPU checks after every instruction cycle ( time taken for completion of one instruction in the program) whether or not an interrupt is pending to take action. If the CPU does not find any interrupts to be pending then it will continue the execution of the next instruction of the program but if the CPU finds an interrupt pending then it will stop the execution of the instruction of the program and service the pending interrupt by calling the interrupt handler program. 

When the CPU finds the interrupt, the program will be put to the suspended mode to stop the program temporarily and the state of the program and the current instruction number will be saved by the CPU to attend it later on. 

After this CPU will attend the interrupt and it will what interrupt is produced and what is the interrupt number and with this interrupt number it will match to a table (interrupt vector table) to know exactly which interrupt handler program will be needed to solve the interrupt and will give the necessary service to the interrupt generated. 

After the CPU has attended to the interrupt it will go back to the previous program that it was executing and change the state of the program from a suspended state to the ready state and also load the exact instruction address where it has executed the program and then it will continue the execution of the remaining program. This is the whole operation of how the interrupt is handled in the operating system. 

There is another way similar to the interrupts that the operating system can check the processor whether it is ideal or not and it’s with the help of the kernel. At a regular interval of time, some signals can be sent to the kernel whether the CPU is ideal or not. And if it’s ideal then some processes can be provided to the processor and if not it can be allowed to complete its function. 

 

What are traps in OS?

A trap is a software-generated interrupt which can be caused by some reason such as – error in the instruction such as division by zero, trap can be generated due to invalid memory access, also the trap can be generated when the user program initiates a specific service request from the operating system.

The traps are more likely to be caused by the execution of the current instructions and therefore the traps are also known as synchronous events. 

The system calls are another example of a trap where the operating system is asked by the program to request a particular service and the operating system then generates an interrupt to access the services for the program.

The traps are much more active as an interrupt because the code will heavily rely on the fact that the traps can be used to communicate to the operating system to talk to an operating system therefore they will repeat the function of the trap to either use any system service to access or any other functionality of the operating system.

Trap Mechanism

The user program that is up for execution on CPU generally uses library calls to issue system calls. The function of the library routine check is to check the arguments that are provided by the application and then build a data structure to convey the arguments from the application to the kernel of the operating system and then execute special instructions which are called the trap or the software interrupts. 

These special instructions or traps contain operands that help to identify exactly which kernel service the application arguments are demanding. So, when the process is made to execute the traps then the interrupt saves the state of the user code and then switches to the supervisor mode and then dispatches the right kernel routine that can implement the requested service. 

Difference between trap and interrupt [ Trap Vs Interrupt] 

INTERRUPTS

TRAPS

Include both the software and hardware interrupts.  Only deals with some specific software interrupts. 
They can be asynchronous in hardware interrupts and synchronous in the software interrupt.  They are asynchronous as they belong in the category of software interrupt. 
Both operating system generated and user generated interrupt. Only user generated interrupt. 
Not all interrupts are traps. All traps are interrupt

If you did not find what you were looking for do check out these forum on the post Difference between Trap and Interrupt – Quora and stack overflow. You will definitely find something in these forum check the links of the forum for difference between Trap and Interrupt.

16 thoughts on “Difference between trap and interrupt”

Leave a Comment