Polling vs Interrupts vs DMA
When a computer system communicates with external devices such as a printer, disk, or keyboard, it must decide how to transfer data and manage the interaction. There are three fundamental techniques used by operating systems to coordinate this communication: Polling, Interrupts, and Direct Memory Access (DMA). Each method differs in how the CPU is involved and how efficiently data moves between the I/O devices and memory.
Â
Understanding these techniques is critical to evaluating system performance, responsiveness, and overall efficiency in handling input/output operations.
Polling
Polling is a technique where the CPU continuously checks the status of an I/O device at regular intervals to determine if it’s ready for communication or has completed a task.
How it works:
Â
- The CPU issues a command to the device.
- It then repeatedly checks (polls) the device’s status register.
- Once the device is ready, the CPU proceeds with data transfer.
Â
Pros:
Â
- Simple to implement.
- Gives CPU complete control over the timing of operations.
Â
Cons:
- CPU time is wasted during idle waiting.
- Not suitable for high-speed or high-frequency I/O tasks.
Â
Example Use Case: Suitable for devices with infrequent communication needs, such as reading from a keyboard in low-power embedded systems.
Interrupts
Interrupts provide a more efficient alternative to polling. Instead of constantly checking the device, the CPU can continue executing other tasks. When the I/O device is ready, it sends an interrupt signal to the CPU.
How it works:
Â
- The device sends an interrupt signal when it needs attention.
- The CPU pauses its current task, handles the interrupt through an interrupt service routine (ISR), and then resumes the paused task.
Â
Pros:
Â
- Saves CPU resources by allowing multitasking.
- Faster response to device readiness compared to polling.
Â
Cons:
Â
- Requires additional hardware and software support.
- Frequent interrupts can still affect CPU performance (interrupt overhead).
Â
Example Use Case: Ideal for keyboards, network cards, and real-time systems where timely response is important.
Direct Memory Access (DMA)
DMA is a technique that allows the I/O device to transfer data directly to or from memory without involving the CPU for each byte of data.
How it works:
Â
- The CPU sets up the DMA controller with the memory address, the size of the data, and the direction of transfer.
- The DMA controller manages the data transfer directly between memory and the device.
- Once the transfer is complete, the controller notifies the CPU via an interrupt.
Â
Pros:
Â
- Frees up the CPU to perform other tasks.
- High-speed data transfer with minimal CPU involvement.
- Ideal for large volumes of data.
Â
Cons:
Â
- Requires dedicated DMA hardware.
- Adds system complexity.
Â
Example Use Case: Used in high-speed operations such as reading/writing to hard drives, streaming audio/video, and network communication.