Well, in English Language we define the word Interrupt
as to stop someone from speaking or doing something, or to cause an activity or event to stop briefly.
Imagine you are playing a game on your computer and suddenly, your mom calls you for the dinner. You immediately pause the game and go to dinner because you mom's call is like an interrupt. Once you complete the dinner, then come back and resumes playing the game.
So, we can say that:
- An interrupt is like when your mom calls you for dinner while you are playing.
- It's a signal that something important needs attention right now.
- On receiving the interrupt, you stops and pause whatever you were doing and goes to handle the interrupt.
- After the interrupt is handled, you goes back to what you were doing before.
What is an Interrupt?
An interrupt is a signal emitted by hardware or software to indicate the need for immediate attention from the processor. Interrupts temporarily suspend the currently executing process and transfer control to a special piece of code called an interrupt handler or interrupt service routine (ISR).
Types of Interrupts
1 Hardware Interrupts:
- Generated by hardware devices to request attention from the CPU.
- Examples include:
- Timer interrupts
- I/O interrupts (keyboard input, mouse input, disk I/O)
- Hardware error interrupts
Hardware Interrupts are further divided into two types:
- Maskable Interrupts:
- These interrupts can be delayed or temporarily ignored by the CPU if it is currently handling a more urgent task.
- They are typically used for non-critical tasks
- Non-Maskable Interrupts (NMI):
- These interrupts cannot be ignored by the CPU and must be serviced immediately.
- They are used for critical system events such as power failure or hardware malfunction.
2 Software Interrupts:
In computing, a software interrupt, also known as a trap or exception, is generated by software or the system itself rather than by hardware. These interrupts serve as signals for the operating system or system services to perform specific functions or respond to error conditions.
Software interrupts are typically triggered by specific instructions or exceptional conditions during operation. For instance, system calls often result in software interrupts. Similarly, exceptions such as division by zero can also lead to a software interrupt.
One common example of a software interrupt is the fork() system call, which generates an interrupt to create a new process. On the other hand, an exception like division by zero results in a software interrupt to handle the error condition.
A specific instruction, known as an "interrupt instruction," is utilized to generate software interrupts. When this instruction is encountered, the processor suspends its current operations and switches to a designated interrupt handler code. The interrupt handler routine then performs the necessary tasks, such as executing a system call or handling errors, before returning control to the interrupted application.
- Generated by software to invoke system calls or communicate with the operating system.
- Examples include:
- System calls (e.g.,
int 0x80
in x86 assembly) - Software exceptions (e.g., divide by zero, page faults)
- System calls (e.g.,
How Interrupts Work:
1 Interrupt Request (IRQ):
- The hardware device sends an interrupt signal (IRQ) to the CPU.
2 Interrupt Controller:
- The interrupt controller prioritizes interrupts and sends them to the CPU.
3 Interrupt Service Routine (ISR):
- The CPU saves the current state of execution and transfers control to the ISR.
4 ISR Execution:
- The ISR handles the interrupt, performs the necessary actions, and returns control to the interrupted program.
5 Context Switch:
- After servicing the interrupt, the CPU resumes the interrupted program from where it left off.
Example:
Say you're doing your homework. Your phone rings and you look at the screen.. you are interrupted
from doing your homework. Depending on who it is.. you react differently. Say your parents are calling you, you pick it up right away and start talking to them. If you know the call is coming from an annoying telemarketing company, you'll definitely ignore it. So in this situation you're either reacting to these interrupts or not. If you choose NOT to react, then you've masked
that interrupt.
There can be other kinds of interrupts as well.. your mom calling you for supper(the last meal of the day
), your friend calling you after he gets into trouble etc. Consider another situation. Say your mom called you to have supper. And you're about to sit down at the table.. your friend calls you and tells you that he's in trouble. You'd obviously ignore your supper and head out.. kick other people's butts and come back have dinner (or your mom's gonna kill you!) and finally sit down to do your homework. Notice that in this situation, there were many interrupts.. with varying priority
. If interrupted in the middle you go do the higher priority work first and then come back to do whatever you're doing now.
You might have also noticed that how you react for each interruption is different right? Sounds lame.. but true nonetheless as computers are not intelligent, you've got to tell them what to do every step of the way. So your reactions for every kind of an interrupt is called an interrupt service routine (ISR)
.