Sub topic 11
Comparing the three techniques and solving GATE-style problems
This chapter assumes you know the basics of I/O interface registers, memory-mapped and isolated I/O, programmed I/O with polling, the interrupt cycle with the interrupt service routine (ISR), and the DMA controller with its bus request and grant handshake. Here you will put the three techniques side by side. You will also learn a repeatable method for the numerical problems GATE CS asks about.
The three techniques side by side
All three techniques move data between a device and the computer. They differ in who notices that the device is ready and who moves the data.
| Aspect | Programmed I/O (polling) | Interrupt-driven I/O | DMA |
|---|---|---|---|
| Who detects readiness | CPU, by reading the status register in a loop | Device, by raising an interrupt request | Device asks the DMA controller, which owns the transfer |
| Who moves the data | CPU, one word at a time | CPU, inside the ISR, one word at a time | DMA controller, directly between device and memory |
| CPU cost | Busy for the whole wait plus the transfer | One interrupt overhead per word | One setup and one completion interrupt per block |
| Extra hardware | Almost none | Interrupt lines, interrupt controller or priority logic | DMA controller, bus arbitration |
| Latency to react | Short if the loop is tight, but the CPU can do nothing else | Bounded by interrupt latency plus ISR entry | Set by when the controller wins the bus |
| Best fit | Very fast devices, simple systems, short waits | Slow or irregular devices such as keyboards | Fast block devices such as disks and network cards |
The main idea is that the cost per unit of work moves. Polling spends CPU time on waiting. Interrupt-driven I/O spends CPU time on switching context for every word. DMA spends CPU time once per block, and the controller spends bus cycles on the data itself.
When polling still wins
Polling is not always worse. Interrupt handling has a fixed cost: the CPU finishes the current instruction, saves the program counter and flags, looks up the vector, runs the ISR, and restores state. If a device is ready almost every time the CPU checks, that cost can exceed the cost of a short polling loop. Embedded systems with a dedicated control loop, and devices that must be serviced at a precise time, often poll for this reason.
When interrupts beat DMA
DMA needs setup: the CPU writes the memory address, the word count, and the direction to the controller's registers. For a single byte from a keyboard, that setup costs more than the transfer. Interrupts are the better choice for small, infrequent transfers.
Hidden costs to remember
- DMA still uses the bus. The CPU is free to compute, but it cannot use the memory bus while the controller holds it. In burst mode the CPU may stall for the whole block. In cycle stealing the CPU loses one bus cycle per word. Transparent DMA uses only cycles the CPU does not need, so it costs the CPU nothing, but it is slower to finish.
- DMA still interrupts once. After the last word, the controller raises an interrupt so the CPU knows the block is done.
- Interrupts have latency. Between the device raising the request and the first ISR instruction there are the instruction-completion time, the state save, and the vector fetch. GATE problems usually fold this into a single "cycles per interrupt" figure.
- Polling cost depends on the poll rate. If the CPU polls at least as often as the device produces data, it must poll continuously. If it polls on a timer, the cost is the poll rate times the cycles per poll.
A method for GATE-style problems
Most numerical I/O questions follow the same pattern. Use these steps every time.
- Convert the device rate into events per second. For polling and interrupts, the event is usually one word (or byte). For DMA, the event is one block.
- Find the CPU cycles spent per event. Add up what the problem lists: polling cycles, interrupt overhead, ISR body, DMA setup, completion interrupt.
- Multiply events per second by cycles per event. This gives CPU cycles consumed per second.
- Divide by the clock rate. This gives the fraction of CPU time. Multiply by 100 for a percentage.
- Check units. Bits versus bytes, KB as 1024 or 1000, MHz as 10^6. GATE usually states which convention it uses. If it does not, follow the style of the numbers given (for example, "1 MB/s" with a 500 MHz clock is typically decimal).
Worked example 1: polling versus interrupts
A device transfers data at 1 MB/s in 4-byte words. The CPU runs at 500 MHz. Polling takes 400 cycles per word check-and-read, and the CPU must poll fast enough to miss nothing. An interrupt costs 1000 cycles per word. Compare the two.
Words per second: 1,000,000 / 4 = 250,000.
- Interrupt-driven: 250,000 × 1000 = 250,000,000 cycles per second. Divided by 500,000,000 gives 0.5, or 50% of CPU time.
- Polling at the word rate: 250,000 × 400 = 100,000,000 cycles per second. Divided by 500,000,000 gives 0.2, or 20%.
In this setup polling uses less CPU time than interrupts. Polling also has a wasted-poll side: if the device is ready only part of the time, the CPU polls many times per word. When the problem says the CPU polls only at the device word rate, 20% holds. A device that is ready at this high a rate is exactly the case where interrupts lose their advantage.
Worked example 2: DMA for the same device
The same device uses DMA with 4096-byte blocks. Setup costs 2000 cycles and the completion interrupt costs 1000 cycles.
Blocks per second: 1,000,000 / 4096 ≈ 244.14.
CPU cycles per second: 244.14 × 3000 ≈ 732,422. Divided by 500,000,000 gives about 0.00146, or about 0.15% of CPU time.
You can reproduce both numbers with a short program. It uses the same assumptions as the example: 1 MB/s, 4-byte words, a 500 MHz CPU, 1000 cycles per interrupt, and 2000 plus 1000 cycles per DMA block.
# CPU time used by a 1 MB/s device on a 500 MHz CPU
rate_bytes_per_s = 1_000_000
word_bytes = 4
cpu_hz = 500_000_000
# Interrupt-driven: one interrupt per word, ISR costs 1000 cycles
words_per_s = rate_bytes_per_s / word_bytes
interrupt_fraction = words_per_s * 1000 / cpu_hz
# DMA: one transfer per 4096-byte block, 2000 cycles setup + 1000 cycles completion interrupt
block_bytes = 4096
blocks_per_s = rate_bytes_per_s / block_bytes
dma_fraction = blocks_per_s * (2000 + 1000) / cpu_hz
print(f"Interrupt-driven: {interrupt_fraction:.2%} of CPU time")
print(f"DMA: {dma_fraction:.4%} of CPU time")The output is 50.00% for interrupt-driven I/O and 0.1465% for DMA. The ratio is about 340 to 1.
Worked example 3: cycle stealing slowdown
A DMA controller in cycle-stealing mode moves one 32-bit word per stolen bus cycle. The memory cycle time is 100 ns, and the device delivers one word every 1 µs. How much of the memory bandwidth does DMA use, and how much is left for the CPU?
Each word costs one 100 ns bus cycle out of every 1000 ns. DMA uses 100 / 1000 = 10% of memory bus time, leaving 90% for the CPU. This is why cycle stealing slows the CPU by at most about the DMA's share of the bus, instead of freezing it as burst mode does.
Worked example 4: how long can the CPU be locked out?
In burst mode, a controller transfers a 1 KB block (1024 bytes) of 4-byte words over a bus with a 50 ns cycle. The CPU cannot use the bus during the burst.
Words: 1024 / 4 = 256. Burst time: 256 × 50 ns = 12,800 ns = 12.8 µs.
If the CPU needs the bus for a high-priority interrupt response, it may wait up to 12.8 µs. Questions on this pattern ask whether a burst is acceptable given a deadline.
Frequent GATE patterns
| Pattern | What to compute | Common trap |
|---|---|---|
| Fraction of CPU time for interrupt-driven I/O | (rate in words/s × cycles per interrupt) / clock | Using bytes per second instead of words per second |
| Fraction for DMA | (rate in blocks/s × (setup + completion cycles)) / clock | Charging an interrupt per word instead of per block |
| Maximum transfer rate the CPU can sustain by polling | clock / cycles per poll-and-read, times word size | Forgetting to multiply by the word size |
| Time to transfer a block with cycle stealing | words × (bus cycle + any CPU-visible delay) | Ignoring that stolen cycles are only part of the memory cycle time |
| Interrupt priority with a daisy chain | The device closest to the CPU on the chain wins | Reversing the order |
| Interrupt latency | Wait for current instruction + state save + vector fetch | Forgetting the wait for the current instruction to finish |
Maximum sustainable rate
A related question reverses the calculation. If an interrupt costs 1000 cycles on a 500 MHz CPU and the CPU may devote at most 100% of its time to I/O, the CPU can take 500,000,000 / 1000 = 500,000 interrupts per second. With 4-byte words, that is a ceiling of 2,000,000 bytes per second, about 2 MB/s. A real system needs headroom for other work, so problems often cap I/O at a fraction such as 50%, which halves the ceiling.
Priority questions
Priority questions test the daisy-chain and masking rules from the earlier chapter. Remember three points:
- In a daisy chain, the interrupt acknowledge signal passes through devices in physical order. The first device with a pending request keeps it and stops the signal, so the device nearest the CPU has the highest priority.
- A masked interrupt is not lost. It stays pending in the device or controller until it is unmasked, unless the question says the line is edge-triggered and the edge was missed.
- A non-maskable interrupt cannot be disabled by the interrupt-enable flag. It is used for events such as power failure or memory errors.
Worked example 5: nested interrupts
Three devices, A, B, and C, have priorities A > B > C. While the CPU runs C's ISR, B interrupts, and then A interrupts during B's ISR. Assuming interrupts are enabled inside ISRs, the order of completion is A, then B, then C. The CPU suspends C for B, then suspends B for A. Each ISR resumes in last-in, first-out order because the return addresses sit on the stack. If B had interrupted while the CPU ran A's ISR instead, B would wait until A finished, because a lower-priority request cannot preempt a higher-priority ISR.
Common errors
- Mixing bits and bytes. A "10 Mbit/s" network device moves 1.25 MB/s, not 10 MB/s.
- Using the wrong event. DMA is per block. Interrupt-driven I/O is per word (or per byte if the problem says so). Write the event next to your formula before you compute.
- Forgetting that the DMA CPU cost is not zero. Setup and completion still take cycles.
- Treating cycle stealing as free. The CPU loses the stolen cycles. Only transparent DMA is truly free of CPU delay.
- Rounding too early. Keep fractions until the final step. Block counts such as 244.14 per second are not integers, and rounding them to 244 changes later digits.
- Assuming interrupts are always better than polling. For a device ready at a very high rate, the fixed interrupt cost makes interrupts more expensive than a tight polling loop, as example 1 showed.
Summary of the decision
Choose polling when the wait is short and the CPU has nothing else to do. Choose interrupts when the device is slow or unpredictable and the CPU has other work. Choose DMA when data comes in large blocks at high rates, because the per-block cost is far lower than the per-word cost of the other two. In every case, ask what the CPU does during the wait and what it pays per event.
Exercise
A disk transfers data at 8 MB/s (1 MB = 10^6 bytes) in 4-byte words. The CPU runs at 1 GHz. Each interrupt costs 500 cycles. (a) What fraction of CPU time does interrupt-driven I/O use? (b) With DMA in 8000-byte blocks, setup costs 4000 cycles and the completion interrupt costs 1000 cycles. What fraction does DMA use?
Show answer
(a) Words per second = 8,000,000 / 4 = 2,000,000. Cycles per second = 2,000,000 × 500 = 1,000,000,000. Divided by 1 GHz gives 1.0, so interrupt-driven I/O uses 100% of CPU time and cannot keep up with any other work. (b) Blocks per second = 8,000,000 / 8000 = 1000. Cycles per second = 1000 × (4000 + 1000) = 5,000,000. Divided by 10^9 gives 0.005, so DMA uses 0.5% of CPU time.
Exercise
A DMA controller in cycle-stealing mode transfers 16-bit words. The memory cycle time is 200 ns and the device produces one word every 2 µs. (a) What percentage of memory bus time does DMA use? (b) How long does a 512-word block take to transfer?
Show answer
(a) Each word occupies one 200 ns bus cycle in every 2000 ns, so DMA uses 200 / 2000 = 10% of the bus time. (b) The device limits the rate, so the block takes 512 × 2 µs = 1024 µs, about 1.02 ms. The bus is busy for only 512 × 200 ns = 102.4 µs of that time.
Exercise
Four devices D1, D2, D3, D4 sit on a daisy chain in that order starting from the CPU. D2 and D4 raise requests at the same time. Which is served first, and what happens to the other?
Show answer
D2 is served first, because the acknowledge signal reaches D2 before D4 and D2 keeps it. D4 stays pending, and is served after D2's ISR finishes, provided no higher-priority request arrives first.