7 Input and Output
Learn how computers exchange data with peripherals, how controllers and device drivers manage those devices, and how polling, interrupts, and DMA coordinate transfers.
1. Input, output, and peripherals
is the exchange of data between a computer and devices outside its main processor and memory. A keyboard, microphone, or sensor sends data into the computer, so it is an input device. A display, speaker, or printer receives data from the computer, so it is an output device. Storage drives and network adapters can both send and receive data.
These categories describe the direction of data exchange, not necessarily separate kinds of hardware: one peripheral may perform both input and output.
2. Controllers, registers, and drivers
The CPU does not usually manage every detail of a peripheral directly. Instead, a manages the device and presents a standard interface to the computer. The controller may provide registers for data, status, and commands. The CPU can use these registers to exchange information, check whether a device is ready, or request an operation.
The operating system’s uses the controller’s interface to manage the device. The CPU can access controller registers through , where the registers occupy addresses in the memory address space. Some processors instead support a separate I/O address space accessed with special instructions. In either approach, the CPU communicates with the controller, which handles device-specific operations.
3. Coordinating transfers
Peripherals can be slower than the CPU, so the computer needs a way to coordinate when data is ready or an operation is complete. Three common approaches are , interrupts, and .
With , the CPU repeatedly checks a device’s status register. For example, it might wait until a printer controller reports that the printer is ready before sending more data. is straightforward, but repeated checks use CPU time. It can be useful when a device needs frequent attention or the wait is very short.
Interrupts
With , the CPU starts an operation and continues executing other instructions. When the device needs attention or finishes, its controller sends an interrupt signal. The processor saves enough information to resume its current work, runs an interrupt handler, and then continues. This avoids constant checking, although handling many frequent interrupts also takes processing time.
For example, when a key is pressed, the keyboard controller can send an interrupt. The processor’s handler reads the available key data and passes it to the operating system for the appropriate program.
Direct memory access
For large or rapid transfers, having the CPU copy every byte or word can be inefficient. With , the CPU sets up the transfer, such as its direction, memory address, and size. A DMA-capable controller then moves data between the device and main memory while the CPU can do other work. The controller commonly interrupts the CPU when the transfer finishes or needs attention. DMA reduces the CPU’s copying work, but requires additional hardware and coordination.
4. Putting an I/O operation together
A storage read shows how the operating system, CPU, controller, and memory work together:
A program requests data, and the operating system’s prepares the request.
The CPU gives the storage controller a command and the necessary information, such as what data to read and where it should go in memory.
The controller operates the device and transfers the data. Depending on the hardware and operation, the transfer may be managed by the CPU or use DMA.
The controller reports completion or an error, often by raising an interrupt.
The operating system makes the result available to the program.
The method of transfer can vary, but the controller provides the hardware interface and the operating system makes the operation usable by programs.
Takeaway: Controllers manage device-specific interactions; and interrupts coordinate when the CPU pays attention; DMA can transfer larger blocks with less CPU copying.