1. Home
  2. Explore
  3. Education
  4. Transcription and Subtitles

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Transcription and Subtitles

Input/Output Systems in Operating Systems

Input/output (I/O) is the part of an operating system that manages communication between the computer and devices outside the CPU and main memory. Examples include keyboards, monitors, printers, hard disks, SSDs, USB drives, webcams, network interfaces, and other peripherals.

I/O matters because these devices are extremely different from one another. An operating system must provide a consistent way for applications to use them even though their connectors, speeds, data formats, and methods of operation may differ.

For example, an application should be able to request that a file be opened without needing to know whether the file is stored on an SSD, a USB drive, or a network server. The operating system hides those hardware-specific details.

An I/O system provides the hardware, software, and control mechanisms needed to transfer data between a computer and its devices.

Why I/O is difficult to manage

A computer may contain many I/O devices, and each device may have different characteristics:

  • An SSD can transfer data rapidly.
  • A printer is much slower and may take a long time to complete a request.
  • A keyboard generally produces one character at a time.
  • A hard disk or SSD usually transfers blocks of data.
  • A monitor primarily receives data from the computer.
  • A keyboard primarily sends data to the computer.
  • A storage device can usually both read and write.
  • Some devices operate sequentially, while others allow random access.
  • Some devices can be shared by several processes, while others must be dedicated to one process at a time.

The operating system must therefore solve several problems:

  1. How should devices connect to the computer?
  2. How should data move between the device and memory?
  3. How should the CPU know when a device needs attention?
  4. How can slow devices operate without making the entire computer wait?
  5. How can several processes safely share a device?
  6. How can device errors be detected and handled?
  7. How can unauthorized processes be prevented from directly controlling hardware?

I/O hardware organization

Buses and ports

A bus is a communication pathway used to transfer data and control information between components. A computer may have a main system bus, such as a PCI-based bus, to which other interfaces and controllers are connected.

A port or connector provides a physical or logical connection for a device. USB is a familiar example. A single USB interface can connect many different devices, including:

  • Keyboards
  • Mice
  • USB storage devices
  • Phones
  • Webcams
  • External hard disks
  • Foot pedals
  • Monitors or hubs

Although these devices use the same general USB connection, they do not operate in the same way internally. Each device has its own controller and device-specific behavior.

An expansion bus allows additional devices and interfaces to connect to the main system bus. Older systems might have used interfaces such as IDE, SCSI, serial ports, or parallel ports. Modern systems commonly use USB and other high-speed interfaces instead.

Device controllers

A device controller is hardware that manages a particular type of device. It acts as an intermediary between the operating system and the physical hardware.

For example, a USB controller manages communication with USB devices, while a storage controller manages communication with storage devices. A controller may contain:

  • Control logic
  • Status information
  • Registers
  • Temporary memory called a buffer
  • Hardware-specific instructions for operating the device

The controller understands the physical details of the device. The operating system does not need to know exactly how a USB mouse or an SSD performs its internal operations. It can issue a general request, such as “read data,” and the controller handles the device-specific work.

Registers

A register is a small storage location used by a processor or controller. I/O controllers use registers to exchange information with the CPU and operating system.

Registers may contain:

  • Data read from a device
  • Data waiting to be sent to a device
  • Status information
  • Control commands
  • Whether the device is currently busy
  • Whether an error occurred

The general process resembles reading from memory. Data travels across the bus and is placed into a register or memory location where the CPU or operating system can access it.

A status register is especially important. If a printer is already processing a request from one application, another application should not immediately send more data without coordination. Otherwise, the output could become mixed together.

Device drivers

A device driver is software that allows the operating system to communicate with a particular device or class of devices.

The driver understands the device’s:

  • Commands
  • Data format
  • Connection method
  • Operational characteristics
  • Error conditions
  • Controller registers

The operating system can use a common interface, while the driver translates that general request into the specific commands required by the hardware.

For example, an application might request that a character be read from a keyboard. The keyboard driver knows how to communicate with the keyboard controller and retrieve the character. The application does not need to know those details.

A device requires cooperation among several parts:

  1. The application requests an operation.
  2. The operating system’s I/O subsystem receives the request.
  3. The appropriate device driver translates the request.
  4. The device controller communicates with the physical device.
  5. Data is transferred through the bus and possibly through buffers.
  6. The result is returned to the application.

Installing software for a new printer, keyboard, mouse, graphics card, or storage device is often necessary because the operating system needs the appropriate driver.

