Operating System 简明教程

Operating System - I/O Hardware

操作系统的一项重要任务是管理各种 I/O 设备,包括鼠标、键盘、触摸板、磁盘驱动器、显示适配器、USB 设备、位图屏幕、LED、模数转换器、开/关开关、网络连接、音频 I/O、打印机等。

One of the important jobs of an Operating System is to manage various I/O devices including mouse, keyboards, touch pad, disk drives, display adapters, USB devices, Bit-mapped screen, LED, Analog-to-digital converter, On/off switch, network connections, audio I/O, printers etc.

需要一个 I/O 系统来接收应用程序 I/O 请求并将其发送到物理设备,然后接收设备发回的任何响应并将其发送到应用程序。I/O 设备可分为两类−

An I/O system is required to take an application I/O request and send it to the physical device, then take whatever response comes back from the device and send it to the application. I/O devices can be divided into two categories −

  1. Block devices − A block device is one with which the driver communicates by sending entire blocks of data. For example, Hard disks, USB cameras, Disk-On-Key etc.

  2. Character devices − A character device is one with which the driver communicates by sending and receiving single characters (bytes, octets). For example, serial ports, parallel ports, sounds cards etc

Device Controllers

设备驱动程序是可以插入操作系统以处理特定设备的软件模块。操作系统借助设备驱动程序来处理所有 I/O 设备。

Device drivers are software modules that can be plugged into an OS to handle a particular device. Operating System takes help from device drivers to handle all I/O devices.

设备控制器充当设备和设备驱动程序之间的接口。I/O 单元(键盘、鼠标、打印机等)通常由机械组件和电子组件组成,电子组件称为设备控制器。

The Device Controller works like an interface between a device and a device driver. I/O units (Keyboard, mouse, printer, etc.) typically consist of a mechanical component and an electronic component where electronic component is called the device controller.

对于每个设备,始终有一个设备控制器和一个设备驱动程序与操作系统通信。一个设备控制器可能会处理多个设备。作为接口,其主要任务是将串行比特流转换为字节块,根据需要执行纠错。

There is always a device controller and a device driver for each device to communicate with the Operating Systems. A device controller may be able to handle multiple devices. As an interface its main task is to convert serial bit stream to block of bytes, perform error correction as necessary.

连接到计算机的任何设备都是通过插头和插槽连接的,插槽连接到设备控制器。以下是用于连接 CPU、内存、控制器和 I/O 设备的模型,其中 CPU 和设备控制器都使用公共总线进行通信。

Any device connected to the computer is connected by a plug and socket, and the socket is connected to a device controller. Following is a model for connecting the CPU, memory, controllers, and I/O devices where CPU and device controllers all use a common bus for communication.

device controllers

Synchronous vs asynchronous I/O

  1. Synchronous I/O − In this scheme CPU execution waits while I/O proceeds

  2. Asynchronous I/O − I/O proceeds concurrently with CPU execution

Communication to I/O Devices

CPU 必须有一种方法来与 I/O 设备之间传递信息。有三种方法可用于与 CPU 和设备通信。

The CPU must have a way to pass information to and from an I/O device. There are three approaches available to communicate with the CPU and Device.

  1. Special Instruction I/O

  2. Memory-mapped I/O

  3. Direct memory access (DMA)

Special Instruction I/O

这使用专门用于控制 I/O 设备的 CPU 指令。这些指令通常允许将数据发送到 I/O 设备或从 I/O 设备读取数据。

This uses CPU instructions that are specifically made for controlling I/O devices. These instructions typically allow data to be sent to an I/O device or read from an I/O device.

Memory-mapped I/O

使用存储器映射 I/O 时,同一地址空间由内存和 I/O 设备共享。设备直接连接到某些主内存位置,以便 I/O 设备可以在不经过 CPU 的情况下将数据块传输到/从内存。

When using memory-mapped I/O, the same address space is shared by memory and I/O devices. The device is connected directly to certain main memory locations so that I/O device can transfer block of data to/from memory without going through CPU.

memory mapped io

使用存储器映射的 IO 时,操作系统在内存中分配缓冲区并通知 I/O 设备使用该缓冲区将数据发送到 CPU。I/O 设备与 CPU 异步操作,在完成后中断 CPU。

While using memory mapped IO, OS allocates buffer in memory and informs I/O device to use that buffer to send data to the CPU. I/O device operates asynchronously with CPU, interrupts CPU when finished.

这种方法的优点是,可以访问内存的每条指令都可以用于操作 I/O 设备。存储器映射 IO 用于大多数高速 I/O 设备,如磁盘、通信接口。

The advantage to this method is that every instruction which can access memory can be used to manipulate an I/O device. Memory mapped IO is used for most high-speed I/O devices like disks, communication interfaces.

