# Input-Output and Peripheral Communication

Computer Science I · Introduction to Computers · https://tryals.app/learn/computer-science-i/input-output-and-peripheral-communication

## Peripheral Communication

A CPU on its own is deaf and mute; **input/output (I/O)** is how it communicates with keyboards, disks, networks, and displays. Because these devices operate on their own slower schedules, the system needs disciplined ways to coordinate.

The simplest method is **polling** (programmed I/O), where the CPU repeatedly asks a device in a loop if it is ready yet. While simple, polling wastes valuable CPU cycles through busy-waiting, doing no useful work while waiting.

| I/O Method | CPU Involvement | Best Used For |
| --- | --- | --- |
| **Polling** | 100% busy-wait | Trivial or timing-critical tasks |
| **Interrupts** | Pauses only to handle events | General asynchronous devices |
| **DMA** | Interrupted only at the end | Large blocks of bulk data |

## Interrupts and DMA

To avoid wasting cycles, we use **interrupts**. The CPU issues a request and continues other work until the device sends an **interrupt** signal, prompting the CPU to run an **interrupt handler**.

For bulk data transfers, **direct memory access (DMA)** is used. A dedicated **DMA controller** moves data between devices and main memory without the CPU handling every single byte.

> **Common pitfall:** Assuming polling is better because it is simpler. Polling wastes CPU cycles; use **interrupts** or **DMA** for efficiency.

## Practice questions

6 of this lesson's 11 practice questions, with answers. The full set is in the app.

### 1. Order what happens when a device signals the CPU with an interrupt.

**Answer:**

1. The CPU issues its request to the device and carries on with other work
2. The device finishes and raises an interrupt signal
3. The CPU pauses and saves its current state
4. The interrupt handler runs and deals with the device
5. The CPU restores its state and resumes where it left off

**Why:** The CPU does not wait: it requests, works on something else, and is called back. Saving state before the handler and restoring it afterwards is what makes the interruption invisible to the interrupted program. Putting the save after the handler would destroy the work being resumed.

Page: https://tryals.app/practice/computer-science-i/input-output-and-peripheral-communication/order-what-happens-when-a-device-signals-the-cpu-with-an-interrupt

### 2. Polling, interrupts and DMA all move data between a device and memory. Why does adding DMA improve throughput far more than switching from polling to interrupts?

A. Because DMA transfers data faster over the bus than the CPU can.
B. Because interrupts are unreliable and can be missed, while DMA cannot.
C. Because with interrupts the CPU still copies every byte; DMA does not.
D. Because DMA compresses the data during the transfer.

**Answer:** C. Because with interrupts the CPU still copies every byte; DMA does not.

**Why:** Polling wastes the CPU asking 'ready yet?'; interrupts fix that and the CPU still executes every copy. DMA removes that too, program the controller once and it transfers the lot, interrupting at the end, so involvement drops from a thousand operations to two. The cost is bus contention and stale cached copies, which is why cache coherence appears as soon as DMA does.

Page: https://tryals.app/practice/computer-science-i/input-output-and-peripheral-communication/polling-interrupts-and-dma-all-move-data-between-a-device-and

### 3. Sort each peripheral by the direction in which data mainly flows.

**Answer:**

- Mainly input: Keyboard, Mouse
- Mainly output: Display, Printer
- Both directions: Disk, Network card

**Why:** Keyboards and mice only deliver data in; displays and printers only take data out; disks and network cards do both, which is why they are the devices that most need DMA. Filing the disk as input only, because 'loading a file' is the familiar case, is the usual slip.

Page: https://tryals.app/practice/computer-science-i/input-output-and-peripheral-communication/sort-each-peripheral-by-the-direction-in-which-data-mainly-flows

### 4. A DMA controller writes data into memory while the CPU holds an older copy of that region in its cache. What problem does that create?

A. The DMA transfer fails because the memory is locked by the cache
B. Cache incoherence, the CPU reads its stale cached copy
C. No problem. DMA writes always go through the cache
D. The cache is automatically flushed, which slows the transfer

**Answer:** B. Cache incoherence, the CPU reads its stale cached copy

**Why:** DMA's whole point is bypassing the CPU, so the cache is never told the memory changed and happily serves stale bytes. Device drivers therefore invalidate cache ranges around transfers, an unglamorous detail, and a classic source of bugs that appear only under load when the line happens to still be resident.

Page: https://tryals.app/practice/computer-science-i/input-output-and-peripheral-communication/a-dma-controller-writes-data-into-memory-while-the-cpu-holds-an-older

### 5. Match each mechanism or component to what it does.

**Answer:**

- Polling → The CPU repeatedly asks whether the device is ready
- Interrupt → The device signals the CPU only when it needs attention
- Interrupt handler → The short routine run before the CPU resumes its work
- DMA controller → Moves a whole block without the CPU touching each byte

**Why:** Polling puts the CPU in charge of asking; an interrupt puts the device in charge of telling. The handler is the code run on arrival; the DMA controller is separate hardware that does the bulk copying. The shift from asking to being told is the central idea of the lesson.

Page: https://tryals.app/practice/computer-science-i/input-output-and-peripheral-communication/match-each-mechanism-or-component-to-what-it-does

### 6. Select every statement that is a genuine advantage of interrupts over polling.

A. A slow device no longer holds up the processor
B. The CPU never has to save or restore any state
C. The CPU can do useful work while the device is busy
D. The transfer itself becomes faster than the device can physically manage
E. The device gets the CPU's attention only when it actually needs it

**Answer:** A. A slow device no longer holds up the processor; C. The CPU can do useful work while the device is busy; E. The device gets the CPU's attention only when it actually needs it

**Why:** Interrupts free the CPU between request and completion and remove the need to hold the processor hostage to a slow device. They do not speed up the device itself, and they actually add the cost of saving and restoring state on each interrupt, so option 2 has the truth exactly backwards.

Page: https://tryals.app/practice/computer-science-i/input-output-and-peripheral-communication/select-every-statement-that-is-a-genuine-advantage-of-interrupts-over
