Signal external events, generated by the hardware.
- I/O hardware interrupts
- Timers
Interrupts on modern CPUs are precise, as CPU transfers control only on instruction boundaries.
Handling
Interrupt handler is in the kernel, working by:
- Disable interrupts at lower priorities
- Save state
- Transfer control to the interrupt service routine in the kernel
- When done, re-enable interrupts
- Resume the user-level program at next instruction
Examples
Timer
Prevents programs from hogging the CPU, allowing the OS to always regain control from wrong or malicious programs. Also used for time-based functions like sleep.
A fallback mechanism by which the OS reclaims control over the machine. Set to generate an interrupt after a period of time, handled by the kernel which decides which program to run next. Setting the timer is a privileged instruction, and forms the basis for the OSโs ๐๏ธ Scheduler.
I/O
Asynchronous I/O
- OS initiates I/O
- Device operates independently of the rest of the machine
- Device sends an interrupt to the CPU when done
- CPU context switches to the interrupt handler
- Eventually resumes to the original process