I/O
How the kernel talks to devices: programmed I/O, interrupts, DMA, block vs character.
Level: intermediate
What is it?
Input/output is the transfer of data between software and devices. Operating systems provide device abstractions, drivers, buffering, scheduling, error handling, and completion notification; mechanisms include programmed I/O, interrupt-driven I/O, and direct memory access (DMA).
Why does it matter?
CPU and device speeds differ greatly, and devices can fail or complete asynchronously. Good I/O paths avoid wasting CPU while waiting and preserve consistency across buffers, caches, and device state.
Mental model
Treat the device as an independent actor. The CPU prepares a request and memory buffers, the device performs work, and completion is reported through polling, interrupts, or another event path.
How it works
- Programmed I/O makes the CPU repeatedly inspect or move device data, which can waste cycles for slow operations.
- Interrupt-driven I/O lets the CPU do other work until a device signals an event.
- DMA allows a device controller to transfer data between the device and main memory with less CPU copying, subject to platform mapping and protection rules.
- Block devices transfer addressable blocks; character devices expose stream-like or device-specific semantics.
- Buffered and asynchronous APIs change when data are copied, visible, or considered complete.
Steps
- The kernel/driver prepares an I/O request and validates access.
- The device controller performs the transfer, potentially using DMA.
- The device signals completion or the driver observes it through polling.
- The kernel wakes the waiting task or completes an asynchronous request and reports errors/status.
Real-world usage
- Comparing blocking, nonblocking, and asynchronous I/O.
- Reading system traces of disk, network, and device waits.
- Understanding buffering and completion semantics in application code.
Common mistakes
- Assuming an I/O completion means data is durable on stable storage.
- Mixing up interrupt-driven I/O and DMA; one reports events, the other moves data.
- Ignoring cancellation, partial transfers, and error codes.
Interview questions
What is the role of DMA?
It lets a device controller transfer data to or from main memory with less CPU intervention, under appropriate mapping and protection setup.
Does using interrupts mean the CPU performs no work for I/O?
No. The CPU still prepares requests, handles completion, coordinates buffers, schedules tasks, and processes errors.