Sub topic 3
Buffering, caching and spooling
This chapter assumes you know how polling, interrupt-driven I/O and DMA move data between a device and memory. It also assumes you know what a system call and the kernel are. Buffering, caching and spooling are three kernel services built on top of those transfer mechanisms. They are easy to confuse because all three keep data in memory on the way to or from a device. They solve different problems, and GATE questions often test whether you can tell them apart.
The problem these services solve
Devices and processes rarely run at the same speed. A disk delivers data in blocks, a keyboard delivers single characters, and a CPU consumes data at memory speed. A device such as a printer also cannot be shared by two processes at the same moment. The kernel's I/O subsystem uses three tools to deal with this.
| Technique | Main purpose | Holds | Typical example |
|---|---|---|---|
| Buffering | Absorb speed or size mismatch, and decouple producer from consumer | The only copy of data in transit | Network receive buffer, disk write buffer |
| Caching | Make repeated access faster | A faster copy of data that also lives elsewhere | Disk block cache (buffer cache, page cache) |
| Spooling | Let many processes share a device that serves one job at a time | Whole jobs queued for the device | Print queue |
Buffering
A buffer is a region of memory that holds data while it moves between two places. The kernel uses buffers for three reasons.
- Speed mismatch. A fast producer fills the buffer and a slow consumer drains it. For example, a modem or network link delivers bytes slowly, and the process wants to read a whole block at once.
- Transfer size mismatch. Network protocols split messages into packets. The receiver collects the packets in a buffer and reassembles the message. A disk reads whole blocks, but a process may ask for 10 bytes.
- Copy semantics. When a process calls
write(), the kernel copies the data into a kernel buffer and returns. The process may then change its own memory without corrupting the pending write. The disk write happens later from the kernel's copy.
Single, double and circular buffering
With a single buffer, the device fills the buffer, then the process consumes it. The two steps cannot overlap on the same buffer. Double buffering uses two buffers. While the device fills one, the process consumes the other. Then the roles swap. This removes the wait when the consumer is not slower than the producer. A circular (ring) buffer generalizes this to N slots and suits a producer and consumer that run at uneven rates.
A common GATE calculation compares the time per block. Let the device take T to fill one block and the CPU take C to process it. A copy from the buffer to user space takes M.
- With a single buffer, each block costs about
max(T, C) + Min the usual textbook model, because reading the next block must wait until the current one has been moved out. - With double buffering, each block costs about
max(T, C + M), because reading the next block overlaps with processing the current one.
The following program computes both for a stream of blocks.
def single_buffer_time(n, t, c, m):
# Each block: device fills (t), then data is moved and processed
# (m + c) before the buffer can be refilled, so max(t, c) + m.
return n * (max(t, c) + m)
def double_buffer_time(n, t, c, m):
# Filling the next block overlaps with processing the current one.
return n * max(t, c + m)
n, t, c, m = 100, 10, 6, 1
print(single_buffer_time(n, t, c, m)) # 1100
print(double_buffer_time(n, t, c, m)) # 1000Check the model your exam question states. Some questions define the single-buffer cost as t + c or t + m + c, and then you must follow that definition.
Caching
A cache is a region of fast memory that holds a copy of data stored elsewhere. The key difference from a buffer is that the original data still exists on the slower device. If the cache is lost, the data is not lost, with one exception described below.
The kernel keeps a disk block cache in main memory. On a read, the kernel checks the cache first. A hit returns the data at memory speed. A miss reads the block from disk and stores it in the cache. Effective access time follows the usual formula:
EAT = h × t_cache + (1 − h) × t_disk
where h is the hit ratio. With a 1 microsecond cache time, a 10 ms disk time and a 90% hit ratio, EAT is about 1.0 ms. Even a small miss rate dominates, because the disk is thousands of times slower.
Writes raise a design choice.
- Write-through writes to the cache and to the disk at once. It is safe but slow, since each write waits for the disk.
- Write-back (delayed write) writes only to the cache and marks the block dirty. The kernel flushes dirty blocks later, for example every 30 seconds or on
sync. It is fast, but a crash can lose recent writes. This is the case where a cache briefly holds the only up-to-date copy of the data.
When the cache is full, a replacement policy such as LRU chooses which block to evict. Read-ahead is another common optimization. If a process reads blocks sequentially, the kernel fetches the next blocks before they are requested.
Spooling
A spool (Simultaneous Peripheral Operations On-Line) is a buffer that holds output for a device that cannot accept interleaved data streams. A printer is the standard case. If two programs wrote to it directly, their pages would mix. Instead, each program writes its output to a file on disk. A spooler process, often called a daemon, sends the queued jobs to the printer one at a time.
Spooling gives three benefits. The application finishes quickly, because it does not wait for the slow printer. The device is used by one job at a time, so output is never interleaved. The queue can also be managed, so jobs can be reordered, paused or cancelled.
Spooling is different from buffering in one way that matters. A buffer holds data for a moment, usually in memory, and it serves one transfer. A spool holds complete jobs, usually on disk, and it serves many independent processes.
Common errors
- Calling a cache a buffer. A buffer holds the only copy of data in transit. A cache holds a duplicate for faster reuse. One memory area can serve both roles, as the kernel's buffer cache does, but the concepts stay separate.
- Treating spooling as a way to share any device. It suits devices that serve one job at a time, such as printers. A disk is shared directly by scheduling requests.
- Forgetting the overlap in double buffering. The gain comes from overlapping fill and processing. If the device is much slower than the CPU, or the reverse, the faster side waits and the gain is limited by
max. - Ignoring write-back risk. Faster writes come at the price of possible data loss on a crash. This is one reason file systems use journaling, covered in a later chapter.
Exercise
A device takes 8 ms to fill a block. The CPU takes 5 ms to process a block, and moving a block from the buffer to user space takes 1 ms. Using the models in this chapter, how long does it take to handle 50 blocks with a single buffer and with double buffering? Which is faster, and by how much?
Show answer
Single buffer: 50 × (max(8, 5) + 1) = 50 × 9 = 450 ms. Double buffer: 50 × max(8, 5 + 1) = 50 × 8 = 400 ms. Double buffering is faster by 50 ms, because filling the next block overlaps with moving and processing the current one.
Exercise
A disk block cache has a hit ratio of 95%. A cache access takes 2 microseconds and a disk access takes 8 ms. What is the effective access time, and what happens to it if the hit ratio drops to 90%?
Show answer
At 95%: 0.95 × 0.002 ms + 0.05 × 8 ms = 0.0019 + 0.4 = about 0.402 ms. At 90%: 0.9 × 0.002 + 0.1 × 8 = about 0.802 ms. A five-point drop in hit ratio doubles the effective access time, because the miss term dominates.
Exercise
Two users send print jobs at the same time to one printer. Name the technique that prevents their pages from mixing, and state where the jobs wait.
Show answer
Spooling. Each job is written to a spool file on disk, and a spooler daemon sends the jobs to the printer one at a time.