1. Principles of I/O hardware
(1) I/O Devices
I/O devices can be roughly divided into tow categories: block devices and character devices.
· The essential property of a block device is that it is possible to read or write each block independently of all the other ones (disk).
· A character device delivers or accepts a stream of characters, without regard to any bock structure. It is not addressable and does not have any seek operation (printer).
· Another special device: clock
(2) Device Controllers
Device Controller contains Mechanical component and Electronic component(device controller or adapter).
(3) Memory-Mapped I/O
Advantage of memory-mapped I/O:
· First, an I/O device driver can be written entirely in c, without memory-mapped I/O, some assembly code is needed (there is no way to execute an IN or OUT instruction in C and C++).
· Second, with memory-mapped I/O, no special protection mechanism is needed to keep user processes fro performing I/O.
· Third, every instruction that can reference memory can also reference control registers.
Separate I/O and memory space
Hybrid

2. I/O control ways
(1) Programmed I/O
The essential aspect of programmed I/O is that after outputting a character, the CPU continuously polls the device to see if it is ready to accept another. This behavior is called polling or busy waiting.
Disadvantage: programmed I/O is simple but has the disadvantage of tying up the CPU full time until all the I/O is done.
(2) Interrupt Driven I/O
· Device is finished.
· Controller issues interrupt.
· CPU Acknowledges interrupt.
Disadvantage: an interrupt occurs on every character. Interrupts take time, so this scheme wastes a certain amount of CPU time.
(3) DMA
Basic idea: let the DMA controller feed the characters to the printer one at time, without the CPU being bothered.
Operation of a DMA transfer:
· CPU programs the DMA controller.
· DMA requests transfer to memory.
· Data transferred from the buffer of disk controller to memory.
· Disk controller Acknowledges DMA controller.
· DMA controller interrupts CPU when done.
Advantage: reducing the number of interrupts from one per character to one per buffer printed.
Disadvantage: the DMA controller is usually much slower than the main CPU.
3. I/O software
(1) Goals of the I/O Software
· Device independence
· Uniform naming
· Error handling
· Synchronous vs. asynchronous transfers
· Buffering
· Sharable vs. dedicated devices
(2) I/O software layers

① Interrupt Handlers
Waking up device driver when I/O done
② Device Driver
Device driver: each I/O device attached to a computer needs some device-specific code for controlling it. The code, called the device driver, is generally written by the device’s manufacturer and delivered along with the device. Since each operating system needs its own drivers, device manufacturers commonly supply drivers for several popular operating systems.
A device driver has several functions:
· Accept abstract read and write requests from the device-independent software.
· Initialize device.
· Manage its power requirements and log events.
· Set up device registers.
· Check status.
③ Device-Independent I/O Software
Functions of the device-independent I/O software:
· Uniform interfacing for device drivers
· Buffering
· Error reporting
· Allocating and releasing dedicated devices
· Providing a device-independent block size
· Uniform interfacing for device drivers
④ user-space I/O software
· Make System calls
· Put their parameters in the appropriate place
· format of input and output is done by library procedures
· spooling
4. Disks
(1) Disk Hardware: Magnetic disk, Hard disk, Floppy disk.
(2) Disk Formatting
· low-level format: The format consists of a series of concentric tracks, each containing some number of sectors, with short gaps between the sectors. The format of a sector is shown in the following figure.
· being partitioned: Logically, each partition is like a separate disk.
· high-level format
(3) Disk Arm Scheduling Algorithms
First, consider how long it takes to read or write a disk block. The time required is determined by three factors:
· Seek time (the time to move the arm to the proper cylinder)
· Rotational delay (the time for the proper sector to rotate under the head)
· Actual data transfer time
Normal disk arm scheduling algorithms:
· FCFS (first come first served): may be require a lot of arm motions.
· SSF (shortest seek first): always handle the closest request next, to minimize seek time. Requests far from the middle may get poor service.
· Elevator algorithm: to keep moving in the same direction until there are no more outstanding requests in that direction, then they switch directions.