The device driver understands the software interface, while the device controller understands the hardware interface. Together, they connect the operating system to the physical device.

Think About It

Why is a single USB port able to support a keyboard, hard disk, webcam, and phone even though each device performs a completely different task?

Representing I/O devices

The operating system must decide how hardware devices are represented internally. Two important approaches are memory-mapped I/O and port-mapped I/O.

Memory-mapped I/O

With memory-mapped I/O, device registers are assigned addresses that resemble memory addresses. The CPU uses ordinary memory instructions to communicate with the device.

For example, a particular address might represent a graphics location. Writing data representing the color blue to that address could cause the graphics hardware to make a particular pixel blue.

The CPU appears to be writing to memory, but the address actually refers to a device or device register. The hardware recognizes the address and sends the appropriate signal to the device.

Memory-mapped I/O is useful because it allows devices to be accessed using familiar memory operations. However, the operating system and hardware must carefully reserve particular address ranges so that memory and devices do not conflict.

Port-mapped I/O

With port-mapped I/O, devices have a separate I/O address space. Special I/O instructions are used instead of ordinary memory instructions.

In this arrangement, the CPU distinguishes between:

  • Instructions that access main memory
  • Instructions that access an I/O port

Both approaches allow the CPU to communicate with hardware, but they organize the addresses and instructions differently.

Detecting when a device needs attention

A device may need to tell the CPU that an event has occurred. For example:

  • A keyboard may have received a key press.
  • A storage controller may have completed a read.
  • A network interface may have received data.
  • A device may have encountered an error.

Two main techniques are used: polling and interrupts.

Polling

With polling, the CPU repeatedly checks each device to ask whether it has data or needs attention.

The process might look like this:

  1. Check the keyboard.
  2. Check the storage controller.
  3. Check the network interface.
  4. Check the printer.
  5. Return to the keyboard and repeat.

Polling is straightforward and can be fair because every device is checked in turn. It may work adequately when there are only a few fast devices and the operating system knows exactly what devices are present.

However, polling can be inefficient. The CPU may spend time repeatedly checking devices that have nothing to report. A slow device can also delay the checking of devices later in the sequence. If data arrives and is not checked soon enough, information may be lost, especially when the device cannot reproduce the data.

Interrupts

An interrupt allows a device to notify the CPU when it needs attention. Instead of the CPU repeatedly asking whether something happened, the device raises an interrupt request.

The general sequence is:

  1. The CPU is executing a process.
  2. A device raises an interrupt request.
  3. The CPU temporarily suspends the current activity.
  4. The CPU runs an interrupt handler.
  5. The handler communicates with the device and collects the necessary information.
  6. The handler usually places the data into a buffer.
  7. The CPU returns to the interrupted process.

For example, when a key is pressed, the keyboard can interrupt the CPU. The keyboard interrupt handler retrieves the character and stores it in a buffer. It does not usually interpret or process the character fully. Its goal is to perform the urgent hardware work quickly and then return control to the interrupted process.

Interrupt handlers should therefore be:

  • Short
  • Fast
  • Focused on essential work
  • Careful about delaying other processes

The actual processing of the input can occur later, outside the interrupt handler.

Interrupt priority

Several devices may request attention at approximately the same time. The system therefore needs a way to determine which interrupt should be handled first.

Interrupts may have different priorities. Critical hardware events can receive higher priority than less urgent events.

Some interrupts are maskable. A maskable interrupt can temporarily be ignored or disabled while the CPU handles more important work.

A non-maskable interrupt cannot normally be ignored. It is reserved for events that must be serviced immediately.

Interrupt systems may also handle exceptions generated by the CPU, including events such as:

  • Divide errors
  • Debugging events
  • Breakpoints
  • Page faults
  • Floating-point errors
  • System calls

An interrupt may be interruptible or non-interruptible depending on its priority and the design of the system. If an interrupt handler can itself be interrupted, the system must ensure that the higher-priority event can be handled safely.

Interrupt coordination

A shared bus or interrupt line can create a problem if several devices attempt to communicate simultaneously. The system must determine which device is requesting service and avoid having devices transmit over one another.

Clock signals help coordinate operations. Devices can attempt communication at defined clock moments. If multiple devices attempt access simultaneously, a device may back off for a random period before trying again. Before transmitting, it can check whether the communication path is free.

This is similar to people using a shared conversation channel: each participant needs a way to indicate that they want to speak, and the system needs a method for deciding who speaks next.

Think About It

Why is interrupt-based I/O generally more efficient than polling when many devices are connected?

