I/O Management Fundamentals for System Design

I/O Device Fundamentals

Block Devices

Devices that store data in fixed-size blocks (e.g., 512 bytes or 4 KB) and allow for random access. Examples include hard drives, SSDs, and DVDs.

Character Devices

Devices that send or receive a stream of characters. Examples include the keyboard, mouse, and serial ports.

Why can’t you seek with character devices? Data arrives as a real-time stream, making random access impossible.

Memory-Mapped vs. Separate I/O

Separate I/O

The CPU uses special instructions to communicate with dedicated I/O ports.

Memory-Mapped I/O

I/O devices share the same address space as RAM.

  • Advantage: It is easier to program, as I/O is treated like memory.
  • Disadvantage: It reduces the amount of available addressable memory.

Direct Memory Access (DMA)

DMA facilitates data transfers directly between I/O devices and memory, without involving the CPU.

How DMA Works

  1. The CPU provides the DMA controller with the source address, destination address, and transfer size.
  2. The DMA controller performs the data transfer independently.
  3. Once the transfer is complete, the DMA controller interrupts the CPU.

Why is DMA better than interrupt-driven I/O for large transfers? The CPU is not interrupted for every piece of data, only once the entire transfer is finished, freeing it for other tasks.

Interrupts and System State

Interrupts

An interrupt is a signal that causes the CPU to pause its current task to service an external device or event.

Precise Interrupts

A precise interrupt must meet four criteria to ensure a predictable system state:

  1. The program counter (PC) is saved in a known location.
  2. All instructions before the one indicated by the PC are fully executed.
  3. No instructions after the one indicated by the PC have been executed.
  4. The execution state of the instruction indicated by the PC is known.

What happens if one of these criteria fails? The interrupt becomes imprecise, leading to an unpredictable and potentially unstable system state.

Examples of systems with precise interrupts include CPU exceptions and real-time systems.

Core Goals of I/O Software

The primary objectives of I/O software include:

  • Device Independence: Allows software to work with any device type. For example, the same read() function can work for both a hard drive and a USB drive.
  • Uniform Naming: Provides a consistent naming scheme for files and devices.
  • Error Handling: Manages and reports errors at the software layer rather than exposing them to the user application.
  • Synchronous vs. Asynchronous Transfers: Supports both blocking (synchronous) and non-blocking (asynchronous) I/O operations.
  • Buffering: Smooths data flow and manages speed mismatches between the CPU and I/O devices.

I/O Operation Methods

Programmed I/O

The CPU continuously polls the device to check if it is ready. This method is simple but wastes CPU cycles. An example is a simple print loop.

Interrupt-Driven I/O

The I/O device sends an interrupt signal to the CPU when it is ready for a data transfer.

DMA-Based I/O

A dedicated DMA controller handles the data transfer. The CPU is only interrupted once the entire transfer is complete. This is the most efficient method for large file transfers.

The Layers of I/O Software

I/O software is typically structured in the following layers, from highest to lowest level:

  1. User-Level I/O Software
  2. Device-Independent OS Software
  3. Device Drivers
  4. Interrupt Handlers
  5. Hardware

The role of device-independent software is to handle device naming, buffering, allocation, and error reporting in a generic way.

Interrupt Handler Procedure

The typical sequence of steps for handling an interrupt is:

  1. Save the current CPU registers, including the Program Status Word (PSW). If the CPU state isn’t saved, the process state can become corrupted.
  2. Set up the context and stack for the interrupt handler.
  3. Acknowledge the interrupt to the device.
  4. Execute the Interrupt Service Routine (ISR) to handle the specific request.
  5. Decide which process to run next.
  6. Restore the state of the next process and resume its execution.

Data Buffering Techniques

Buffering is used to manage speed differences between devices and improve efficiency.

  • Unbuffered I/O: Data is transferred directly between the device and the application’s memory.
  • User-Level Buffering: A temporary storage area is maintained in the user space of an application.
  • Kernel-Level Buffering: The operating system maintains a buffer in kernel space.
  • Double Buffering: Two kernel buffers are used. While one buffer is being filled by the I/O device, the other is being used by the application. This technique significantly improves throughput and is often the fastest method for high-performance tasks.

Magnetic Disk Structure

  • Cylinders: A set of tracks on all platters at the same arm position.
  • Sectors: A pie-shaped slice of a track, representing the smallest unit of storage.
  • Physical Geometry: The actual hardware layout of the disk.
  • Logical Geometry: An abstracted view of the disk’s layout presented to the operating system. This abstraction ensures OS compatibility across different drive models.

RAID (Redundant Array of Independent Disks)

  • RAID 0 (Striping): Focuses on speed by striping data across multiple disks. It has no fault tolerance.
  • RAID 1 (Mirroring): Provides redundancy by duplicating data on two or more disks.
  • RAID 2-6: Incorporate various forms of parity and error correction for a balance of performance and redundancy.

For the fastest read performance, RAID 0 or RAID 5 are typically the best choices.

Low-Level Disk Formatting

  • Sector Structure: Each sector contains a header, the data payload, and an Error-Correcting Code (ECC) for detecting and correcting data errors.
  • Cylinder Skew: Sectors are offset between adjacent tracks to reduce latency when the head switches tracks, avoiding delays.
  • Interleaving: Spacing sectors apart on a track to give slower disk controllers time to process data before the next sector arrives.

Disk Arm Scheduling Algorithms

Disk access time is a combination of seek time, rotational latency, and transfer time. Scheduling algorithms aim to minimize seek time.

  • SSF (Shortest Seek First): Selects the request closest to the current head position, minimizing arm movement.
  • Elevator (SCAN): The disk arm moves in one direction, servicing all requests in its path, then reverses direction.

Principles of Stable Storage

A technique using a redundant pair of disks to ensure data integrity, even in the event of a crash or power loss. It is crucial for databases and file systems.

Key Operations

  • Stable Write: Ensures data is successfully written to both disks before the operation is considered complete.
  • Stable Read: Reads data from either disk. If an error is found on one, it reads from the other and corrects the faulty one.
  • Crash Recovery: A mechanism to recover from a partial write that may have occurred during a system failure.

System Clocks and Timers

A system clock is typically a programmable timer chip that generates interrupts at regular intervals. Clock interrupts are the fundamental mechanism that enables time-slicing for process scheduling.

Clock Software Responsibilities

  • Maintaining the time of day.
  • Enforcing process time limits.
  • Compiling CPU usage statistics.
  • Handling user-set alarms.
  • Implementing watchdog timers to prevent processes from running indefinitely (e.g., in an infinite loop).
  • Providing data for system profiling.

The X Window System Architecture

A client-server model for graphical user interfaces where messages are passed over a network protocol or local socket. This model provides a modular design and allows applications to be displayed on remote machines.

  • Client: The application program (e.g., a web browser or text editor).
  • Server: The software that manages the display, keyboard, and mouse.