How the operating system performs device management (preliminary stage)

Preface: I/O device management is involved in both the operating system and the computer composition principle, but their focus is different, and the explanation is messy. This part often includes different devices and device-related applications in many fields Programs are also relatively vague and abstract to learn. To be honest, the blogger has learned the principles of computer composition and the operating system again. After learning it, he really doesn’t know anything, because it is too scattered. If you mention this knowledge point alone, you will know it, and it has been put in the topic. , different concept nouns, completely confused, so the blogger made up his mind to integrate the knowledge points in his own way, so as to facilitate understanding and memory, and share it with everyone here.

insert image description here

1. I/O device

I will not explain the terminology of this I/O device. There is no specific definition, but everyone should know it.

1. I/O device classification

insert image description here

There is nothing to say about the classification according to the transmission rate, just look at the picture

Classification by units of information exchange:

Block device:

  • Take data block as information exchange unit
  • high transfer rate
  • addressable
  • Usually using DMA control
  • eg: disk

Character device:

  • Characters are the unit of information exchange
  • low transfer rate
  • not addressable
  • Usually interrupt control
  • eg: keyboard, printer

Classified by shared attributes of the device:

Exclusive resources:

  • Only one process is allowed to access for a period of time, which is a critical resource
  • deadlock
  • eg: printer

Shared device:

  • Multiple processes are allowed to access for a period of time, but only one device can access at the same time (concurrent, but not parallel)
  • does not cause deadlock
  • Must be an addressable, randomly accessible device
  • eg: disk

Virtual device:

​ What is it? It virtualizes a physical exclusive device into several logical devices. Strictly speaking, a virtual device is physically an exclusive device, but logically it is a shared device, that is, multiple processes can be allowed within a period of time. access. There are no explicit attributes here, just know what the virtual device is.

2. Device Controller

The I/O interface (device controller, I/O controller) is located between the CPU and the device. It not only needs to communicate with the CPU, but also communicates with the device, and also has the function of controlling the work of the device according to the commands sent by the CPU. , mainly consists of three parts:

insert image description here

  • 1) The interface between the device controller and the CPU . The interface has three types of signal lines: data lines, address lines and control lines. Data lines are usually connected to two types of registers: data registers (store input data sent from the device or output data sent from the CPU) and control/status registers (store control information sent from the CPU or device status information).
  • 2) The interface between the device controller and the device . A device controller can connect to one or more devices, so there are one or more device interfaces in the controller. There are three types of signals, data, control, and status, in each interface.
  • 3) I/O logic . Used to realize the control of the equipment . It interacts with the CPU through a set of control lines to decode the I/O commands received from the CPU. When the CPU starts the device, it sends the start command to the controller, and at the same time sends the address to the controller through the address line. The I/O logic of the controller decodes the address and controls the selected device accordingly.

The error-prone point here is that it is actually the I/O logic that implements the device control function in the device manager .

The main functions of the device controller are:

  • ①Receive and identify commands sent by the CPU, such as the disk controller can receive commands such as read, write, and search;
  • ② Data exchange, including data transmission between the device and the controller, and data transmission between the controller and main memory;
  • ③ Identify and report the status of the device for processing by the CPU;
  • ④ Address identification;
  • ⑤ Data buffering;
  • ⑥ error control.

3. I/O ports

The I/O port refers to the registers in the device controller that can be directly accessed by the CPU, mainly including the following three types of registers.

  • Data registers: Realize data buffering between the CPU and peripherals.
  • Status Register: Obtain the status information of the execution result and the device to let the CPU know whether it is ready.
  • Control Registers: Written by the CPU in order to initiate commands or change device modes.

In the meter group, the I/O port can be understood as a physical connection point, so there is an equation: I/O port + control logic = I/O interface. Therefore, when doing counting questions, you must not confuse I/O ports with I/O interfaces.

In order to realize the communication between the CPU and the I/O port, there are two methods:

  • 1) Independent addressing . Assign an I/O port number to each port, and all I/O ports form an I/O port space, which cannot be accessed by ordinary user programs. Only the operating system uses special I/O instructions to access ports.
  • 2) Unified addressing . Also known as memory-mapped I/O , each port is assigned a unique memory address, and no memory is assigned this address. Usually, the address assigned to the port is close to the top of the address space.

