THINK FIRST·CODE LATER

← All labs

Polling, interrupts or DMA? An I/O overhead calculator

Problem

For each device of an EdgeCampus server, compute the CPU overhead of the three I/O methods.

Input: first line cpuHz (e.g. 1000000000). Then one device per line:

name bytesPerSecond wordBytes pollCycles irqCycles dmaBlockBytes dmaCycles
  • Polling: one poll per word → (bytesPerSecond / wordBytes) × pollCycles cycles/s
  • Interrupts: one interrupt per word → (bytesPerSecond / wordBytes) × irqCycles
  • DMA: one setup+interrupt per block → (bytesPerSecond / dmaBlockBytes) × dmaCycles

Print for each device:

name: polling 40.0000% | interrupts 50.0000% | DMA 0.1465% -> best DMA

(percent of cpuHz, four decimals; best is the method with the lowest overhead, ties resolved in the order polling, interrupts, DMA). Finally Total with best methods: X% (four decimals).

Input:

1000000000
disk 4000000 4 400 500 4096 1500
keyboard 10 1 400 300 4096 1500

Output:

disk: polling 40.0000% | interrupts 50.0000% | DMA 0.1465% -> best DMA
keyboard: polling 0.0004% | interrupts 0.0003% | DMA 0.0000% -> best DMA
Total with best methods: 0.1465%

(The keyboard's DMA figure rounds to zero, but a real keyboard produces single keystrokes, not blocks — see the model solution's comment.)

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.