Sub topic 2
Buffering, Caching and Spooling
Assume you know processes, system calls, user mode, kernel mode, interrupts, and basic memory operations. This chapter explains how an operating system manages data moving between a producer and a slower or differently timed consumer.
The three mechanisms
A buffer is temporary storage used while data moves between components. A producer can place data in the buffer without waiting for the consumer to handle every item immediately. This handles differences in speed and timing.
A cache stores copies of data that may be needed again. The goal is reuse. If the requested data is already in the cache, the system avoids a slower operation.
Spooling stores complete jobs in a queue, usually on secondary storage, so a device can process them later. The name comes from Simultaneous Peripheral Operations On-Line. Printing is the classic example: many processes submit print jobs, while the printer handles one job at a time.
| Mechanism | Main purpose | Typical location | Example |
|---|---|---|---|
| Buffer | Smooth a data stream | Memory | Keyboard input waiting for a process |
| Cache | Avoid repeated slow reads | Memory or fast storage | Recently used disk blocks |
| Spool | Queue complete jobs | Disk | Print jobs waiting for a printer |
Working model
A buffer is commonly placed between a producer and a consumer. The producer writes data into the buffer, and the consumer removes it. If the buffer becomes full, the producer may block. If it becomes empty, the consumer may block.
Operating systems use double buffering when one buffer is being consumed while another is being filled. This overlaps computation and I/O. A larger buffer can absorb longer bursts, but it consumes more memory and can increase waiting time.
A cache has a hit when the requested data is present. A miss requires fetching the data from the backing source. The hit ratio is:
\[ \text{hit ratio} = \frac{\text{cache hits}}{\text{total requests}} \]
A cache does not normally guarantee that every copy is current. Write-through caching sends updates to the backing store immediately. Write-back caching delays those updates, improving speed but requiring careful recovery after a crash.
Example: explicit flushing
Buffered output may remain in memory until the buffer fills, the program flushes it, or the stream closes.
import io
raw = io.BytesIO()
buffered = io.BufferedWriter(raw, buffer_size=8)
buffered.write(b"abc")
print(raw.getvalue()) # b'' because the buffer has not been flushed
buffered.flush()
print(raw.getvalue()) # b'abc'Common errors
- Calling a buffer a cache: a buffer handles in-progress movement; a cache keeps reusable copies.
- Assuming a successful write means data is already on stable storage. It may still be buffered.
- Treating spooling as caching. A spool queues independent jobs; it does not primarily serve repeated reads.
- Ignoring ordering and failure rules in write-back caches.
Exercise
A process reads the same disk block repeatedly, while a printer receives jobs from many processes. Which mechanism best fits each case?
Show answer
Use a cache for the repeated disk block and a spool for the printer jobs. A buffer may also move data during either operation, but it is not the main purpose in these examples.