🌟 Understanding the Basics
Let’s start simple.
When a computer is running, its CPU is like a very focused worker — it keeps executing one instruction after another. But sometimes, something unexpected happens — maybe a key is pressed, a printer finishes printing, or an error occurs.
How does the CPU handle these sudden events without losing track of what it was doing?
That’s where interrupts and exceptions come in.
Both are ways for the processor to pause what it’s doing, handle the event, and then go back to work as if nothing happened.
🧠 What Are Interrupts?
An interrupt is a signal sent to the CPU by a hardware device or software that needs immediate attention.
It’s like a student raising their hand in class to ask a question — the teacher (CPU) stops for a moment, listens, answers, and then continues teaching.
✨ Example:
- When you press a key on the keyboard, an interrupt tells the CPU,
“Hey! The user just pressed a key. Please read it.” - The CPU pauses its current work, reads the key input, and then resumes what it was doing before.
🧩 Types of Interrupts
- Hardware Interrupts:
These come from external devices like keyboards, printers, or disk drives.
Example: A printer finishes printing and notifies the CPU. - Software Interrupts:
These are generated by programs themselves — often to request system-level services.
Example: A program asks the operating system to save a file. - Maskable and Non-maskable Interrupts:
- Maskable interrupts can be ignored (or “masked”) temporarily by the CPU.
- Non-maskable interrupts cannot be ignored — they are usually for critical events like hardware failures.
⚡ What Are Exceptions?
An exception happens inside the CPU when something goes wrong during instruction execution.
You can think of it as an internal error that the CPU detects by itself.
✨ Example:
- Dividing a number by zero
- Accessing an invalid memory location
- Executing an illegal instruction
These are not signals from external devices — they’re the CPU’s way of saying,
“Oops, something unexpected just happened while I was running this program.”
🧠 Interrupt vs Exception (Simple Comparison)
| Feature | Interrupt | Exception |
|---|---|---|
| Source | External device or software signal | Internal CPU event |
| Example | Keyboard input, disk ready | Divide by zero, invalid memory |
| Triggered by | Hardware or software | CPU while executing instructions |
| Purpose | Handle I/O or system events | Handle errors or unusual conditions |
🖼️ Diagram: How Interrupts and Exceptions Work
+----------------------+
| CPU Executes |
| Instructions One |
| by One |
+----------+-----------+
|
v
+----------------------+
| Interrupt/Exception? |
+----------+-----------+
|
+---------+----------+
| |
v v
+---------------+ +---------------+
| Save Current | | Save Current |
| CPU State | | CPU State |
+---------------+ +---------------+
| |
v v
+---------------+ +---------------+
| Handle Event | | Handle Error |
| (Interrupt) | | (Exception) |
+---------------+ +---------------+
| |
+---------+----------+
|
v
+----------------------+
| Resume Normal Task |
+----------------------+
💬 Real-life Analogy
Imagine you’re cooking dinner (the CPU’s main task).
- If someone rings the doorbell (interrupt), you stop cooking, open the door, handle it quickly, and return to cooking.
- If you accidentally spill hot oil (exception), you have to stop, clean up, and fix the problem before continuing.
Both interrupt your flow — but one comes from outside, and the other from within.
🧾 Why Are Interrupts and Exceptions Important?
- They make the system responsive — the CPU doesn’t have to keep checking every device manually.
- They improve efficiency — the CPU works on tasks only when needed.
- They ensure error handling — problems inside the processor can be fixed without crashing the system completely.