The blogger understands this way. These ports are registers in the memory. We know that the memory itself has a memory address. The unified addressing is still based on the memory address, but each port now assigns you a memory address to indicate Your location; and independent addressing has nothing to do with memory. It numbers the ports by itself, which is to redefine an addressing method specially used to represent ports and form its own address expression method.

2. I/O control method

The operating system is written relatively concisely here, but this part is highlighted in the computer composition principle, and the knowledge points of this part are indeed complicated. I will write a blog dedicated to I/O control methods in two days. Introduce various control methods, here we first list four I/O control methods within the 408 range, but do not write in detail.

  • direct program control
  • interrupt driven
  • DMA mode
  • Channel control method

3. I/O software hierarchy

The key points of the exam in this chapter of the postgraduate entrance examination operating system must be mastered.

1. I/O software

Here I would like to ask you first, what is I/O software? Did you know that many postgraduate entrance examination guidance books here don’t explain it, and bloggers have to check the information to understand it. Didn’t we just learn about I/O devices? Isn’t one of the main functions of the operating system to realize device management? In the principle of computer composition, do we understand it as system-level software called system software, which belongs to the software layer. Well, after understanding this, I now tell you that I/O software is software used to manage and control I/O devices. The software here is the system software layer, so it can be said that I/O software is part of the operating system.

The I/O software involves a wide range of aspects. It is closely related to the hardware at the bottom, and directly interacts with the virtual memory system, file system and users at the top. They all need I/O software to realize I/O operations (if you continue to use If the operating system is subdivided, it is equivalent to the bottom layer of the operating system).

In order to make the complex I/O software have a clear structure, good portability and adaptability, the I/O software with hierarchical structure has been widely used at present. The device management module in the system is divided into several levels, and each level uses the services provided by its lower level to complete some sub-functions in the input/output function, and shields the details of the realization of these functions to provide services to the upper level. In the hierarchical structure of I/O software, as long as the interface between the layers remains unchanged, the modification of the software in a certain layer will not cause the change of the lower layer or the upper layer code, and only the lowest layer involves the specific characteristics of the hardware.

insert image description here

Here you should understand that it is divided into four levels. The bottom hardware is not. The reason why it is drawn is to better express its hierarchical structure in the entire computer composition.

2. User layer I/O software

Realize the interface for interacting with the user, and the user can directly call the library functions related to the IO operation provided in the user layer to operate the device. Generally speaking, most of the I/O software is inside the operating system, but a small part is still in the user layer, including library functions linked with user programs. User-level software must obtain operating system services through a set of system calls.

3. Device independent software (system call handler)

It is used to realize the unified interface between user programs and device drivers, device commands, device protection, device allocation and release, etc., and provide necessary storage space for device management and data transmission.

Device independence, also known as device independence , makes the application independent of the specific physical device used . To achieve device independence, the two concepts of logical device and physical device are introduced. In the application program, use the logical device name to request the use of a certain type of device; and when the system is actually executed, the logical device name must be mapped to the physical device name for use. So this layer can also be called device-independent software.

The benefits of using logical device names are: ① increase the flexibility of device allocation; ② easy to implement I/O redirection, the so-called I/O redirection means that the device used for 1/O operation can be replaced (that is, redirected), without changing the application. In order to achieve device independence, a layer of device independence software must be placed on top of the driver.

In general, the main functions of device independence software can be divided into the following two aspects: ① Execute common operations of all devices, including: allocation and recycling of devices; mapping logical device names to physical device names; protecting devices , Prohibit users from directly accessing the device; buffer management; error control; provide a logical block that is independent of the size of the device, and shield the difference in the size and transmission rate of information exchange units between devices. ②Provide a unified interface to the user layer (or file layer). No matter what kind of equipment, the interfaces they provide to users should be the same. For example, for the read/write operations of various devices, the read/write command is uniformly used in the application program.

4. Device Drivers

Directly related to the hardware, it is responsible for implementing the operating instructions issued by the system to the device and driving the driver for the I/O device. Usually, each type of device configuration-a device driver, which is a communication program between the I/O process and the device controller, often exists in the form of a process. The device driver provides a set of standard interfaces to the upper-layer user program, and the specific differences of the device are encapsulated by the device driver, which is used to receive the abstract IO requirements sent by the upper-layer software, such as read and write commands, which are converted into specific requirements and sent To the device controller, it controls the work of the I/O device; it also transmits the signal sent by the device controller to the upper layer software, thereby hiding the differences between the device controllers for the I/O kernel subsystem.

