Appearance
42. Buffering / Caching / Spooling
All three temporarily store data, but for different reasons: buffering smooths a speed mismatch, caching avoids repeating expensive work, spooling queues jobs for a device that can't multitask.
1. Buffering — a temporary area absorbing speed differences between a fast producer and slow consumer (e.g., batching many tiny writes into a few large disk writes). Types: no buffering (every write goes straight through — unbuffered stderr), full buffering (flush only when full — file I/O), line buffering (flush on newline — terminal stdout).
2. Caching — storing results of expensive operations so they aren't repeated, exploiting temporal locality (recently used data is likely reused) and spatial locality (nearby data is likely used too). The OS page cache is the classic example: the first read() hits disk and populates the cache; subsequent reads are served from RAM. Replacement policies: LRU, LFU, FIFO. Miss types: cold (unavoidable first access), capacity (cache too small), conflict (two items map to the same slot).
3. Spooling (Simultaneous Peripheral Operations On-Line) — queuing jobs for a slow, shared device that only handles one job at a time (the classic example: a print queue). Processes submit jobs and move on without waiting for the device.
| Buffering Caching Spooling | |||
|---|---|---|---|
| Purpose | Speed mismatch | Avoid repetition | Queue for shared device |
| Data reused? | No | Yes | No |
| Example | TCP buffer | Page cache | Print queue |