Input/keluaran
Diperkenalkan
6:22
Arsitektur Komputer
This course looks at the machine below the program, and how it earns its speed. The first two units are the contract, the instruction set, and the machine that…
Narasi bahasa Inggris · takarir bahasa Inggris dan 中文 yang dapat dipilih
Dalam pelajaran ini
| English | Makna |
|---|---|
| Programmed I/O/ˈprəʊɡræmd aɪ əʊ/ | the CPU repeatedly checks a device's status and moves the data itself |
| Interrupt-driven I/O/ˈɪntərʌpt ˈdrɪvən aɪ əʊ/ | the device signals the CPU with an interrupt when it is ready, so the CPU need not keep checking |
| DMA/ˌdiː em ˈeɪ/ | DMA (Akses Memori Langsung) |
| interrupt vector table/ˈɪntərʌpt ˈvektə ˈteɪbl/ | a table of addresses that tells the CPU which handler to run for each interrupt |
| memory-mapped I/O/ˈmeməri mæpt aɪ əʊ/ | device registers appear at memory addresses, so ordinary load and store instructions control the device |
Three transfer styles
- Programmed I/O 程序控制输入输出 (polling) keeps the core checking a status register. Interrupt-driven I/O 中断驱动输入输出 lets the device send a signal, and a handler runs for each event.
- DMA 直接存储器访问 (direct memory access) moves a whole block and interrupts once, at the end.

DMA differs from interrupt-driven I/O because it —
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
A DMA engine owns the transfer; the CPU pays one setup plus one completion.
The polling cost
- A device flag changes every 2 µs, and one poll costs 50 cycles at 250 ps = 12.5 ns. To keep the delay short the core must poll all the time: $2\,\text{µs} / 12.5\,\text{ns} = 160$ polls per event, or 8 000 cycles of CPU time per word.
- Interrupts cut that to the handler alone: 200 cycles per word. DMA 直接存储器访问 moves 1024 words for a 500-cycle setup plus a 200-cycle completion interrupt: 0.68 cycles per word.
| Style | CPU cycles per word | Best for |
|---|---|---|
| Polling | 8 000 | sampling at a fixed rate |
| Interrupts | 200 | events that arrive now and then |
| DMA | 0.68 | blocks that stream |
With 2 µs events and 12.5 ns polls, worst-case polling needs — polls per event
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
2 µs / 12.5 ns = 160 polls, all but one wasted.
Choose by shape
- A keyboard (about 5 characters a second): interrupts. One event per keypress costs 200 cycles, which is almost nothing. A 4 MiB disk block at 2 GB/s: DMA 直接存储器访问. Even a 1024-word block costs 204 800 cycles of overhead with one interrupt per word, against 700 with DMA: 292 times more.
- A sensor read exactly every 2 µs: polling in a tight loop is the right choice, because the sample rate is the polling rate. The rule: poll what must be sampled, interrupt what arrives, use DMA for what streams.
A 2 GB/s disk block transfer belongs to —
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Streaming data is DMA: 0.68 CPU cycles per word against 200 by interrupt.
What happens on an interrupt
- The device raises an interrupt line. The CPU finishes the current instruction, saves the PC and status, blocks lower priorities, and jumps through the interrupt vector table 中断向量表 to the handler.
- The handler serves the device and clears its flag; a return-from-interrupt then restores the PC.
- The overhead is easy to price: $10\,000$ interrupts a second at $200$ cycles each is $2 \times 10^6$ cycles a second, only $0.05\%$ of a $4$ GHz core. At $10^7$ events a second the same handler uses half the core, and then DMA or polling is better.
Put the interrupt sequence in order.
Kesulitan 3/5 · 200 koin + bonus kecil pada penyelesaian pertama hari ini
The current instruction completes first, so the saved state is exact.
A 2 GHz core takes 50 000 interrupts a second at 400 cycles each. What percentage of the core do they use?
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
50 000 x 400 = 2 x 10^7 cycles a second, which is 1% of 2 x 10^9.
Devices at addresses
- With memory-mapped I/O 内存映射输入输出, a device's registers sit at fixed addresses:
sw R5, 0(R9), with R9 holding0xFFFF0000, writes to the device, not to memory. - Those addresses must bypass the cache, and the compiler must not drop or merge the accesses; C's
volatileis for this. A cached status register would report the same value for ever.
Device registers should be cached like ordinary memory to speed up polling.
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
A cached status register would never show the device's changes; device addresses bypass the cache.
In C, a pointer to a device status register should be declared:
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
volatile stops the compiler from caching or removing the reads.
Worked example: what DMA takes from the bus
- DMA 直接存储器访问 does not use the core, but it does use the memory bus. A 2 GB/s disk stream on a 16 GB/s bus takes $2/16 = \mathbf{12.5\%}$ of its bandwidth.
- The core's own cache misses wait a little longer while the block moves. That cost is small and spread out, far less than the 292-fold cost of an interrupt per word.
A 2 GB/s DMA transfer on a 16 GB/s memory bus: share of bus bandwidth (one decimal, %)?
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
$2/16 = 0.125$.
Marks that slip away
- Polling everything. Checking a keyboard in a loop burns a whole core for about $5$ events a second. Interrupt what arrives.
- An interrupt per word for a disk. A 4 MiB block that way floods the CPU; DMA 直接存储器访问 interrupts once at the end.
- Ignoring interrupt cost at high rates. $200$ cycles per event is nothing at $10^4$ a second, but half a $4$ GHz core at $10^7$.
Recap
- Polling checks a status register in a loop; interrupts run a handler per event; DMA 直接存储器访问 moves a block and interrupts once.
- DMA here: $700$ cycles for $1024$ words, $0.68$ cycles per word.
- Poll what must be sampled, interrupt what arrives, use DMA for what streams.
Apa yang salah? Hanya tim situs yang dapat melihatnya.