Transcription and Subtitles


Transcription and Subtitles

Hope
Today we're focusing on operating-system input and output, or I/O. This follows naturally from file systems and storage systems, because those systems have to exchange data with devices through I/O systems. The important point is that I/O is not one single mechanism: different devices work differently and must connect to the operating system, applications, and users in different ways.

Corey
And the range of devices is enormous. A typical computer might have a keyboard, mouse, monitors, a webcam, a phone, a USB drive, internal disks, and network connections. Some devices, such as an SSD, are fast, while a printer or tape drive can be much slower. The operating system has to manage all of them despite their different speeds, connection methods, and patterns of interaction.

Hope
Devices also differ in the direction of communication. A monitor is mainly an output device, a keyboard is mainly an input device, and a hard disk supports both reading and writing. Some devices transfer one character or item at a time, while others transfer blocks of data. So the system needs hardware and software for connecting devices, transferring data, and controlling access to them.

Corey
The hardware side includes ports, buses, and device controllers. A bus carries data between components, while a controller contains electronics that understand and operate a particular device. A controller may include control registers, memory, a cache, or a buffer. The controller for a USB mouse is therefore different from the controller for a USB storage device, even though both may use the same kind of connector.

Hope
That USB example is useful because one connector type can support many different devices. A keyboard, mouse, phone, webcam, portable disk, or even foot pedal may all connect through USB. Those devices share an expansion bus, which connects through an interface to a larger system bus, such as the PCI bus. The common connection does not make the devices identical; each still needs its own controller and appropriate software.

Corey
The software that understands a particular device is called a device driver. A driver accounts for the device's particular setup, functions, and connection requirements. When you install a new mouse, printer, keyboard, monitor, graphics card, or internal disk, the operating system may need the corresponding driver so it can communicate with that hardware.

Hope
The operating system tries to provide general I/O instructions rather than inventing an entirely new set of instructions for every device. For example, an operating system can request that data be read without needing the same low-level procedure for a keyboard, hard disk, and USB drive. The device-specific hardware and driver handle how that general request is carried out.

Corey
Registers help support this process. Data read from an I/O device can be placed into a register, and other registers can be used to send data or control the device. A status register might indicate whether a device is busy or available. That matters because two processes should not normally write to a printer simultaneously, or their output could become mixed together.

Hope
There are two broad ways to represent I/O addresses. With memory-mapped I/O, the system treats a device as if it has memory locations. The same kinds of instructions used to write to memory can then be used to write to the device. For example, writing a value representing blue to a particular mapped address could cause a graphics adapter to make a particular screen pixel blue.

Corey
The alternative is to use separate I/O instructions. In that model, the processor explicitly identifies an operation as an I/O operation rather than an ordinary memory operation. Memory-mapped I/O is common, and computers can contain well-known I/O addresses for devices such as interrupt controllers and graphics hardware.

Hope
Once the operating system can send requests to devices, it needs a way to learn when a device has data or a response ready. One possibility is polling. The processor repeatedly checks each device in turn by asking, 'Do you have anything for me?' This is similar to a teacher going around a class and checking every student.

Corey
Polling is fair in the sense that each device gets checked, but it can be inefficient. If one device takes a long time to respond, the processor may spend too much time waiting, and another device's data might arrive and be missed or delayed. Polling can still be reasonable when the devices are fast, known in advance, and manageable, but it becomes less attractive with many devices or unpredictable data.

Hope
The more common approach is an interrupt. Instead of the processor continually asking whether something happened, a device raises an interrupt request when it needs attention. The processor pauses its current work, runs the appropriate interrupt handler, and then returns to the original process.

Corey
For example, when someone presses a key, the keyboard can interrupt the CPU. The keyboard interrupt handler obtains the character and places it in a buffer. It does not try to process the character fully; its job is to capture the data quickly and store it so that the process using the keyboard can handle it later. Keeping interrupt handlers small and fast allows the processor to resume its previous work quickly.

Hope
Interrupts can also have priorities. Some interrupts are maskable, meaning the processor can temporarily ignore or postpone them, while non-maskable interrupts must be serviced. On the processor model discussed in the lecture, the first group of interrupt entries includes non-maskable interrupts, while later entries are maskable. Interrupts can represent device events, but they can also represent exceptions such as divide errors, debugging events, breakpoints, page faults, and system calls.

Corey
That raises an interesting question: can an interrupt itself be interrupted? The answer depends on how the interrupts are defined. Some interrupt handling can be interrupted, while other handling is designated uninterruptible. Priorities and maskability let the system decide which events are important enough to receive immediate service.

Hope
There is still a performance problem because many devices may share one bus and one processor. If the CPU transfers a word of data at a time, it has to remain involved in every part of a large transfer. While it is repeatedly fetching the next word, it cannot spend that time doing other useful work.

Corey
Direct memory access, or DMA, reduces that burden. The CPU starts a transfer by specifying details such as how much data to move, where the data comes from, and where it should go in memory. A separate DMA controller then transfers a larger block, such as several kilobytes, and interrupts the CPU when the operation is complete. The CPU can continue with other tasks during the transfer.

Hope
DMA is especially useful for devices such as hard disks, which often transfer substantial blocks of data. Instead of requesting one word, then another, the processor can request a whole block and be notified afterward. The trade-off is that DMA requires additional hardware, so it improves performance but has an implementation cost.

Corey
Devices can also be classified by how they transfer data. Character devices, such as keyboards, provide a stream of individual characters. Block devices, such as disks, transfer blocks of data. Devices may use sequential access, like a tape drive or a keyboard, or random access, where the system can go directly to a chosen location, as it can with a disk or a particular screen pixel.

