Buffering & Device Drivers
Devices and the CPU run at different speeds. Buffers absorb the mismatch, and drivers hide the hardware behind a stable interface.
In plain English
A buffer is a waiting room where fast and slow workers can hand things off.
By the end you will be able to
- Explain single, double, and circular buffering.
- Describe the role of a device driver.
- Distinguish blocking from non-blocking I/O.
- Know why buffering improves throughput.
Buffering
A buffer is a temporary memory region between a producer and a consumer. Single buffering lets the device fill a buffer while the CPU processes the previous one only after the transfer finishes. Double buffering uses two buffers so the device can fill one while the CPU processes the other. A circular buffer is a ring of slots used for streaming data.
/* double buffering */
device writes buffer A
CPU processes buffer B
swap A and B
device writes buffer B
CPU processes buffer ADevice drivers
A device driver is kernel code that knows how to talk to a specific device. It translates a generic request such as read 4 KB into device-specific register writes, DMA setup, and interrupt handling. The rest of the kernel sees a uniform block or character device interface. Drivers are the reason the OS can support many devices without rewriting the file system for each one.
Blocking versus non-blocking I/O
Blocking I/O suspends the calling process until the operation completes. Non-blocking I/O returns immediately, and the process polls or waits for readiness later. Asynchronous I/O starts the operation and notifies the process on completion. The choice affects concurrency and programming complexity.
Key takeaways
- Buffers absorb speed differences between devices and the CPU.
- Double buffering overlaps device transfer and CPU processing.
- Circular buffers suit streaming data.
- Device drivers translate generic requests into device-specific operations.
- Blocking, non-blocking, and asynchronous I/O trade simplicity for concurrency.
Common pitfalls
- Assuming a buffer removes the need for flow control.
- Confusing a driver with a user-space library.
- Using blocking I/O in a single-threaded server that must serve many clients.
Kernel notes
- Explain single versus double buffering.
- State the driver's role in one sentence.
- Compare blocking, non-blocking, and asynchronous I/O.
Quick check
1. What is the main benefit of double buffering?
2. What does a device driver do?