3.1 Overview
Interrupts, traps, and exceptions are fundamental operating system mechanisms that transfer control from a running program to the operating system kernel.
These mechanisms allow the operating system to:
Respond to hardware events
Handle errors
Execute privileged operations
Manage system calls
Maintain controlled execution
Without these mechanisms:
The CPU would continuously execute user programs without reacting to external events
Hardware devices could not communicate with the CPU
Operating systems could not safely provide services to applications
These concepts form the foundation of:
Interrupt-driven execution
System call handling
Error detection
Hardware communication
Although interrupts, traps, and exceptions all transfer control to the operating system, they differ in:
Source
Timing
Purpose
Behavior
Understanding their differences is extremely important in:
Operating system interviews
Low-level systems programming
Kernel development
Computer architecture
3.2 Interrupts (Hardware-Driven)
Definition
An interrupt is an asynchronous signal generated by hardware that requests immediate CPU attention.
Interrupts allow hardware devices to notify the CPU whenever:
An event occurs
An operation completes
Input becomes available
Instead of continuously checking devices manually, the CPU can execute other tasks and respond only when interrupted.
This mechanism greatly improves:
CPU efficiency
Responsiveness
Device communication
Why Interrupts Are Needed
Suppose the CPU had to continuously check whether:
Keyboard input arrived
Disk operations completed
Network packets were received
This approach, called:
Polling
would waste CPU cycles.
Interrupts solve this problem by allowing hardware devices to:
Notify the CPU only when attention is required.
Asynchronous Nature of Interrupts
Interrupts are:
Asynchronous
meaning:
They occur independently of the currently executing program
The CPU cannot predict exactly when an interrupt will occur.
Example:
Keyboard input may happen at any moment.
Working of Interrupts
Step 1: Device Sends Interrupt Signal
A hardware device sends an interrupt request signal to the CPU.
Examples:
Keyboard key press
Disk I/O completion
Timer signal
Step 2: CPU Completes Current Instruction
The CPU usually finishes the currently executing instruction before responding.
This ensures:
Consistent execution state
Step 3: CPU Saves Context
The CPU saves the current process state, including:
Program Counter (PC)
CPU registers
Processor state information
This saved information is called:
Context
Saving context allows execution to resume later.
Step 4: CPU Looks Up Interrupt Vector Table (IVT)
The CPU uses the interrupt number to locate the corresponding handler inside the:
Interrupt Vector Table (IVT)
The IVT contains addresses of:
Interrupt Service Routines (ISR)
Step 5: Execute ISR
The CPU transfers control to the:
Interrupt Service Routine (ISR)
The ISR handles the interrupt event.
Examples:
Reading keyboard data
Processing completed disk operation
Step 6: Restore Context
After interrupt handling:
Saved CPU state is restored
Step 7: Resume Execution
The interrupted program continues execution from where it stopped.
Interrupt Service Routine (ISR)
An ISR is a special kernel function designed to:
Handle interrupts quickly
Perform required device operations
Restore normal execution
ISRs are typically:
Small
Fast
Highly optimized
because interrupts must be processed efficiently.
Examples of Interrupts
Keyboard Interrupt
Occurs when:
User presses a key
The keyboard controller sends an interrupt signal to the CPU.
Disk I/O Completion Interrupt
Occurs when:
Disk read/write operation finishes
The disk controller notifies the CPU.
Timer Interrupt
Generated periodically by hardware timers.
Used for:
Process scheduling
Time-sharing systems
CPU preemption
Importance of Interrupts
Interrupts enable:
Multitasking
Device communication
Efficient CPU utilization
Responsive systems
Modern operating systems rely heavily on interrupt-driven execution.
3.3 Traps (Software-Generated)
Definition
A trap is a synchronous and intentional interrupt generated by software during program execution.
Unlike hardware interrupts:
Traps are triggered directly by instructions executed by the running program.
Traps are commonly used to:
Request operating system services
Invoke system calls
Support debugging
Synchronous Nature of Traps
Traps are:
Synchronous
meaning:
They occur as a direct result of program execution.
The CPU knows exactly:
Which instruction caused the trap.
Purpose of Traps
System Calls
Applications cannot directly access hardware or privileged resources.
Instead:
They request services using traps.
Examples:
File access
Memory allocation
Process creation
Debugging Support
Traps are used for:
Breakpoints
Debugging tools
Program inspection
Controlled Transition to Kernel Mode
Traps safely transfer execution from:
User mode
toKernel mode
Working of a Trap
Step 1: Program Executes Trap Instruction
The application executes a special instruction.
Examples:
syscall
int instruction
Step 2: CPU Switches to Kernel Mode
The CPU changes privilege level.
Step 3: Context Saved
Current execution state is saved.
Step 4: Kernel Trap Handler Executes
The operating system handles the requested service.
Step 5: Return to User Mode
Execution resumes after the trap completes.
Example of Trap
System Call
Suppose a program executes:
write(1, "Hello", 5);
The write operation requires:
Hardware access
Kernel privileges
The program triggers a trap to request operating system assistance.
Key Property of Traps
Most important property:
Traps are intentional and generated directly by software execution.
3.4 Exceptions (Error Conditions)
Definition
An exception occurs when the CPU detects an abnormal condition during program execution.
Exceptions usually indicate:
Errors
Invalid operations
Illegal execution conditions
Unlike interrupts:
Exceptions originate from the currently executing instruction.
Synchronous Nature of Exceptions
Exceptions are:
Synchronous
because they occur directly due to instruction execution.
Why Exceptions Exist
Exceptions allow the operating system to:
Detect errors
Prevent invalid execution
Protect system stability
Without exceptions:
Faulty programs could corrupt the system.
Examples of Exceptions
Division by Zero
Occurs when:
A program attempts integer division by zero
Example:
int x = 10 / 0;
Page Fault
Occurs when:
A process accesses memory not currently loaded into RAM
The operating system may:
Load the page from disk
Update page tables
Invalid Instruction
Occurs when:
CPU encounters unsupported or illegal instructions
Segmentation Fault
Occurs when:
Program accesses unauthorized memory
Exception Handling Process
Step 1: CPU Detects Error
During instruction execution, the CPU identifies abnormal behavior.
Step 2: Context Saved
Current execution state is preserved.
Step 3: Exception Handler Invoked
The operating system executes the corresponding exception handler.
Step 4: Recovery or Termination
Depending on exception type:
Execution may continue
Process may terminate
OS may recover automatically
Recoverable vs Non-Recoverable Exceptions
Recoverable Exceptions
Example:
Page faults
The OS can often recover and continue execution.
Non-Recoverable Exceptions
Examples:
Illegal instructions
Severe protection violations
Usually terminate the process.
3.5 Classification
| Type | Nature | Source | Example |
|---|---|---|---|
| Interrupt | Asynchronous | Hardware | Keyboard input |
| Trap | Synchronous | Software | System call |
| Exception | Synchronous | CPU | Divide by zero |
Understanding the Classification
Interrupt
Generated externally
Independent of current program
Trap
Intentionally generated
Used to request OS services
Exception
Generated due to execution errors
Indicates abnormal conditions
3.6 Critical Insight
Understanding the core difference is extremely important.
Interrupts
Interrupts handle external hardware events.
Examples:
Keyboard input
Disk completion
Timer signals
They are:
Asynchronous
Hardware-driven
Traps
Traps intentionally request operating system services.
Examples:
System calls
Debugging breakpoints
They are:
Synchronous
Software-generated
Exceptions
Exceptions handle abnormal execution conditions and errors.
Examples:
Divide-by-zero
Page faults
Invalid instructions
They are:
Synchronous
CPU-detected
Relationship with Kernel Mode
Interrupts, traps, and exceptions all cause:
Transfer of control to kernel mode.
The operating system gains control to:
Handle events
Enforce protection
Manage resources safely
After handling:
Execution typically returns to user mode.
Real-World Analogy
Imagine a classroom.
Interrupt
A fire alarm suddenly rings.
External event
Teacher immediately responds
Trap
A student intentionally raises a hand to ask a question.
Planned request
Exception
A student makes a serious mistake during an experiment.
Error condition requiring intervention
Similarly:
Interrupts, traps, and exceptions transfer control to the operating system for handling.