IO

Input/Output

Categories of I/O devices:

  • Block devices: store information in fixed size blocks that have addresses
  • Character devices: delivers or accepts a stream of characters, not addressable

I/O devices have mechanical and electronic components Device controller: the electronic component, converts bitstream to block of bytes & make it available to main memory

Memory-Mapped I/O #

Controller registers mapped to memory addresses

Advantages:

  • There are no I/O instructions are required to read/write to the control registers
  • Writing I/O device driver is simpler
  • Hardware design involves fewer instructions
  • No special protection mechanism needed for user processes
  • Faster I/O operating, single instruction needed Disadvantages:
  • The page of the device control register must not be cached!

I/O methods #

  • Programmed I/O: lets the CPU do all of the work, polling / busy waiting
  • Interrupt driven I/O: CPU can do something else while waiting for interrupts
  • DMA: works independently of the CPU

Direct Memory Access (DMA) #

Requesting one byte at a time is very slow DMA Controller: accesses the system bus independently of the CPU

Buffering #

Buffering can be performed in different ways:

  • User space: user process provides a buffer that the ISR uses, problem: buffer can be paged out
  • Buffer in kernel + copy: more efficient, when buffer is full the page is loaded and copied to user space at once. Problem: what happens to data when page is loaded
  • Double buffering: has two buffers that can be swapped when the first fills up, one buffer can accumulate new input while the other is being copied to user space
  • Circular buffer: a memory region with two pointers that wrap around

Interrupts #

  1. Device is finished, sends a signal to the interrupt controller
  2. Interrupt controller asserts the interrupt to the CPU until it is handled by the CPU. may be ignore if an interrupt is already in process or higher priority interrupt is executing
  3. Shortly after running interrupt-service routine (ISR) CPU acknowledges interrupt to controller, preventing deadlocks

Interrupts are stored in the Interrupt Vector Table which points to Interrupt Service Routines.

Precise interrupt: leaves the CPU in a well-defined state, PC is saved to known location and instructions up to the PC have been completed Imprecise interrupt: complicated / slow Modern CPU use pipelining and transient execution to do a precise interrupt the speculative execution must be rolled back.

Device drivers #

Device drivers: specific code for a device to control it

HDD #

Hard Disk Drives consists

  • Spindle: rotates and holds patters
  • Platter: a disk
  • Track: consists of sectors which contain data
  • Sector: a block of data, starts with a preamble then the data and ends with an ECC
  • Cylinder: the similar tracks over multiple platters
  • Read/Write head: point of arm that reads/writes data

Times:

  1. Seek time (S) for the RW head to move onto the track
  2. Rotation Latency Time (r) time for disk to find the sector under the head
  3. Block Transfer Time (btt): transfer data from/to head to/from OS
    • in practice controller overhead

Bad sectors: sectors that not correctly read back the value just written to them.

A bad sector can be substituted by a spare or all sectors can be shifted to bypass the bad one

Performance optimizations:

  • Cylinder skew: offset in sectors to improve performance due to track-to-track seek time
  • Interleaving: interleaves sections on track when formatting to allow controller to copy to memory to avoid waiting for almost an entire rotation time

Disk Arm Scheduling #

  • First Come First Served (FCFS): little to optimize seek time moves to cylinders in order of arrival
  • Shortest Seek First (SSF) always handles closest request next, howeverc very slow for requests far from head
  • Elevator / SCAN disk arm moves back en forth
  • Circular SCAN (C-SCAN): immediately returns to beginning, treats cylinders as circular list

CRC #

Used for error detection by using Error Correction Codes (ECC)

RAID #

Redundant Array of Inexpensive/Independent Disks Goals: improving I/O performance and storage reliability

  • Level 0: striping, does not improve reliability
  • Level 1: duplicates all disks, N primary and N backup disks. Every strip written twice, read performance up to 2x
  • Level 5: distributed parity bits

Clock hardware #

  • Hardware clock: hardware consisting of a crystal oscillator that produces accurate frequency signal, a counter and a holding register.
  • Clock ticks: when the counter of the hardware clock counter goes to zero a CPU interrupt is caused
  • Clock software: 32-bit counter is too small -> different solutions of separating timers or using 64-bit
  • Timers: processes can request OS to give signal after an interval, OS keeps a table with the pending timer's signal times
  • Watchdog timer: internal timers for the operating system, e.g. for shutdowns