Sub topic 4
Interrupt-Driven I/O
Prerequisites and the basic idea
This chapter assumes that you understand the CPU, registers, memory, buses, the instruction cycle, binary numbers, and stack-based subroutine calls and returns.
In programmed I/O, the CPU repeatedly checks a device's status register. In interrupt-driven I/O, the CPU starts the I/O operation and continues with other work. The device sends an interrupt request when it needs attention. An interrupt request is a signal asking the CPU to pause its current program and run a special function called an interrupt service routine (ISR).
Interrupt-driven I/O avoids wasting CPU cycles on repeated status checks. The CPU still spends time handling interrupts, but only when the device requires service.
Working model
Consider a keyboard receiving characters. The keyboard interface contains a data register and a status register. When a character arrives, the interface:
- Stores the character in its data register.
- Sets a ready bit in its status register.
- Sends an interrupt request to the CPU.
- Waits for the CPU to run the keyboard ISR.
- The ISR reads the character and stores it in a software buffer.
The CPU can execute another program between two keyboard events.
The CPU does not normally jump to an arbitrary address when an interrupt arrives. It obtains the ISR address from an interrupt vector or from a fixed entry point, depending on the processor design. The vector is an address associated with a particular interrupt source.
A simple ISR
The following C sketch shows the main idea. The volatile keyword tells the compiler that a value can change outside the normal instruction flow, such as through hardware or an ISR.
/* Hypothetical memory-mapped device register */
#define DATA_REG (*(volatile unsigned char *)0x4001)
volatile unsigned char buffer[64];
volatile unsigned int count = 0;
void device_isr(void) {
if (count < sizeof buffer) {
buffer[count] = DATA_REG;
count++;
}
}
int main(void) {
/* Platform-specific code enables this device and its interrupt. */
for (;;) {
/* Perform other work. Read buffer when data is available. */
}
}When the device invokes device_isr, the ISR reads DATA_REG. On many devices, reading the data register also clears the device's interrupt condition. The exact behavior is hardware-specific, so the device documentation must be checked.
An ISR should be short. It can copy data into a buffer, record an event, or acknowledge the device. Longer processing should usually happen in the main program or a scheduled task. A long ISR delays other interrupts and increases response time.
Interrupt acceptance sequence
A simplified interrupt sequence is:
- The device asserts its interrupt request.
- The CPU checks whether interrupts are enabled and whether this request is permitted.
- The CPU completes the current instruction.
- The CPU saves the program counter and required status information, often on the stack.
- The CPU identifies the ISR address.
- The CPU transfers control to the ISR.
- The ISR saves any additional registers it will modify.
- The ISR services and acknowledges the device.
- The ISR restores registers and executes a return-from-interrupt instruction.
- The CPU restores its previous state and resumes the interrupted program.
The CPU may temporarily disable some interrupts while saving state or while executing a critical part of an ISR. This prevents inconsistent data, but excessive masking increases interrupt latency.
Common errors
Forgetting to acknowledge the device
If the ISR reads the data but does not clear the device's request, the same interrupt can be raised repeatedly. Acknowledgment might require reading a register, writing a control bit, or sending a separate command.
Sharing data incorrectly
The main program and the ISR can access the same buffer or counter. Such shared data is subject to a race condition, where the result depends on the timing of the accesses. Declaring a variable volatile ensures fresh reads, but it does not make multi-step updates atomic. Protect compound operations when necessary.
Treating an interrupt as free
Interrupts remove polling work, but each interrupt has overhead. The CPU must save state, run the ISR, restore state, and possibly flush or refill processor pipelines. A device that generates one interrupt per byte can still consume a large amount of CPU time.
Polling compared with interrupts
| Technique | CPU behavior while waiting | Typical strength | Typical weakness |
|---|---|---|---|
| Polling | Repeatedly checks device status | Simple control flow | Wastes CPU time while idle |
| Interrupt-driven I/O | Runs other work until notified | Good for irregular events | ISR and context-switch overhead |
| DMA | Configures a block transfer | Efficient for large blocks | Requires hardware and coordination |
Interrupt-driven I/O is therefore a middle choice. It gives the CPU more freedom than polling, but it still involves the CPU for each interrupt event.
Exercise
A device produces one byte every 2 milliseconds. Should the CPU normally use polling or interrupt-driven I/O if it has useful work to perform between bytes? Why?
Show answer
Interrupt-driven I/O is usually preferable because the CPU can perform useful work during the 2-millisecond gaps and handle the device only when a byte arrives.