Direct memory access

The limitation of CPU-controlled transfers

Without special support, the CPU may have to manage every unit of data transferred between a device and memory. If a storage device transfers a large block, the CPU might repeatedly perform the following actions:

  1. Request one word of data.
  2. Receive the word.
  3. Store it in memory.
  4. Request the next word.
  5. Repeat until the entire transfer is complete.

This occupies the CPU and prevents it from doing useful work during the transfer.

DMA operation

Direct memory access, or DMA, allows a device controller to transfer a block of data directly to or from main memory.

The process is:

  1. The CPU specifies the source of the data.
  2. The CPU specifies the destination.
  3. The CPU specifies the amount of data to transfer.
  4. A DMA controller performs the transfer.
  5. The CPU continues executing other work.
  6. The DMA controller interrupts the CPU when the transfer finishes or encounters an error.

DMA is particularly useful for block devices such as hard disks and SSDs because they transfer large quantities of data.

DMA improves performance by reducing the number of individual data-transfer operations performed by the CPU. However, it requires additional hardware, including a DMA controller, so it adds cost and complexity.

DMA transfers large blocks of data between devices and memory with limited CPU involvement.

Types of I/O devices

Devices can be categorized according to how they transfer data and how they are used.

Character devices

A character device transfers data one character or unit at a time. Keyboards are a common example. A keyboard does not normally deliver an entire file as one operation; it produces individual key events.

Character devices are often associated with streams of data. The order in which data arrives matters, and the data is usually processed sequentially.

Block devices

A block device transfers data in blocks. Storage devices commonly use this model. A request might involve a block of a particular size rather than a single character.

Block devices are suitable for files and other structured data because transferring larger blocks is more efficient than transferring every byte separately.

Sequential devices

A sequential device must be accessed in order. A tape drive is an example: reaching later data requires moving through the earlier data.

A keyboard also produces a sequential stream because key events arrive in a particular order.

Random-access devices

A random-access device allows data to be accessed at different locations without reading every preceding item. A hard disk or SSD can retrieve data from a selected location.

A display can also be treated as supporting random access because the system can update one selected region or pixel without redrawing the entire screen.

Synchronous and asynchronous devices

A synchronous operation proceeds in a coordinated sequence, with the requesting process receiving the result as part of the operation.

An asynchronous operation allows the device to continue working independently. The requesting process can continue with other work and receive notification later when the operation completes.

Read-only, write-only, and read-write devices

Devices also differ in the direction of data transfer:

  • A keyboard is primarily an input device.
  • A monitor is primarily an output device.
  • A hard disk or SSD supports both reading and writing.

Shared and dedicated devices

A shared device can be used by multiple processes, often through operating-system coordination. A hard disk is an example.

A dedicated device should generally be assigned to one process at a time. A printer illustrates this problem: if two processes print simultaneously without coordination, their output could become mixed together.

Blocking, non-blocking, synchronous, and asynchronous I/O

The way a process waits for I/O strongly affects system performance.

Blocking I/O

With blocking I/O, a process stops and waits until the I/O operation finishes.

For example, a process requesting data from a slow storage device may be unable to continue until the data has arrived.

Blocking I/O is simple to implement:

  1. The process requests I/O.
  2. The process waits.
  3. The device completes the operation.
  4. The process resumes.

The disadvantage is that the CPU and process may spend significant time waiting, especially when the device is slow.

Non-blocking I/O

With non-blocking I/O, a process does not wait indefinitely for the operation to finish. It performs some available work, checks or receives a partial result, and continues.

A non-blocking operation may report how much data has been transferred so far. The process can then return later to continue the operation.

This approach can use resources more effectively, but it is more complicated because the process must handle incomplete transfers and repeated attempts.

Synchronous I/O

In synchronous I/O, the operation follows a coordinated sequence. The request passes through the operating system, driver, interrupt handling, and device transfer before the result is returned.

The process is informed when the requested operation has reached completion.

Asynchronous I/O

With asynchronous I/O, the request is initiated and the process continues working while the device performs the operation.

The data may be placed in a buffer. When the transfer has completed, the operating system notifies the process that the data is available.

Asynchronous I/O can improve resource use because slow device operations do not force the CPU to remain idle. The cost is greater implementation complexity: the system must track outstanding operations, buffers, completion events, and possible errors.

Think About It

Why would a web browser, operating system, or server benefit from asynchronous I/O rather than waiting for every disk or network operation to finish?

The I/O subsystem