5. Interrupt handler

It is used to save the CPU environment of the interrupted process, transfer to the corresponding interrupt processing program for processing, and return to the interrupted process after the scene of the interrupted process is restored after processing.

The main tasks of the interrupt processing layer are: switch the process context, test the processing interrupt signal source, read the device status and modify the process status, etc. Since interrupt processing is closely related to hardware, it should be shielded as much as possible for users, so it should be placed at the bottom of the operating system, and the rest of the system should be as little as possible in contact with it.

4. Application program I/O interface

In the interface between the I/O system and the upper layer, it is further divided into several interfaces according to the different types of equipment.

(1) Character device interface

Character devices refer to devices whose data access and transmission are based on characters, such as keyboards and printers. The basic feature is that the transfer rate is low, it is not addressable, and it usually uses an interrupt-driven method for input/output. get and put operations. Since the character device is not addressable, it can only be accessed sequentially. Usually, a character buffer is established for the character device. The user program obtains characters from the buffer through the get operation, and outputs the characters to the buffer through the put operation. in-control directives. There are many types of character devices with great differences, so a general in-control command is provided in the interface to deal with them (contains many parameters, each parameter represents a specific function related to a specific device). Character devices are all exclusive devices, so the interface also needs to provide open and close operations to achieve mutual exclusion and sharing.

(2) Block device interface

A block device refers to a device for data access and transmission in units of data blocks, and a typical block device is a disk. The basic feature is that the transmission rate is high and addressable. The I/O of disk devices often uses DMA. The two-dimensional structure of the disk is hidden. In a two-dimensional structure, the address of each sector needs to be represented by a track number and a sector number. The block device interface numbers all sectors of the disk sequentially from 0 to n-1, thus turning the two-dimensional structure into a linear sequence. Map abstract commands to low-level operations. The block device interface supports abstract commands such as opening, reading, writing, and closing files or devices sent by the upper layer. This interface maps the above commands to specific operations at the lower layer that the device can recognize. The memory-mapped interface accesses disk through byte arrays in memory, and does not provide read/write disk operations. The system call that maps a file into memory returns a virtual memory address containing a copy of the file. Virtual memory is actually paged only when access to the memory image is required. The access of memory-mapped files is as simple as reading and writing memory, which greatly facilitates programmers.

(3) Network device interface

Modern operating systems all provide network-oriented functions, so it is also necessary to provide corresponding network software and network communication interfaces, so that the computer can communicate with other computers on the network or browse the Internet through the network. The network I/O interface provided by many operating systems is a network socket interface. The system call of the socket interface connects the local socket created by the application to the socket created by the remote application, and sends and receives through this connection. data.

(4) Blocking/non-blocking I/O

The I/O interface of the operating system also involves two modes: blocking and non-blocking. Blocking IO means that when a user process calls an IO operation, the process is blocked and needs to wait for the completion of the I/O operation before the process is woken up to continue execution. Non-blocking I/O means that when a user process calls an I/O operation, the process is not blocked, and the I/O call returns an error return value. Usually, the process needs to query whether the I/O operation is completed by polling.
The I/O interface provided by most operating systems uses blocking I/O .

5. Exercises

1. Among the following descriptions about device attributes, the correct one is ( B ).
A. The basic feature of a character device is addressable to a byte, that is, the source address of the input or the target address of the output can be specified. B. The
shared device must be addressable and randomly accessible.
C. The shared device refers to the same – A device that allows multiple processes to access it at the same time
D. It may cause process deadlock when allocating shared devices and exclusive devices

2. When a local user logs in to the system through the keyboard, the program to first obtain the input information of the keyboard is ( A ).
A. Command interpreter
B. Interrupt handler
C. System call service program
D. User login program

Tip: The key point of this question is the keyboard, which is a typical device controlled by interrupts.

3. After the user program sends a disk I/O request, the system's processing flow is: user program→system call handler-→device driver→interrupt handler. Among them, the program for calculating the cylinder number, head number, and sector number of the disk where the data is located is ( C ).
A. User program
B. System call handler
C. Device driver
D. Interrupt handler

Tip: Different disks have different cylinder numbers, head numbers, and sector numbers. The specific calculation process should be completed by the device driver provided by the merchant.

6. Summary

This article is just the basis of device management, and it doesn’t even write about how to allocate devices, so let’s continue below.

Guess you like

Origin blog.csdn.net/weixin_51496226/article/details/131513248