Direct Memory Access (DMA)

像键盘这样的慢速设备将在传输每个字节后向主 CPU 生成一个中断。如果像磁盘这样的快速设备为每个字节生成中断,则操作系统将花费其大部分时间来处理这些中断。因此,一台典型的计算机使用直接内存访问 (DMA) 硬件来减少此开销。

Slow devices like keyboards will generate an interrupt to the main CPU after each byte is transferred. If a fast device such as a disk generated an interrupt for each byte, the operating system would spend most of its time handling these interrupts. So a typical computer uses direct memory access (DMA) hardware to reduce this overhead.

直接内存访问 (DMA) 表示 CPU 授予 I/O 模块在不参与的情况下读写内存的权限。DMA 模块本身控制主内存和 I/O 设备之间的数据交换。CPU 只参与传输的开始和结束,并且仅在整个块传输完成后才中断。

Direct Memory Access (DMA) means CPU grants I/O module authority to read from or write to memory without involvement. DMA module itself controls exchange of data between main memory and the I/O device. CPU is only involved at the beginning and end of the transfer and interrupted only after entire block has been transferred.

直接内存访问需要一种称为 DMA 控制器 (DMAC) 的特殊硬件,它管理数据传输并仲裁对系统总线的访问。这些控制器使用源指针和目标指针(读取/写入数据的目的地)、用于跟踪传输字节数的计数器以及设置(包括 I/O 和内存类型、中断和 CPU 周期的状态)进行编程。

Direct Memory Access needs a special hardware called DMA controller (DMAC) that manages the data transfers and arbitrates access to the system bus. The controllers are programmed with source and destination pointers (where to read/write the data), counters to track the number of transferred bytes, and settings, which includes I/O and memory types, interrupts and states for the CPU cycles.

dma

操作系统使用 DMA 硬件如下所示 −

The operating system uses the DMA hardware as follows −

Step

Description

1

Device driver is instructed to transfer disk data to a buffer address X.

2

Device driver then instruct disk controller to transfer data to buffer.

3

Disk controller starts DMA transfer.

4

Disk controller sends each byte to DMA controller.

5

DMA controller transfers bytes to buffer, increases the memory address, decreases the counter C until C becomes zero.

6

When C becomes zero, DMA interrupts CPU to signal transfer completion.

Polling vs Interrupts I/O

计算机必须有办法检测到任何类型的输入的到达。有两种可能发生的情况,称为 pollinginterrupts 。这两种技术都可以让处理器处理随时可能发生并且与其当前正在运行的进程无关的事件。

A computer must have a way of detecting the arrival of any type of input. There are two ways that this can happen, known as polling and interrupts. Both of these techniques allow the processor to deal with events that can happen at any time and that are not related to the process it is currently running.

Polling I/O

轮询是I/O设备与处理器通信最简单的方式。定期检查设备的状态的过程,以查看是否需要进行下一次I/O操作,称为轮询。I/O设备只需将信息放入状态寄存器中,处理器就必须来获取该信息。

Polling is the simplest way for an I/O device to communicate with the processor. The process of periodically checking status of the device to see if it is time for the next I/O operation, is called polling. The I/O device simply puts the information in a Status register, and the processor must come and get the information.

大多数情况下,设备不需要关注,当设备需要关注时,它将不得不等到轮询程序下次对其进行询问。这是一种低效的方法,处理器的许多时间都会浪费在不必要的轮询上。

Most of the time, devices will not require attention and when one does it will have to wait until it is next interrogated by the polling program. This is an inefficient method and much of the processors time is wasted on unnecessary polls.

将此方法比作一位老师不断地一个个询问班级里的每位学生,看他们是否需要帮助。显然,更高效的方法是,当学生需要帮助时,由学生告知老师。

Compare this method to a teacher continually asking every student in a class, one after another, if they need help. Obviously the more efficient method would be for a student to inform the teacher whenever they require assistance.

Interrupts I/O

处理I/O的另一种方案是中断驱动的方法。中断是设备发给微处理器的信号,表示需要关注。

An alternative scheme for dealing with I/O is the interrupt-driven method. An interrupt is a signal to the microprocessor from a device that requires attention.

当设备的CPU需要关注时,设备控制器将中断信号放到总线上;CPU接收到中断后,会保存其当前状态,并使用中断向量(处理各种事件的操作系统例程的地址)调用适当的中断处理程序。当处理完中断设备后,CPU将继续其原始任务,就好像从未中断一样。

A device controller puts an interrupt signal on the bus when it needs CPU’s attention when CPU receives an interrupt, It saves its current state and invokes the appropriate interrupt handler using the interrupt vector (addresses of OS routines to handle various events). When the interrupting device has been dealt with, the CPU continues with its original task as if it had never been interrupted.