The operating system usually contains an I/O subsystem within the kernel. Its purpose is to provide common services for many different devices.

The subsystem may handle:

  • Device scheduling
  • Buffering
  • Caching
  • Spooling
  • Device status
  • Error handling
  • Access protection
  • Resource allocation
  • Communication with drivers and controllers

The I/O subsystem creates a boundary between applications and hardware. Applications use general operations such as opening, reading, writing, and closing. The subsystem and drivers translate those operations into hardware-specific actions.

Scheduling I/O requests

When several processes request the same device, the operating system may need to schedule the requests.

Scheduling can serve two purposes:

  1. Efficiency: Arrange requests in an order that reduces unnecessary device movement or delay.
  2. Fairness: Ensure that one process does not keep the device permanently, preventing other processes from making progress.

For example, a printer may receive several print requests. The operating system can place the requests into an organized queue rather than allowing every process to write directly to the printer.

Different devices may require different scheduling strategies because their speeds and access methods differ. A storage device, printer, and network interface do not have identical scheduling needs.

Quality of service

Quality of service refers to providing processes with predictable or guaranteed access to resources.

An operating system may use quality-of-service rules so that each process receives a suitable amount of access to a shared device. This prevents one process from monopolizing the device and helps maintain fairness.

Buffering

A buffer is an area of memory used to hold data temporarily while it moves between components operating at different speeds.

Suppose main memory can accept data much faster than a USB device can provide it. If the operating system transferred data directly at every moment, the faster component might have to wait constantly for the slower one.

A buffer solves this mismatch:

  1. The slow device transfers data into the buffer.
  2. The buffer stores the data temporarily.
  3. The system later transfers the buffered data at the speed of the bus or memory.

Buffers are also useful when devices transfer different-sized units. A keyboard may provide one character at a time, but an application may process several characters together. The buffer holds the characters until the application is ready.

Multiple copies and copy semantics

Data may exist simultaneously in several locations:

  • Inside the device
  • In a device-controller buffer
  • In a kernel buffer
  • In a cache
  • In an application’s memory

This can create a consistency problem. If one copy changes, the system must know which copy is current and which copies must be updated.

This issue is related to copy semantics. Whenever data is copied, the system must establish which version is authoritative, particularly if the data is modified and later written back to a device.

Caching

A cache stores frequently or recently used data in faster memory.

If the operating system expects that data will be needed again, it can keep a copy in the cache. A later request can then be satisfied from fast memory instead of returning to a slow device.

For example, a file stored on a hard disk or network drive may also be kept in local memory. Reading it again from the cache is much faster than retrieving it from the original device.

Caching improves performance, but it creates the same consistency concern as buffering. The system must determine whether the cached copy or the device copy contains the most recent version.

Spooling

Spooling allows a process to place output into a queue rather than waiting for a slow device to process it immediately.

Printing is the standard example:

  1. An application creates a print file.
  2. The file is placed in a spool queue.
  3. The application continues working.
  4. The printer retrieves jobs from the queue.
  5. The printer processes each job when it is ready.

The print file usually contains not only the characters to print but also formatting information needed to reproduce the intended output.

Spooling is useful when a device is slow or can serve only one request at a time. It separates the speed of the application from the speed of the device.

Device reservation and deadlock

Some devices may be reserved for a particular process. Reservation can prevent conflicts, but it can also create deadlock.

Deadlock occurs when processes wait for one another in a cycle. A simple analogy is a circular arrangement of vehicles at a roundabout, where every vehicle is waiting for another vehicle to move first.

In an I/O system, deadlock might occur as follows:

  1. Process A holds access to one device and waits for another.
  2. Process B holds the second device and waits for the first.
  3. Neither process can continue.
  4. Neither process releases its device.

The result is a permanent waiting cycle. Operating systems must therefore manage reservations and resource allocation carefully.

Error handling

I/O devices can fail in many ways. The operating system must determine how to respond to errors such as:

  • A failed read
  • A failed write
  • A disconnected device
  • Corrupted data
  • A device that does not respond
  • A storage block that cannot be accessed

Possible responses include:

  • Retrying the operation
  • Reporting an error to the process
  • Marking a storage block as unusable
  • Recording the event in a log
  • Stopping use of a failing device

A storage controller may retry access to a block several times. If the block continues to fail, it can be marked as bad so that the operating system avoids using it in the future.

The operating system must also decide when to report an error. Repeatedly retrying a temporary problem may be sensible, but endless retries would prevent the rest of the system from continuing.

