Operating System 简明教程
Operating System - I/O Softwares
I/O 软件通常按以下层组织−
I/O software is often organized in the following layers −
-
User Level Libraries − This provides simple interface to the user program to perform input and output. For example, stdio is a library provided by C and C++ programming languages.
-
Kernel Level Modules − This provides device driver to interact with the device controller and device independent I/O modules used by the device drivers.
-
Hardware − This layer includes actual hardware and hardware controller which interact with the device drivers and makes hardware alive.
I/O 软件设计的一个关键概念是它应该与设备无关,即可能编写可以在访问任何 I/O 设备的情况下无需提前指定设备的程序。例如,一个将文件作为输入读取的程序应该能够在软盘、硬盘或 CD-ROM 上读取文件,而无需对每个不同的设备修改程序。
A key concept in the design of I/O software is that it should be device independent where it should be possible to write programs that can access any I/O device without having to specify the device in advance. For example, a program that reads a file as input should be able to read a file on a floppy disk, on a hard disk, or on a CD-ROM, without having to modify the program for each different device.
Device Drivers
设备驱动程序是软件模块,可以插入操作系统以处理特定设备。操作系统从设备驱动程序获取帮助以处理所有 I/O 设备。设备驱动程序封装了与设备相关的代码,并实现了一个标准界面,这种方式下该代码包含特定于设备的寄存器读/写。设备驱动程序通常由设备制造商编写,并与设备一起在 CD-ROM 上交付。
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. Device drivers encapsulate device-dependent code and implement a standard interface in such a way that code contains device-specific register reads/writes. Device driver, is generally written by the device’s manufacturer and delivered along with the device on a CD-ROM.
设备驱动程序执行以下工作 −
A device driver performs the following jobs −
-
To accept request from the device independent software above to it.
-
Interact with the device controller to take and give I/O and perform required error handling
-
Making sure that the request is executed successfully
设备驱动程序处理请求的方式如下:假设有一个请求来读取块 N。如果驱动程序在请求到达时处于空闲状态,它将立即开始执行请求。否则,如果驱动程序已经忙于处理其他请求,它会将新请求放在待定请求队列中。
How a device driver handles a request is as follows: Suppose a request comes to read a block N. If the driver is idle at the time a request arrives, it starts carrying out the request immediately. Otherwise, if the driver is already busy with some other request, it places the new request in the queue of pending requests.
Interrupt handlers
中断处理程序也被称为中断服务例程或 ISR,是一段软件,更确切地说,是操作系统中的回调函数,或更确切地说,是设备驱动程序中的回调函数,其执行是由中断的接收触发的。
An interrupt handler, also known as an interrupt service routine or ISR, is a piece of software or more specifically a callback function in an operating system or more specifically in a device driver, whose execution is triggered by the reception of an interrupt.
发生中断时,中断程序会执行它必须执行的所有操作来处理中断,更新数据结构并唤醒等待中断发生的进程。
When the interrupt happens, the interrupt procedure does whatever it has to in order to handle the interrupt, updates data structures and wakes up process that was waiting for an interrupt to happen.
中断机制接受一个地址 ─ 一个从一小组中选择特定中断处理例程/函数的数字。在大多数体系结构中,此地址是一个存储在称为中断向量表的表中的偏移量。此向量包含专用中断处理程序的内存地址。
The interrupt mechanism accepts an address ─ a number that selects a specific interrupt handling routine/function from a small set. In most architectures, this address is an offset stored in a table called the interrupt vector table. This vector contains the memory addresses of specialized interrupt handlers.
Device-Independent I/O Software
与设备无关的软件的基本功能是执行所有设备通用的 I/O 功能,并向用户级软件提供一个统一的接口。虽然很难编写完全与设备无关的软件,但我们可以编写一些在所有设备中通用的模块。以下是设备无关 I/O 软件功能的列表 −
The basic function of the device-independent software is to perform the I/O functions that are common to all devices and to provide a uniform interface to the user-level software. Though it is difficult to write completely device independent software but we can write some modules which are common among all the devices. Following is a list of functions of device-independent I/O Software −
-
Uniform interfacing for device drivers
-
Device naming - Mnemonic names mapped to Major and Minor device numbers
-
Device protection
-
Providing a device-independent block size
-
Buffering because data coming off a device cannot be stored in final destination.
-
Storage allocation on block devices
-
Allocation and releasing dedicated devices
-
Error Reporting
User-Space I/O Software
这些是提供更丰富且简化的接口以访问内核的功能或最终与设备驱动程序交互的库。大多数用户级 I/O 软件都由库过程组成,但有些例外,比如后台处理系统,它是多道程序系统中处理专用 I/O 设备的一种方式。
These are the libraries which provide richer and simplified interface to access the functionality of the kernel or ultimately interactive with the device drivers. Most of the user-level I/O software consists of library procedures with some exception like spooling system which is a way of dealing with dedicated I/O devices in a multiprogramming system.
I/O 库(例如 stdio)位于用户空间中,用于提供与驻留在操作系统中的与设备无关的 I/O SW 的接口。例如,putchar()、getchar()、printf() 和 scanf() 是 C 编程中可用的用户级 I/O 库 stdio 的示例。
I/O Libraries (e.g., stdio) are in user-space to provide an interface to the OS resident device-independent I/O SW. For example putchar(), getchar(), printf() and scanf() are example of user level I/O library stdio available in C programming.
Kernel I/O Subsystem
内核 I/O 子系统负责提供与 I/O 相关的许多服务。以下是一些提供的服务。
Kernel I/O Subsystem is responsible to provide many services related to I/O. Following are some of the services provided.
-
Scheduling − Kernel schedules a set of I/O requests to determine a good order in which to execute them. When an application issues a blocking I/O system call, the request is placed on the queue for that device. The Kernel I/O scheduler rearranges the order of the queue to improve the overall system efficiency and the average response time experienced by the applications.
-
Buffering − Kernel I/O Subsystem maintains a memory area known as buffer that stores data while they are transferred between two devices or between a device with an application operation. Buffering is done to cope with a speed mismatch between the producer and consumer of a data stream or to adapt between devices that have different data transfer sizes.
-
Caching − Kernel maintains cache memory which is region of fast memory that holds copies of data. Access to the cached copy is more efficient than access to the original.
-
Spooling and Device Reservation − A spool is a buffer that holds output for a device, such as a printer, that cannot accept interleaved data streams. The spooling system copies the queued spool files to the printer one at a time. In some operating systems, spooling is managed by a system daemon process. In other operating systems, it is handled by an in kernel thread.
-
Error Handling − An operating system that uses protected memory can guard against many kinds of hardware and application errors.