Operating Systems Lecture 17: Communication with I/O devices

Operating Systems Lecture 17: Communication with I/O devices

Understanding I/O Devices and Operating System Communication

Introduction to I/O Devices

  • The lecture focuses on how operating systems communicate with various input/output (I/O) devices, which include disks, keyboards, mice, network cards, and gaming consoles.

Connection of I/O Devices

  • I/O devices connect to the CPU and memory through different types of buses. Key bus types include:
  • Memory bus for CPU-memory connection.
  • High-speed buses like PCI.
  • Slower buses such as SATA and USB.

Device Types: Block vs Character

  • There are two main types of devices:
  • Block Devices: Expose numbered blocks (e.g., disks).
  • Character Devices: Handle streams of bytes (e.g., keyboards and network cards).

Device Interface and Registers

  • Each device exposes an interface consisting of registers that the operating system interacts with:
  • Status Register: Indicates device status (busy, error).
  • Command Register: Accepts commands for device actions (e.g., read/write).
  • Data Register: Holds data being transferred to/from the device.

Communication Methods with Device Registers

  • The operating system communicates with device registers using two methods:
  • Explicit Instructions: Privileged instructions like in and out on x86 architecture.
  • Memory Mapped I/O: Registers appear as memory locations allowing standard read/write operations.

Executing an I/O Request

  • The process for executing an I/O request involves several steps:
  • Check if the device is busy; wait if it is.
  • Provide data to be written or read from the device.
  • Issue a command to perform the desired operation.

Inefficiencies in Current Protocols

Understanding CPU Inefficiencies in Disk I/O

The Problem with CPU and Disk Interaction

  • Writing to a disk block is inefficient as it wastes CPU cycles; the disk takes time to satisfy requests, leaving the CPU idle in a while loop.
  • Polling, such as using a while loop, is not efficient. Additionally, significant work is involved in copying data, known as program I/O, where the CPU manually handles data transfer.
  • The manual copying of data from main memory to device registers consumes considerable time and CPU cycles for simple tasks.

Introduction of Interrupts

  • Interrupts allow devices to notify the CPU when they have completed an I/O request instead of constant polling by the CPU.
  • When a process issues an I/O request (e.g., writing to disk), it can be put to sleep while another process runs on the CPU until the disk completes its task.
  • Upon completion, the disk raises an interrupt that signals the CPU to resume execution of the original process.

Handling Interrupts Efficiently

  • The operating system uses an interrupt descriptor table that contains routines for handling interrupts from various devices based on their IRQ numbers.
  • Once an interrupt occurs, the kernel processes notifications from devices and manages any waiting processes or queued requests efficiently.

Trade-offs of Interrupt Driven I/O

  • While interrupts improve efficiency by allowing other processes to run during I/O operations, context switching can introduce overhead if devices complete tasks quickly.

Optimizing Data Transfer with DMA

  • Another inefficiency arises from CPUs spending excessive time copying data between main memory and device registers during write operations.
  • Direct Memory Access (DMA) allows a special hardware engine to handle data transfers without burdening the CPU with this task.
  • By instructing the DMA engine to copy data directly from memory to a device, CPUs can focus on executing other processes rather than managing these transfers.

Conclusion: Benefits of Modern Techniques

Understanding Device Drivers and DMA

The Role of DMA in Data Transfer

  • The Direct Memory Access (DMA) engine facilitates data transfer by copying received data from a device to main memory before raising an interrupt, allowing the CPU to focus on other tasks without managing data copying.
  • Utilizing interrupts and DMA significantly reduces CPU cycle usage, enhancing overall system efficiency during data handling operations.

Functionality of Device Drivers

  • Each device is managed by a specific piece of OS code known as a device driver, which understands the unique language and operational commands of that device.
  • Device drivers are responsible for knowing the locations of various registers within the device, such as command and status registers, enabling effective communication between the OS and hardware.

Higher-Level Operating System Code

  • Most operating system code operates at a higher abstraction level, focusing on block-level operations rather than low-level hardware interactions.
Video description

Based on the book Operating Systems: Three Easy Pieces (http://pages.cs.wisc.edu/~remzi/OSTEP/) For more information please visit https://www.cse.iitb.ac.in/~mythili/os/