I/O Hardware Overview
Devices connect to the CPU through buses and I/O ports. The CPU reads/writes device registers to issue commands and check status. Modern systems use memory-mapped I/O: device registers appear as regular memory addresses.
Polling
Simplest I/O model: the CPU repeatedly reads the status register until the device is ready. Simple, but wastes CPU cycles in a busy loop.
Interrupts
The device signals the CPU when it finishes. The CPU saves state, runs an interrupt service routine (ISR), then resumes the interrupted work. Interrupt controllers (APIC) prioritize and vector interrupts to the right handler.
Direct Memory Access (DMA)
For bulk transfer (disk, network), the CPU programs a DMA controller with source, destination, and count; the controller moves bytes directly between memory and device without CPU involvement and raises one interrupt when done.
Device Drivers
A device driver is kernel code that understands a particular device's protocol. Drivers expose a uniform interface (open, read, write, ioctl) so the OS core and applications are device-independent.
I/O Software Layers
- User-level libraries (stdio, fread).
- OS system-call interface (read, write).
- Device-independent kernel software (buffering, caching, device naming).
- Device drivers.
- Interrupt handlers and hardware.
Buffering, Caching, Spooling
- Buffering — match producer/consumer speeds.
- Caching — keep recently used data in fast storage.
- Spooling — queue output for shared devices like printers.
Disk Structure
A magnetic disk has platters, tracks, sectors, and cylinders. SSDs have no moving parts but use pages and blocks with NAND flash.
Disk Access Time
Access = Seek time + Rotational latency + Transfer timeSeek time dominates; disk scheduling aims to minimize it.
Disk Scheduling Algorithms
Assume pending requests at cylinders 98, 183, 37, 122, 14, 124, 65, 67 and head at 53:
FCFS
Serve in arrival order. Fair but long seeks. Total head movement for the example ≈ 640 cylinders.
SSTF (Shortest Seek Time First)
Always pick the nearest pending request. Minimizes seek but risks starving far requests.
SCAN (Elevator)
Head moves in one direction serving requests until the end, then reverses. Fairer than SSTF.
C-SCAN
Head moves in one direction; when it reaches the end, it jumps to the other end without serving and starts again. Provides uniform waiting.
LOOK / C-LOOK
Like SCAN/C-SCAN but the head reverses at the last request, not the end of the disk.
RAID
Redundant Array of Independent Disks combines multiple disks for speed and/or reliability:
- RAID 0 — striping, no redundancy.
- RAID 1 — mirroring.
- RAID 5 — striping with distributed parity.
- RAID 6 — double parity.
- RAID 10 — mirror + stripe.
SSD Considerations
SSDs have no seek penalty but wear out with writes. Controllers use wear leveling and TRIM commands to extend life. File systems like F2FS are optimized for flash.
Disk Formatting and Partitioning
Low-level formatting marks sectors; partitioning divides the disk into logical units (MBR, GPT); high-level formatting creates a file system.
Swap Space
A dedicated partition or file used by virtual memory. On swap-heavy systems, placing swap on a fast SSD improves responsiveness.
Summary
I/O hardware speaks through drivers; the OS hides device details behind a uniform API. Disk scheduling reduces seek time; RAID delivers performance and reliability; SSDs change the performance profile and need different algorithms.
Important Questions
- Differentiate polling and interrupt-driven I/O.
- What is DMA? Why is it used?
- List and describe the I/O software layers.
- Define seek time, rotational latency, and transfer time.
- Compute total head movement for FCFS and SSTF given a request queue.
- Explain SCAN and C-SCAN with diagrams.
- Compare RAID 0, 1, 5, and 10.
- How are SSDs different from HDDs for OS design?