I/O protection

Processes must not be allowed to control hardware directly without operating-system supervision.

A badly behaved or malicious process might attempt to:

  • Access another process’s device operation
  • Read data from a device without permission
  • Send commands directly to a controller
  • Interfere with another process’s output
  • Bypass operating-system checks

The operating system therefore requires I/O operations to pass through controlled system mechanisms, such as system calls. Direct access to protected hardware instructions or registers is blocked.

This is similar to memory protection. Just as a process should not access another process’s memory, it should not be able to interfere directly with devices or with I/O operations belonging to other processes.

Operating-system records and object-based management

The kernel must maintain information about the devices and requests in the system. It needs to know:

  • Which devices exist
  • Which devices are connected
  • Which process is using each device
  • Which requests are waiting
  • Which operations are currently active
  • What type of device is involved
  • What errors have occurred
  • Which buffers or caches contain copies of the data

Some operating systems use object-oriented structures and message-passing methods to organize this information. Devices, files, network connections, and file-system records can be represented as structured objects that refer to one another.

This organization requires memory and processing time. Every additional device, buffer, cache, status record, or controller adds management overhead, but these structures allow the operating system to coordinate complex I/O safely.

How a file request travels through the system

An application thinks in terms of files and simple operations. It may request that a file be opened or read. The actual process is much more involved.

A typical read request follows these stages:

  1. Application request: The application asks to open or read a file.
  2. Name translation: The operating system determines which device corresponds to the file name or drive identifier.
  3. File-system processing: The operating system determines where the requested data is located.
  4. I/O subsystem processing: The request is passed to the appropriate subsystem.
  5. Device-driver processing: The relevant driver converts the general request into device-specific commands.
  6. Controller operation: The device controller communicates with the physical device.
  7. Data transfer: The data travels through a buffer, cache, DMA operation, or another transfer mechanism.
  8. Completion notification: The system learns that the operation has completed, often through an interrupt.
  9. Return through the layers: The result moves back through the controller, driver, kernel, and I/O subsystem.
  10. Application access: The application receives the data and uses it.

If the required data is already in a cache, the process may finish much sooner because the physical device does not need to be accessed.

Network files add another stage. The request must travel over a network to a different computer, where the file is stored or processed. The network introduces additional delay, which is why cloud-storage services often keep local synchronized copies of frequently used files.

Layered I/O design

I/O is organized in layers so that each layer has a manageable responsibility.

A simplified structure is:

  1. Application layer: Requests files, input, or output.
  2. Operating-system I/O subsystem: Provides common services.
  3. Device driver: Understands a particular device or interface.
  4. Interrupt handler: Responds quickly when a device requires attention.
  5. Device controller: Operates the hardware.
  6. Physical device: Performs the actual input or output.

Layering means that an application does not need to know whether a file is stored on an SSD, USB drive, or network system. It also means that a new device can be supported by adding or modifying an appropriate driver rather than redesigning every application.

Network I/O uses the same general principle. Network protocols separate hardware communication, data transfer, and application-level meaning into different layers. This allows different hardware and communication technologies to work with the same higher-level applications.

Layering allows the operating system to present a consistent interface while hiding the differences among physical devices.

Comprehensive Review

An operating system’s I/O system connects applications and the CPU to a wide variety of hardware devices. Because devices differ in speed, direction, access method, and data size, the system needs several cooperating mechanisms.

  • Ports and buses provide physical and logical connections.
  • Device controllers operate the hardware.
  • Device drivers translate operating-system requests into device-specific commands.
  • Registers hold data, commands, and status information.
  • Memory-mapped I/O uses memory-like addresses for devices, while port-mapped I/O uses separate I/O instructions.
  • Polling repeatedly checks devices, whereas interrupts allow devices to request attention.
  • DMA transfers blocks directly between devices and memory with little CPU involvement.
  • Blocking I/O waits for completion, while non-blocking and asynchronous methods allow other work to continue.
  • Buffers handle differences in speed and data size.
  • Caches keep frequently used data in faster memory.
  • Spooling places output into a queue for slow devices.
  • Scheduling and quality of service coordinate competing requests.
  • Error handling manages failed operations and unusable storage areas.
  • Protection prevents processes from bypassing operating-system control.
  • Resource management prevents conflicts, including deadlock.
  • Layering hides hardware differences and gives applications a consistent interface.

The central purpose of all these mechanisms is to make slow, varied, and complicated hardware appear manageable and consistent to the programs that use it.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent