Sub topic 1
I/O Devices and Controllers
This chapter introduces how an operating system talks to hardware, how the disk is organized, and how to compute disk access time and head movement for FCFS and SSTF scheduling.
Prerequisites
This chapter assumes you know basic operating system terms such as processes, system calls, and kernel mode. You should also be comfortable with powers of two, converting between KB, MB, and GB, and computing averages.
I/O devices and controllers
An I/O device is any hardware that moves data in or out of a computer, such as a keyboard, network card, or disk. A device controller is a small chip that manages one device. The controller has device registers: memory locations the CPU reads and writes to send commands and check status.
Polling, interrupts and DMA
The CPU can learn that a device is ready in three ways:
- Polling: the CPU repeatedly checks the status register. It wastes CPU time when the device is slow.
- Interrupt-driven I/O: the device raises an interrupt when it finishes. The CPU runs other work in the meantime.
- DMA (direct memory access): a DMA controller copies data between the device and memory without the CPU touching each byte. The CPU gets one interrupt when the whole transfer finishes.
Buffering, caching and spooling
A buffer holds data while it moves between two devices that run at different speeds. A cache keeps copies of recently used data for fast reuse. Spooling queues output for a slow device, such as a printer, so programs do not wait for it.
How a magnetic disk is organized
A magnetic disk stores data on circular platters. Each platter is divided into concentric rings called tracks. A track is split into fixed-size sectors. A cylinder is the set of tracks at the same radius across all platters. The disk arm moves across cylinders, and the platters spin under the head.
Computing disk access time
Access time has three parts:
- Seek time: moving the arm to the right cylinder.
- Rotational latency: waiting for the sector to spin under the head. On average this is half a rotation.
- Transfer time: reading the sectors.
def avg_rotational_latency_ms(rpm):
return (60_000 / rpm) / 2
def access_time_ms(seek_ms, rpm, sectors_per_track, sectors_read):
rotation_ms = 60_000 / rpm
return seek_ms + rotation_ms / 2 + rotation_ms * sectors_read / sectors_per_track
print(round(avg_rotational_latency_ms(7200), 2)) # 4.17
print(round(access_time_ms(9, 7200, 400, 8), 2)) # 13.33FCFS and SSTF scheduling
The disk scheduler orders pending requests. First Come First Served (FCFS) serves requests in arrival order. Shortest Seek Time First (SSTF) always serves the pending request closest to the current head position. SSTF moves less but can starve requests far from the head.
Exercise
Exercise
The head starts at cylinder 53. The request queue is 98, 183, 37, 122, 14, 124, 65, 67. What is the total head movement under FCFS?
Show answer
640 cylinders. The moves are 45, 85, 146, 85, 108, 110, 59, and 2, which sum to 640.