Hope
Another distinction is whether I/O is synchronous or asynchronous. With synchronous I/O, a request is made and the operation completes through the device driver and interrupt handling before the result is returned. With asynchronous I/O, the request starts the operation and the process continues doing other work; later, the system reports that the transfer has finished or that data is available.

Corey
Blocking and non-blocking I/O describe related choices. In blocking I/O, the process waits until the operation finishes. That is simple to implement, but it can be slow and inefficient because nothing else can proceed in that process while it waits. Non-blocking I/O allows the process to do some work, check what has transferred, and continue rather than stopping completely.

Hope
Asynchronous I/O goes further: after requesting the transfer, the process does not wait around for the result. Data can be placed in a buffer while the process continues. When the operation is complete, the system can notify the process. This is more difficult to implement, but it uses system resources more effectively, especially because I/O devices are often much slower than the CPU.

Corey
The I/O subsystem has several responsibilities beyond simply moving bytes. It may schedule access to devices, especially devices such as printers. Scheduling can prevent one process from holding a device indefinitely and can help ensure fairness. Providing each process with a guaranteed share of a resource is described as quality of service.

Hope
Buffering is another major responsibility. A buffer is an area of memory between a device and the rest of the system. It absorbs data from a slow device until the system or bus is ready to handle it. For example, a disk controller can collect a block of data in a buffer and then transfer that block efficiently, while keyboard input can wait in a buffer until the application is ready to use it.

Corey
Buffering is also useful when devices transfer different amounts or at different speeds. Without a buffer, the system might have to stop and wait for the slow device. With one, memory can accept data quickly while the device continues its slower operation. The lecture also connects buffering with copy semantics, where several places may contain versions of the same data, so the system must know which copy is current and which copies need to be written back.

Hope
Caching has a similar performance goal. If data retrieved from a slow device is likely to be needed again, the system can keep a copy in faster memory. It can then satisfy a later request from the cache rather than contacting the device again. A device might involve several layers at once: data could exist on the device, in a device buffer, in a cache, and in main memory, so the system must carefully track changes and keep the copies consistent.

Corey
Spooling is another way to avoid waiting for a slow device. Printing is the clearest example. Instead of forcing an application to wait while the printer works, the application creates a print file and places it in a spool queue. The printer later takes files from that queue and prints them, using the required format for the print job.

Hope
The operating system also maintains device-status information. A device-status table can record which devices are connected, which process is using each one, and which requests are waiting. It can also record details such as the location and size of requested data. Scheduling decisions may depend on those requests; for example, serving a small request first could sometimes clear it quickly before handling a much larger request.

Corey
Device reservation must be handled carefully because it can contribute to deadlock. Imagine processes and devices arranged like cars at a roundabout, where each one is waiting for something held by the next. If process A holds one device and waits for another, while process B holds the second and waits for the first, neither can continue. The operating system therefore has to be cautious when reserving devices for particular processes.

Hope
Error handling is another essential part of I/O. A read or write may fail, so the system has to decide whether to retry, how many times to retry, and when to report the problem. A disk controller might mark a block as bad after repeated failures and prevent it from being used. If too many bad blocks appear, the problem may need to be logged or reported for attention.

Corey
I/O must also be protected. Just as a process should not access memory belonging to another process without permission, it should not bypass the operating system and directly issue unrestricted commands to devices. I/O requests should go through system calls and kernel control. Direct attempts to bypass those controls are blocked so that devices and other processes remain protected.

Hope
The kernel coordinates all of this. It tracks the devices connected to the system, the drivers and controllers they use, which processes are using them, their speeds and capabilities, and their current status. Some operating systems use object-oriented structures and message passing to represent devices, file systems, and networking components. Those records consume memory and resources, so adding devices has a management cost.

Corey
Windows, for example, uses an object-oriented method with a message-passing protocol to implement I/O. More generally, operating systems provide interfaces for reading, writing, and locating data regardless of whether the underlying device is a character device, block device, raw connection, or network resource. Programming languages and APIs expose commands that applications can use to access those interfaces.

Hope
Network I/O adds another layer. An application may use a socket interface to communicate over a network, while the lower layers deal with the network hardware and protocols. This separation is important because the way an application requests network data does not have to be identical to the way the hardware sends signals. It follows the broader idea of separating responsibilities into layers.

Corey
The complete path becomes clearer if we follow a file request. A user or application asks to open a file, but the system first translates the file or drive name into a particular device. It then uses the I/O subsystem, device driver, interrupt mechanisms, and controller to request the data. The device places the result into a buffer or memory, and the information travels back through the layers until the application is told that the file is available.

Hope
So even a simple action such as pressing a key or opening a file can involve many layers. The request may pass through the application interface, the I/O subsystem, the kernel, the device driver, the interrupt handler, and the controller before data is transferred. The response then travels back up those layers. The reason this feels instantaneous is that the system uses buffering, caching, DMA, scheduling, and other techniques to reduce the apparent delay.

Corey
And network storage can add another delay because the request has to cross a network before reaching the remote machine or storage device. Services such as OneDrive or Google Drive may therefore keep frequently used material locally and synchronize it with the cloud. That local copy avoids making a network request every time the user opens the same data.

Hope
The central lesson is that I/O is difficult because devices differ in type, speed, access pattern, direction, and connection method, while the computer still has shared resources such as one processor and buses. The operating system hides much of that complexity behind drivers, controllers, system calls, and layered interfaces. Underneath, it manages interrupts, DMA, buffering, caching, spooling, scheduling, protection, error handling, and device status.

Corey
Exactly. If you remember the overall flow, you have the framework: an application makes a general request, the kernel's I/O subsystem identifies the device, the driver communicates with the controller, and data is transferred using mechanisms suited to that device. Interrupts and DMA keep the CPU from waiting unnecessarily, while buffers, caches, queues, protection, and error policies make the whole system reliable and efficient.

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