What is an Interrupt?
Learn what an interrupt is — hardware vs software, ISR handling, and preemptive scheduling — with operating systems interview questions.
Expected Interview Answer
An interrupt is a signal to the CPU, generated by hardware or software, that forces the processor to pause its current instruction stream and immediately transfer control to a special handler routine.
Hardware interrupts come from devices such as a keyboard, disk controller, or timer signaling that they need attention, while software interrupts (traps) are deliberately triggered by an instruction, most commonly a system call requesting a kernel service. When an interrupt fires, the CPU saves the current program counter and register state, looks up the appropriate handler in the interrupt vector table, and executes that interrupt service routine (ISR); once the ISR finishes, the saved state is restored and the interrupted program resumes as if nothing happened. Interrupts are what let a CPU respond to asynchronous events like I/O completion without wasting cycles on busy-waiting, and they are the mechanism underlying preemptive multitasking via the timer interrupt.
- Lets the CPU react to I/O events without busy-waiting
- Enables preemptive multitasking via periodic timer interrupts
- Provides the mechanism for system calls to enter the kernel
- Allows fast, prioritized handling of urgent hardware events
AI Mentor Explanation
An interrupt is like a physio running onto the field when a player signals an injury — play pauses instantly regardless of what over it is. The umpire notes exactly where play stopped (saving the program counter and registers) so the match can resume from that precise point. The physio treats the player using a known protocol (the interrupt service routine), and once done, play resumes exactly where it left off, just like a CPU returning to the interrupted program.
Step-by-Step Explanation
Step 1
Signal fires
A hardware device or software trap raises an interrupt line/instruction to get the CPU’s attention.
Step 2
Context saved
The CPU saves the current program counter and register state onto the stack.
Step 3
Handler dispatched
The interrupt vector table is consulted and the matching interrupt service routine (ISR) runs.
Step 4
Context restored
After the ISR completes, the saved state is restored and the interrupted program resumes seamlessly.
What Interviewer Expects
- A clear definition distinguishing hardware and software interrupts
- Understanding of context save/restore around the ISR
- Mention of the interrupt vector table for dispatch
- Connection to preemptive scheduling via the timer interrupt
Common Mistakes
- Confusing interrupts with polling or busy-waiting
- Forgetting that system calls are implemented as software interrupts/traps
- Not mentioning that CPU state must be saved and restored
- Overlooking the role of the timer interrupt in preemptive multitasking
Best Answer (HR Friendly)
“An interrupt is a signal that makes the processor immediately pause whatever it is doing to handle something urgent, like a keyboard press or a timer going off. It carefully remembers exactly where it stopped, deals with the urgent event using a dedicated routine, and then picks up right where it left off, which is also how the operating system fairly shares CPU time between programs.”
Code Example
/* Simplified view of what the CPU does on an interrupt */
void cpu_interrupt(int irq_number) {
save_context(current_pc, registers); /* remember exact spot */
void (*handler)(void) = interrupt_vector_table[irq_number];
handler(); /* run the ISR */
restore_context(current_pc, registers); /* resume exactly there */
}
/* Example ISR for a keyboard interrupt */
void keyboard_isr(void) {
int scancode = read_from_keyboard_port();
buffer_keypress(scancode);
send_end_of_interrupt();
}Follow-up Questions
- What is the difference between an interrupt and a trap?
- How does the timer interrupt enable preemptive scheduling?
- What is an interrupt vector table?
- What is the difference between maskable and non-maskable interrupts?
MCQ Practice
1. What does the CPU do first when an interrupt fires?
The CPU saves its current context so it can resume the interrupted program exactly where it left off.
2. A system call is typically implemented using which mechanism?
System calls trigger a software interrupt (trap) that transfers control into the kernel.
3. Which interrupt is central to enabling preemptive multitasking?
A periodic timer interrupt lets the OS scheduler regain control and switch between processes.
Flash Cards
What is an interrupt? — A signal that makes the CPU pause its current work and run a handler routine immediately.
Hardware vs software interrupt? — Hardware: from devices (keyboard, disk, timer). Software: from an instruction, e.g. a system call.
What is saved before the ISR runs? — The program counter and register state, so execution can resume exactly afterward.
Which interrupt enables preemption? — The periodic timer interrupt, which returns control to the OS scheduler.