2024 Postgraduate Entrance Examination 408-Operating System Chapter 5-Input and Output IO Management Study Notes

Article directory

1. Overview of I/O management

image-20230718235445592

Previously, it was all about the inside of the computer host, but the current section is about the management of hardware devices outside the host.


1.1. Concept and classification of I/O devices

1.1.1. What is an I/O device?

" I/O" is "input/output" ( Input/Output).

I/OA device is an external device that can input data to a computer or receive data output from a computer. It is a hardware component in a computer.

Next, let’s take a look at input devices, output devices, and devices that can be both input and output :

image-20230718235729899

  • Note on devices that can be both input and output: For input, you can input the data stored in the mobile hard disk into the computer, or you can output the data in the computer to the mobile hard disk for storage.
    • The input process: reading the data prepared by the device into the computer.
    • The output process: outputs the data prepared in the computer to the output device

UnixThe system abstracts external devices into a special file, and users can operate external devices in the same way as file operations.

  • Write操作: Write data to external device.
  • Read操作: Read data from external device.

1.1.2. Classification of I/O devices: according to usage characteristics

image-20230719000421697

Human-computer interaction peripherals : mouse, keyboard, printer, etc., used for human-computer interaction. (The data transmission speed will be slower, and the data transmission is in bytes or tens of bytes)

image-20230719000244587

Storage devices : mobile hard drives, optical disks, etc., mainly used for data storage. Data transfer speed is fast.

image-20230719000324062

Network communication equipment : modems, routers, etc., used for network communication, with transmission speeds between the above two speeds.

image-20230719000405203


1.1.2. Classification of I/O devices: Classified by transmission rate

image-20230719000514356

Low-speed devices : mice, keyboards, transmission rates of several to several hundred bytes per second.

image-20230719000626608

Medium-speed equipment : such as laser printers, etc., with transmission rates of thousands to tens of thousands of bytes per second.

image-20230719000636331

High-speed devices : such as disks, devices with transfer rates of several kilobytes to gigabytes per second.

image-20230719000642961


1.1.3. Classification of I/O devices: Classified according to the unit of information exchange

image-20230719000708547

Block devices : such as disks, etc., the basic unit of data transmission is "block". The transfer rate is high and it is addressable, that is, any block can be read/written randomly.

image-20230719000820855

Character devices : mouse, keyboard light, the basic unit of data transmission is characters. The transmission rate is slow, not addressable, and interrupt-driven is used for input/output duration.

image-20230719000826344


Review of knowledge points and important test points

image-20230719001012468


1.2. I/O controller

image-20230719103944639


1.2.1. Mechanical components of I/O equipment

image-20230719104054946

Mechanical components of I/O devices : Mainly used to perform specific I/O operations.

  • For example, you can see and touch the buttons of the mouse/keyboard: the LED screen of the monitor (used to display images), the magnetic arm of the mobile hard disk, and the disk surface.

The electronic component of the I/O device is usually a printed circuit board that plugs into an expansion slot on the motherboard.


1.2.2. I/O controller and functions

电子部件: It is a printed circuit board. The CPU cannot directly control the mechanical components of these devices. It needs to indirectly control these mechanical components through electronic components. It serves as the end between the CPU and the mechanical components of the I/O device and is used to realize the CPU's control of the device. control.

This electronic component is also called an I/O controller (also called a device controller) : the CPU controls the I/O controller, which in turn controls the mechanical components of the device.

I/O controller functions :

1. Receive and identify commands issued by the CPU: If the CPU sends a read/write command, there will be a corresponding control register in the I/O controller to store the command and parameters .

2. Report the status of the device to the CPU: There will be a corresponding status register in the I/O controller to record the current status of the I/O device, such as: 1 means idle, 0 means busy.

3. Data exchange: The I/O controller will set up the corresponding data register . When outputting, the data register is used to temporarily store the data sent by the CPU, and then the controller transmits the data to the device. During input, the data register is used to temporarily store the data sent by the device, and then the CPU takes the data from the data register.

4. Address identification: Similar to memory addresses, in order to distinguish each register of the device controller , a specific "address" needs to be set for each register. The I/O controller uses the "address" provided by the CPU to determine which register it is.

  • In order to identify each register, it is also necessary to assign a corresponding address to each register just like addressing memory.

1.2.3. Composition of I/O controller

The I/O controller is mainly composed of three parts : the interface between the CPU and the controller, I/O logic, and the interface between the controller and the device.

  • CPU与控制器的接口: Used to implement communication between the CPU and the controller. The CPU issues commands through the controller; specifies the device to be operated through the address line; takes out (inputs) data or puts (outputs) data through the data lines.
  • I/O逻辑: Responsible for receiving and identifying various commands of the CPU (such as address decoding), and responsible for issuing commands to the device.
  • 控制器与设备的接口i: Used to implement communication between the controller and the device.

image-20230719110258150

The controller will have multiple interfaces with the device, that is, an I/O controller may be responsible for controlling multiple specific I/O devices. In order to distinguish which device is to be operated at this time, it must also be addressed.


1.2.4. Implementation process of I/O controller

image-20230719110320771

Implementation process :

  1. The CPU will first issue specific instructions to the I controller through the control line. At the same time, the CPU will also indicate which device it wants to control on a line such as the address line. If it wants to output data, then the CPU will pass the data The bus outputs the output data to the I/O controller 数据寄存器, and then the I/O logic obtains the data output by the CPU from the data register. Some corresponding parameters of the I/O instructions issued by the CPU will be placed 控制寄存器in it, and then the I/O logic can also read some parameters from the control register.
  2. In order to realize the management of each device, the CPU will read a status of each device from the status register, such as busy, idle, fault, etc. Then the I/O installation logic will write corresponding data into the status register to inform the CPU of the status of each device (what is done by the interface between the CPU and the controller ).
  3. I/O logic can also implement device control. It will issue some control commands to the corresponding device according to the commands and parameters issued by the CPU to let the device perform work.

Small details :

1. An I/O controller may correspond to multiple devices.

2. There may be multiple data registers, control registers, and status registers (for example, each control/status register corresponds to a specific device), and these registers must have corresponding addresses to facilitate CPU operation. There are two methods for address addressing.

  • Method 1: Let these registers occupy part of the memory address, which is called memory-mapped I/O.
  • Method 2: Use I/O dedicated addresses, that is, independent addressing of registers.

1.2.5. Two register addressing methods

Method 1: Memory image I/O

内存映像I/O: The registers and memory addresses in the controller are uniformly addressed.

Advantages : Simplified instructions, the controller can be operated using instructions that operate on memory.

image-20230719112049094


Method 2: Register independent addressing

寄存器独立编址: In register independent addressing, the registers in the controller use separate addresses.

Disadvantages : Special instructions need to be set to operate the controller, not only the address of the register, but also the number of the controller.

image-20230719112611806


Knowledge review and important test points

image-20230719112658837


1.3. I/O control method

image-20230719113341840


1.3.1. Program direct control method

1. The process of completing a read/write operation (taking read operation as an example)

If performing a read command:

image-20230719114410605

  1. The CPU will first send a read command to the I/O controller through the control line, and then the I/O controller will start the corresponding device according to the CPU's requirements, and set the status of the device to the busy state. [Current status of status register: busy]
  2. Since the device speed is much slower than the CPU speed, the device has not completed I/O at this time, and the CPU will continue to poll the status of the controller until the status in the status register is 0, 1 means busy.
  3. After the input device prepares the data, it transmits the data to the controller and reports its current status.
  4. The controller puts the data input by the device into the data register, and then changes the status register status flag to 0. [Current status of status register: ready]
  5. The CPU polling program finds that the device status is ready. At this time, the memory in the data register will be read into the CPU register, and then the CPU register will be put into the memory. (Data register—>CPU register—>Memory)
  6. If you want to continue reading data, the CPU will still issue a read command at this time.

The flow chart described above is shown below:

image-20230719115112386

Why do we need to write to memory after reading into the CPU's data register?

  • With the following read scanf command, the data we enter in the cmd window will eventually be read into a variable in the memory, so it needs to be written to the memory, and it will also be read from the memory first when printf is output. The data is finally output in the cmd window.

image-20230719114752366

2. Frequency of CPU intervention

Very frequently, the CPU needs to intervene before and after the I/O operation is started, and the CPU needs to continuously poll and check while waiting for the I/O to complete.

3. Data transmission unit

Read/write one word at a time .

4. Data flow

Read operation (data input): I/O device—>CPU—>Memory. (CPU refers to the registers of the CPU)

Write operation (data output): Memory—>CPU—>I/O device.

The reading/writing of each word requires the help of the CPU.

5. Main Disadvantages and Main Disadvantages

Advantages: Simple to implement. After the read/write instructions, a series of instruction interfaces that implement loop checks are added. Therefore, it is called "direct program control method".

Disadvantages: The CPU and I/O devices can only work serially. The CPU needs to be polled and checked all the time. It is in a blind waiting state for a long time and the CPU utilization is low.


1.3.2. Interrupt driven mode

1. The process of completing the read/write operation, the key: interruption.

image-20230719140030638

Implementation process:

  1. The interrupt mechanism is introduced. Since the I/O device is very slow, after the CPU issues a read/write command, the process waiting for I/O can be blocked and switched to another process for execution.
  2. When the I/O is completed, the controller will send an interrupt signal to the CPU. After detecting the interrupt signal, the CPU will save the running environment information of the current process and execute the interrupt handler to handle the interrupt.
  3. During the interrupt processing process, the CPU reads a word of data from the I/O controller, transfers it to the CPU register, and then writes it to the main memory. At this time, the CPU resumes the running environment waiting for I/O (or other processes), and then continues execution.

Notice:

1. The CPU checks for interrupts at the end of each instruction cycle.

2. During the interrupt processing process, the running environment of the process needs to be saved and restored. This process requires a certain amount of time. It can be seen that if the frequency of interrupts is too high, system performance will be reduced.

  • Only one word can be read each time an interrupt occurs. If a large amount of data is read, a large number of interrupts will occur.

2. Frequency of CPU intervention

Before each I/O operation starts and after completion, CPU intervention is required.

While waiting for I/O to be completed, the CPU can switch to other processes for execution (at this time, the CPU and I/O can work in parallel).

3. Unit of data transmission

Read/write one word at a time

4. Data flow

Read operation (data input): I/O device—>CPU—>Memory

Write operation (data output): Memory—>CPU—>I/O device

5. Main disadvantages and main advantages

Advantages: Compared with the "direct program control mode", in the "interrupt-driven mode", the I/O controller will actively report that the I/O has been completed through the interrupt signal, and the CPU no longer needs to poll continuously. The CPU and I/O devices work in parallel, and the CPU utilization is significantly improved.

Disadvantages: The transmission of each word between the I/O device and the memory needs to go through the CPU, and frequent interrupt processing will consume more CPU time.

Thoughts: You can see that the read/write process passes through the CPU. Can the transfer step be cancelled? DMA method.


1.3.3, DMA method

Compared with the "interrupt-driven method", the DMA method (Direct Memory Access, direct memory access, mainly used for I/O control of block devices) has the following improvements :

①The unit of data transmission is "block", which is no longer the transmission of one word or one word.

②The flow of data is from the device directly into the memory, or from the memory directly to the device. The CPU is no longer needed as a "delivery brother".

③CPU intervention is only required at the beginning and end of transmitting one or more data blocks.

1. Complete the process of reading/writing operations

image-20230719141218678

1. The CPU will issue an instruction to the I/O module to read or write a block. After that, the CPU can do other things, and the I/O device begins to complete a series of read/write operations.

2. After the I/O devices designated by the CPU have completed the work tasks, the DMA controller will send an interrupt signal to the CPU.

3. At this time, the CPU intervenes to handle the interrupt. When the CPU issues a read or write instruction, it needs to indicate how much data is to be read or written this time, where the data should be stored in the memory, and whether the data is in the external device. Where should it be stored?

4. At this time, the DMA controller will complete the data reading/writing work according to the requirements put forward by the CPU. After the entire block of data is transferred, it will send an interrupt signal to the CPU and require the CPU to intervene in the interrupt.

In order to achieve communication between the controller and the CPU, a series of registers are set up in the DMA controller, and then the CPU can read or write the contents of some of these registers through the system bus.

image-20230719141855500

  • DR(Data Register,数据寄存器): Temporarily store data from the device to the memory, or from the memory to the device.
  • MAR(Memory Address Register、内存地址寄存器): When inputting, MAR indicates where the data should be placed in the memory; when outputting, MAR indicates where the data to be output is placed in the memory.
  • DC(Data Counter,数据计数器): Indicates the number of bytes remaining to be read/written.
  • CR(Commend Register,命令/状态寄存器): Used to store I/O commands sent by the CPU or device status information.

These registers are the primary interface between the host and the controller.

Additional points:

  1. The CPU will indicate at the beginning where the data read this time is stored on the disk and where the data read comes from in the memory. This information is placed in the MAR and indicates how much data will be stored in the DR.
  2. At this time, the DMA controller will read the data from the corresponding location on the disk based on the series of parameters provided by the CPU, and then write it to the memory. This process does not require CPU intervention. Only the DMA controller completes the series specified by the entire CPU. After the task is completed, an interrupt signal will be sent to the CPU, and the CPU will intervene for subsequent processing.

Note: The actual DMA controller does not read a whole block of data each time, but also reads one word at a time. Then each word read will be first stored in the DR data register, and then written from the DR. into memory.

2. Frequency of CPU intervention

CPU intervention is only required when transferring the beginning and end of one or more data blocks.

3. Unit of data transmission

Read/write one or more blocks at a time.

Note: Only multiple consecutive blocks can be read and written each time, and these blocks read into the memory must also be consecutive in the memory.

4. Data flow (no longer needs to go through the CPU)

Read operation (data flow direction): I/O device—>memory

Write operation (data output): Memory—>I/O device

5. Main advantages and disadvantages

Advantages: Data transmission is in "block" units, and the frequency of CPU intervention is reduced. Data transmission no longer needs to pass through the CPU and then be written to the memory, further increasing the data transmission efficiency. Parallelism between CPU and I/O devices has been improved.

Disadvantages: The CPU does not issue an I/O instruction and can only read/write one or more consecutive data blocks.

Features that can be improved: If you want to read/write multiple and discretely stored data blocks, or write data to different memory areas, the CPU must issue multiple I/O instructions and perform multiple interrupt processing to complete. .

  • Propose "channel control method".

1.3.4. Channel control method

通道: A kind of hardware that can be understood as a "weak version of the CPU". A channel can identify and execute a series of channel instructions.

  • Compared with the CPU, the instructions that the channel can execute are very single, and the channel program is placed in the host memory, which means that the channel and the CPU share memory.

1. The process of completing a read/write operation

image-20230719145534567

1. The CPU issues an I/O instruction to the channel, indicating the location of the channel program in the memory, and indicating which I/O device to operate. Then the CPU switches to other processes for execution.

2. The channel executes the channel program in the memory (which specifies how much data to read/write out, where the read/written data should be placed in the memory, etc.).

The channel will follow the instructions of the CPU to find where the channel program to be executed is stored in the memory. A channel can actually be understood as a task list, which is a collection of a series of channel instructions.

  • In essence, it is the same as the program we are familiar with. There will also be storage indicating how much data to read/write out and where the read/write data should be placed in the memory. These are all things that the channel can know during the execution of this program.
  • If this method is adopted, it is equivalent to the CPU only knowing the high-speed channel. If you perform such a task now, the task list has been placed in the memory. The specific task to be done is not directly told by the CPU to the channel, but directly by the channel. Read the program in the memory and execute it step by step. When the channel completes a series of tasks, an interrupt signal will be sent to the CPU.

3. After the channel completes the specified tasks, it will send an interrupt signal to the CPU, and then the CPU will process the interrupt.

2. Frequency of CPU intervention

Extremely low, the channel will execute the corresponding channel program according to the instructions of the CPU. Only after completing the reading/writing of a set of data blocks does it need to send an interrupt signal and request CPU intervention.

3. Unit of data transmission

Read/write one group of data blocks at a time.

4. Data flow (under the control of the channel)

Read operation (data input): I/O device—>memory

Write operation (data output): Memory—>I/O device

5. Main disadvantages and main advantages

Advantages: CPU, channels, and I/O devices can work in parallel, and resource utilization is very high.

Disadvantages: The implementation is complex and requires specialized channel hardware support.


Review of knowledge points and important test points

image-20230719145940379


1.4. I/O software hierarchy

image-20230719161257241

Focus on the functions implemented at the software level and analyze each function in sequence from bottom to top.


1.4.1. User layer software

image-20230719161754803

User layer software : needs to request the operating system to provide services and make system calls downward. This layer will be called the system call processing layer.

  • The windows operating system provides a series of system calls to the outside world. However, because the format of the system calls is strict and troublesome to use, a series of more convenient library function interfaces are encapsulated on the user layer for users to use (Windows API)

1.4.2. Device independence software

①Understand device independence software

Device-independent software : Also known as independent software, functions that have nothing to do with the hardware characteristics of the device are implemented in this layer. Mainly responsible for the specific control of hardware devices, converting a series of commands issued by the upper layer (such as read/write) into a series of operations that a specific device can "understand". Including setting device registers, checking device status, etc.

image-20230719192432379

Responsibilities include : translating system call parameters into device operation commands.


②Function implemented by device independence software

Mainly implemented functions :

1. Provide a unified calling interface to the upper layer (such as read/write system calls).

2. Device protection, the principle is similar to file protection. The device is regarded as a special file. Different users have different access rights to each file, and similarly, the access rights to the device are also different.

3. Error handling: used to handle errors on some devices.

4. Equipment allocation and recycling: Most equipment are critical resources and cannot be allocated to multiple devices. The operating system must manage the allocation and recycling of device resources.

5. Data buffer management: Differences in data exchange unit size and transmission speed between devices can be shielded through buffering technology.

6. Establish a mapping relationship between logical device names and physical device names: select the corresponding driver according to the device type.

  • When the user or user layer issues a system call related to an I/O operation, the logical device name of the I/O device to be operated needs to be specified. For example: when going to school to print, select printer 1 and printer 2. These are the logical device names.
  • Device independence software needs to use the "Logical Unit Table" (LUT, Logical Unit Table) to determine the physical device corresponding to the logical device and find the device driver corresponding to the device.

Many operating systems treat the device as a special file. The file also has a storage path. Each representation records a mapping relationship from the logical device name to the physical device name. Different drivers are required for different types of I/O devices. Program processing.

image-20230719162600077


③Two methods used to manage the logical device table

The operating system manages logical device tables (LUTs) in two ways :

Method 1: Only one LUT is set for the entire system, which means that all users cannot use the same logical device name, so this method is only suitable for single-user operating systems.

Method 2: Set up a LUT for each user. The logical device names used by each user can be repeated, which is suitable for multi-user operating systems. The system will create a user management process for the user when he logs in, and the LUT is stored in the PCB of the user management process.

  • It is the same as the multi-level user directory on the file management side.

1.4.3. Device driver

Thinking: Why do different types of I/O devices require different driver processing?

Reason : Various devices have different shapes, and their respective electronic components (I/O controllers) may also be different.

For example, the design of the electronic components of a Canon printer is as shown below: there are two data registers, and the corresponding control register 1 means busy, and 0 means idle.

image-20230719163117106

HP's is as follows (for example): There is only one data register, and the corresponding control register 0 means busy and 1 means idle.

image-20230719163044862

The internal hardware characteristics of different devices are also different. These characteristics are only known by the manufacturer. Therefore, the manufacturer needs to provide a driver corresponding to the device. The CPU executes the instruction sequence of the driver to complete the work of setting registers, checking device status, etc.

Conclusion : The internal hardware characteristics of various devices are different, so the specific driver corresponding to it must be executed to properly control the device hardware.


Understanding device drivers

Mainly responsible for : specific control of hardware devices, converting a series of commands (such as read, write) issued by the upper layer into a series of operations that a specific device can "understand".

Including setting device registers, checking device status, etc.

image-20230719164634333


1.4.4. Interrupt handler

When the hardware device completes the I/O operation, it will send an interrupt signal as a response, and the system will find the interrupt handler corresponding to the interrupt signal based on the type of the interrupt signal.

Interrupt handler processing flow :

1. Process according to the interrupt handler, and read the device status from the I/O controller or device controller to determine whether the I/O this time ended normally.

  • If it ends normally this time, then the interrupt handler will read a word of data from the data register of the device controller, and then put it into the memory buffer through the CPU.
  • If it does not end normally, it means that an accident occurred in the middle, such as a hardware failure, etc., and the system will handle it accordingly according to the cause of the exception.

2. When the interrupt handler puts the data to be input this time into the memory, it will be handed over to the device driver for further processing. After the device driver has finished processing, it will be handed over to the upper layer of device independence software. Further processing is carried out, and the final layer is networked and returned to the user.

image-20230719165352493

Device drivers and interrupt handlers deal directly with the hardware.


Knowledge review and summary

The user will first issue I/O requests from top to bottom, and then the hardware will perform I/O responses from bottom to top.

image-20230719165615899

image-20230719165634811

It is necessary to have a general impression of the LUT logical device table.


1.5. Input and output application program interface and driver interface

image-20230719170128839


1.5.1. Input and output application program interface

Input/output API

image-20230719173836954

Ask a question : For I/O devices that may have many different reading units, user-level applications cannot use a unified system call interface to complete all types of device I/O, so the device independence software layer needs to Upper-layer applications provide several types of program interfaces.

Because the system interfaces called are different, they are: character device interface block, device interface and network device interface.


①Character device interface

字符型设备接口: get/put system call to read/write a character to the character device.

  • Get reads a character from the specified character device, and put writes a character to the specified character device.

②Block device interface

块设备接口

  • For an entire disk, you can use the seek system call to specify the read and write address. Which address should it be placed on the disk? After placing it at the specified address, you can use read or write to read or write several units of characters behind the position pointed by the read-write pointer.
  • Multiple bytes and multiple characters can be read and written at a time. At the same time, you must also specify the address for reading and writing. This location is the interface of the block device.

Reason for whether to use address parameters for character and block devices: Providing these different types of interfaces to the upper layer is actually defined based on the characteristics of this type of device. Character devices do not have the concept of addresses, so get and put do not need to be provided. The address parameter is based on the concept of address for block devices, so an address parameter needs to be provided.


③Network device interface

Understand network device interfaces

网络设备接口: Also known as "network socket socket interface".

  • socket system call: Create a network socket and specify the network protocol (TCP, UDP).
    • bind system call: Bind the socket to a local port.
    • connect: Link the socket to the remote address.
    • read/write: After the socket initialization is completed, you can use read/write to read and write data to the network socket.
  • Solve the problem: For example, when using WeChat and Alipay, the network card will receive data packets from these two at the same time. Some distinctions need to be made as to which specific application the specific data packet should be sent to.

The actual sending and receiving process of network sockets

​ The host 2P4 process first uses the socket socket to create a network socket (essentially applying for a kernel space, which can receive and send data). At this time, the socket socket will return a descriptor to the process (pointing to a pointer to the socket)

​ Then you need to use bind to bind this socket to a local port, such as port 6666.

image-20230719182506776

At this time, the host 1P1 process also creates a socket object through the socket system call, and also uses bind to specify the port of the local machine.

At this time, the processes of the two hosts have their own sockets and have determined their own data sending and receiving port numbers. At this point, you can use the connect system call to complete the local socket connection to another machine.

At this time, the P1 process of host 1 initiates a connet system call, indicating the other party's IP address and corresponding port number:

image-20230719172353733

​ If you want to send a data packet at this time, then P1 first prepares the data packet in its own user area, and then uses the write system call to write the yellow data to the socket pointed to by fd, independent device After the software layer receives the write system call, it will copy the data prepared by the user process to the kernel area, which is the buffer corresponding to the socket.

image-20230719172649637

​ Next, this layer will call the driver (network card) of the network controller to process this data. Then the driver will be responsible for outputting the prepared data to the network device. At this time, the network controller can The packet is sent to the network.

	此时主机2中的网络控制器会收到发送来的数据包,接着网路控制器会向主机发送一个中断信号,此时中断信号处理程序会调用网络控制器,使用驱动程序来把网络控制器里边收到的这些数据搬到内核的缓冲区里。由于网络包指明了目标的端口6666,此时会将数据复制到6666端口的这片内核缓冲区当中。

​ If the P3 process of Host 2 wants to accept a network data packet, then you only need to use the read system call to read out a data packet from the socket object pointed to by fd. Then the result of the system call is that the device independence software will copy the data from the buffer to the user area of ​​the user process. At this time, P3 can use the block data.

image-20230719173521168

​ Sockets are point-to-point. When this connection is established, each socket will be bound to a local port. At this time, we can connect to any place in the world through the host's IP address + port. Socket object.

How to distinguish the buffers that different packets should be put into?

  • Put it into the appropriate socket object according to the port information specified in the data packet.

Concept: What is blocking IO and non-blocking IO

阻塞I/O: The application issues an I/O system call, and the process needs to wait in a blocking state.

  • Example: Character device interface, read a character get from the keyboard.

非阻塞I/O: The application issues an I/O system call, and the system call can return quickly, and the process does not need to block and wait.

  • Example: Block device interface, write data to disk.
  • Note: When an I/O system call is issued, even if the disk is busy, the user process will not be allowed to wait, because the data prepared by the process is in the user area, and the operating system kernel has a kernel area. When write is called, this data is written to When accessing the disk, even if the disk is busy, the device independence software will quickly respond to this system call request, that is, copy this data to the kernel first. As long as this piece of data is copied to the kernel, then the kernel slowly writes the data back to the disk, and that's it. Then for the user process, as long as the data copy process is completed, you can continue to execute.
  • Features: This type of system call does not require blocking and can return quickly.

1.5.2. Device driver interface

image-20230719174752228

Since similar products are developed by different companies, the driver interfaces are not of the same specification. How to solve this problem?

  • Adopt a unified standard device driver interface.

​ Unified standard device driver interface: The operating system stipulates the interface standard for device drivers, and each manufacturer must develop device drivers in accordance with the requirements. If a driver wants to be used on my operating system, it must follow my specifications. For example, the name and parameters of the read function must be consistent with the specifications I gave!

image-20230719174727040

​ Different operating systems have different standards for device driver interfaces, so device manufacturers must develop corresponding device drivers based on the interface requirements of the operating system so that the device can be used.

image-20230719174857329


2. Device independence software

2.1. IO core subsystem

I/O system (I/O subsystem): It is the kernel part of the operating system and mainly includes device independence software, device drivers, and interrupt handlers.

image-20230720142019489


2.1.1. Function introduction of user layer software and device independence software layer

image-20230720145644430

用户层软件: Spooling technology (SPOOLing technology).

  • Spooling technology requires device independence, so it is generally implemented at the user level.

设备独立性软件: I/O scheduling, device protection , device allocation and recycling, buffer management (i.e. buffering and caching).

  • I/O scheduling: I/O scheduling is very similar to processor scheduling. A certain algorithm is used to determine a good order to process each I/O request. For example, disk scheduling is implemented using a certain algorithm.
    • Disk scheduling: first come first served algorithm, shortest seek first algorithm, SCAN algorithm, C-SCAN algorithm, LOCK algorithm, C-LOCK algorithm. When multiple disk I/O requests arrive, a certain scheduling algorithm is used to determine the order in which the I/O requests are satisfied.
    • Devices such as printers can also use the first-come, first-served algorithm, priority algorithm, and short-job limited algorithm to determine the I/O scheduling sequence.
  • Device protection: Devices will be regarded as special files in the Unix system, and the system will create a corresponding fcb and file control block for each device. Different users have different access rights to different files. The system will determine whether the user has the corresponding access rights based on the information recorded in the FCB.

Knowledge overview

Spooling technology is actually implemented in user-level software. In the syllabus, they are classified as functions to be implemented by the I/O core subsystem.

image-20230720145441863


2.2. Spooling technology (SPOOLING technology, user layer software layer)

image-20230720150025992

Spooling technology is actually implemented using software.


2.2.1. What is offline technology? (Evolution in various stages)

Manual operation stage : The host obtains data directly from the I/O device. Since the device is slow and the host is fast, the contradiction between human and machine speed is obvious, and the host wastes a lot of time waiting for the device. [Input and output both use paper tape machines, which are extremely slow]

image-20230720150321820

The batch phase introduces offline input/output technology : using tape to complete

First, try a paper tape machine to input your own program data into the tape. The input process is implemented using a peripheral control machine. After that, the CPU can directly read the desired input data from the tape, which greatly alleviates this speed problem. contradiction. Similarly, the host can then output the data to the tape, and then the peripheral controller can output the data to the slow tape drive in turn.

  • Peripheral controller: Under the control of the peripheral controller, the data from the slow input device is first input to the faster tape, and then the host can read the data from the faster tape, resulting in a link speed conflict.

Advantages : Since the tape speed is much faster than the paper tape machine, the CPU can save a lot of time waiting for the output to be completed when inputting. In this case, the CPU utilization rate is greatly improved.

image-20230720151159507

Why is it called "offline" technology?

  • Input/output operations without the control of the host are implemented by a peripheral control machine . This input and output process does not require the intervention and control of the host or CPU. At this time, the CPU can have more time to process. Other computing tasks.
  • After the introduction of offline technology, the speed conflict between the CPU and slow I/O devices is alleviated. On the other hand, even if the CPU is busy, data can be input to the disk in advance; even if the slow output device is busy, data can be input to the disk in advance. Output data to disk.

2.2.2. Understand spooling technology and principles

Based on the idea of ​​​​offline technology, the so-called spooling technology was invented :

" 假脱机技术", also known as " SPOOLing技术", uses software to simulate offline technology. The SPOOLing system consists of the following:

image-20230720151508741


2.2.3. Implementation principle of spooling technology

①Input well, output well

假脱机技术: input well, output well

The system will open two storage areas on the disk : input well and output well.

  • 输入井: A tape used to simulate offline input, used to store data input from I/O devices.
  • 输出井: A tape that simulates offline output and is used to contain data output by the user process.

image-20230720151632095

The input wells and output wells corresponding to the batch processing stage are actually the corresponding tapes :

image-20230720151738832


②Input process, output process

假脱机技术: Input process, output process

Among them, the peripheral control machine is also a very important component for realizing offline technology . The peripheral control machine is implemented by an input process and an output process.

  • 输入进程: The input process to be implemented is to simulate offline.
  • 输出进程: It is the peripheral control machine when simulating offline output.

This input process and output process must be executed concurrently with the user process to complete. This simulates the process of offline input and offline output. Therefore, it must be SPOOLing技术supported by multi-program technology. The system will establish an "input process" and output process.

image-20230720151951561


③Input/output buffer

假脱机技术: input/output buffer

In fact, for the input process and the output process, they are the peripheral control machines in the batch processing stage :

image-20230720152048180

  • The input process simulates the function of the peripheral controller. When data needs to be input from the device to the computer, the input process simulates the peripheral controller in software and puts the input data into the input well of the disk.
  • When the output process simulates offline output, the principle is similar, outputting data from the output well of the disk.

In fact, two buffers will be opened in the memory, one is the input buffer and the other is the output buffer. Their main function is to simulate offline input and offline output, and serve as a data transfer station.

  • 输入缓冲区: Under the control of the input process, the "input buffer" is used to temporarily store the data input by the input device, and then transfer it to the input well.
  • 输出缓冲区: Under the control of the output process, the "output buffer" is used to temporarily store the data sent from the output well, and then transfer it to the output device.

image-20230720153256490

Note: The input buffer and output buffer are buffers in memory.


2.2.4. Specific spooling applications: analysis of the principle of shared printers

First, let’s understand exclusive devices and shared devices :

  • 独占式设备: A device that only allows each process to be used serially, and can only satisfy the request of one process within a period of time.
  • 共享设备: A device that allows multiple processes to be used "simultaneously" (simultaneously on a macro scale, possibly alternately on a micro scale). Can satisfy usage requests of multiple processes at the same time.

Devices such as printers are exclusive devices, but we can use SPOOLing技术them to transform them into "shared devices".

Exclusive example : If process 1 is using the printer, when process 2 requests to use the printer, it will inevitably block and wait.

So how to implement the printer as a shared printer ?

  • The sharing here uses spooling technology.

When multiple user processes make requests for output printing, the system will grant their requests, but the printer will not actually be assigned to them. Instead, the spool management process will do two things for each process.

1. Apply for a free buffer for the process in the disk output well (this buffer is on the disk) and send the data to be printed into it.

image-20230720154416653

  • You can see that two more user processes have applied for space at this time, indicating that two print request tasks have come at this time.

2. Apply for a blank print request table for the user process, fill in the user's print request in the table (actually used to indicate the user's printer data storage location information), and then hang the table to the spool file on the queue.

image-20230720154457030

  • Build a print request table based on print requests and hang it on the spool file queue.

Once the printer is idle, the output process will take out a print request table from the head of the file queue, and transfer the data to be printed from the output well to the output buffer according to the requirements in the table, and then output it to the printer for printing. In this way to process all print tasks in sequence.

  • According to the print request table, it can be determined where the corresponding print information is located in the output well, and the corresponding data can be retrieved.

Final effect : Although there is a printer behind the system, when each process makes a print request, the system will allocate a storage area for it in the output well (equivalent to allocating a logical device), so that each user process can feel You are monopolizing a printer to share the printer.


Knowledge overview

image-20230720154928419


2.3. Allocation and recycling of equipment (device independence software layer implementation function 1)

image-20230720160211409


2.3.1 Factors to consider when allocating equipment

image-20230720160908618

There are three inherent attributes of a device : exclusive device, shared device, and virtual device.

  • 独占设备: A time period can only be allocated to one process (such as a printer).
  • 共享设备: Can be allocated to multiple processes at the same time (such as disk). In fact, each process often shares the use of equipment at the same time on a macro scale, and executes alternately on a micro scale.
  • 虚拟设备: Use SPOOLing technology to transform exclusive devices into virtual shared devices, which can be allocated to multiple processes at the same time. (Such as the shared printer implemented by SPOOLing technology)

Equipment allocation algorithm : first come, first served, higher priority first, short tasks first.

Security in device allocation : Considering the security of process operation, there are two ways to allocate equipment.

Method 1: Safe allocation method: After the process issues an I/O request, the system allocates an I/O device to it. The process can continue to execute, and can also issue new I/O requests later. Only a certain I/O request cannot be obtained. The process will be blocked only when it is satisfied.

  • Brief description: Each process can only use one device within a period of time.
  • Advantages: Destroyed: request and hold conditions, no deadlock.
  • Disadvantages: For a process, the CPU and I/O devices can only work serially.

Method 2: Unsafe allocation method: After a process issues an I/O request, the system allocates an I/O device to it. The process can continue to execute, and can also issue new I/O requests later. The process will be blocked only when an I/O request cannot be satisfied.

  • Brief description: A process can use multiple devices at the same time.
  • Advantages: The computing tasks and I/O tasks of the process can be processed in parallel, allowing the process to advance quickly.
  • Disadvantages: Deadlocks may occur (deadlock avoidance, deadlock detection and release).

2.3.2. Static allocation and dynamic allocation

静态分配: All required resources are allocated to the process before running (it cannot run without all allocation), and the resources are returned after running. [The "request and hold" condition is destroyed, and deadlock will not occur]

动态分配: The process dynamically applies for device resources while the process is running, and does not obtain the resources at the beginning.


2.3.3. Data structure in equipment allocation management

Understand the relationship between devices, controllers, and channels (tree relationship)

The relationship between devices, controllers, and channels (tree relationship) :

image-20230720162954375

One channel can control multiple device controllers, and each device controller can control multiple devices.

  • There may be multiple channels in a system. If you want to control a device, you definitely need the controller of the device. If you want to control a controller, you need to find the channel to which the controller belongs.

The data structure needs to express this subordinate relationship : device control table DCT, controller control table (COCT), channel control table (CHCT), system device table (SDT).


The first data structure: Device Control Table (DCT)

设备控制表(DCT)The properties included are: device type, device identifier, device status, pointer to the controller table, number of repetitions or time, and head pointer of the device queue.

The properties are detailed as follows :

image-20230720163820166

设备类型: Such as printer/scanner/keyboard.

设备标识符: That is, the physical device name. The physical device name of each device in the system is unique.

设备状态:Busy/Idle/Fault…

指向控制器表的指针: Each device is controlled by a controller, and this pointer can find information about the corresponding controller.

重复执行次数时间: Mainly used to record the number of failures. Only when the I/O operation is repeated multiple times and still fails, the I/O is considered to have failed.

  • Regarding the paper jam phenomenon that sometimes occurs when using a printer, it does not mean that the operation fails once and it is considered a real failure. The system will repeat the operation multiple times.

设备队列的队首指针: Points to the process queue waiting for the device (a queue composed of process PCBs).

  • If the process is waiting for the allocation of an I/O device at this time, and the I/O device cannot be allocated to it at this time, then the process will hang on to the waiting point pointed to by the corresponding I/O device control table. At the end of the queue, this queue pointer actually points to the corresponding blocking queue.

Note : It was mentioned in the "Process Management" chapter that "the system will hang the process PCB in different blocking queues according to different blocking reasons."


The second data structure: controller control table (COCT)

控制器控制表(COCT): Each device controller will correspond to a COCT. The operating system operates and manages the controller based on COCT information.

image-20230720164428719

  • 控制器标识符: The unique ID of each controller.
  • 控制器状态:Busy/Idle/Fault…
  • 指向通道表的指针: Each controller is controlled by a channel, and this pointer can find the information of the corresponding channel.
  • 控制器队列的队首指针、控制器队列的队尾指针: Points to the process queue waiting for the controller (a queue composed of process PCBs).

The third data structure: Channel Control Table (CHCT)

通道控制表(CHCT): Each channel corresponds to a CHCT. The operating system operates and manages the channel based on the CHCT information.

image-20230720164833055

  • Channel identifier: The unique ID of each channel.
  • Channel status: busy/idle/fault…
  • The first address of the controller table connected to the channel: All controller-related information (COCT) managed by the channel can be found through this pointer.
  • The head pointer of the channel queue and the tail pointer of the channel queue: point to the process queue (queue composed of process PCB) that is waiting for the channel.

If the channel is temporarily unable to serve a process, the process still needs to wait for the service of this channel. The last two fields point to the queue of processes waiting for this channel.


The fourth data structure: System Device Table (SDT)

系统设备表(SDT): Records the status of all devices in the system, and each device corresponds to an entry.

image-20230720165255255

This table contains multiple devices, and each device record contains the device status. Each field has the following information:

  • Device type: e.g. printer/scanner/keyboard.
  • Device Identifier: A unique physical device name.
  • DCT (Device Control Table): The address of the information data of this device.
  • Driver entry: The entry address of the device program.

2.3.4. Steps of equipment allocation

A concise description is as follows :

image-20230720183825436

The detailed steps are as follows :

1. Find the matching entry from the [System Device Table] based on the device name.

image-20230720170717485

2. After finding the [Device Controller] through the [DCT (Device Control Table) address in the system device table], determine whether it is idle based on the device status. If the device is idle, the device can be assigned to the process. If it is busy at this time, then the process needs to be placed in the blocking waiting queue of the device queue. The process will not be reawakened until the device is empty and the device is assigned to the process.

  • After waking up, in addition to assigning this device, the controller corresponding to this device also needs to be assigned to this process.

image-20230720170733915

3. Find the [controller table] according to the [pointer to the controller table in the device control table DCT]. At this time, it will check whether the controller is idle. If it is idle, the controller will be assigned to the process. If it is busy, the process needs to be put away. to the controller's blocking queue.

  • After assigning the controller, you need to assign the corresponding channel to the process.

image-20230720170756695

4. Find the channel control table CHCT according to the [channel pointer in the controller table]. If the channel is idle, assign it to the process. If the channel is busy at this time, the process needs to be placed in the blocking queue of the channel.

Note: Only when the device, controller, and channel are all successful, the device allocation will be considered successful. After that, the I/O device can be started for data transmission.

image-20230720170809337


2.3.5. Improvement of equipment allocation steps

2.3.5.2. Disadvantages of equipment allocation

Disadvantages of equipment allocation ;

① Users must use the "physical device name" when programming. The underlying details are not transparent to the user and are inconvenient for programming.

②If the physical device is replaced, the program will not run.

③If the physical device requested by the process is currently busy, and there are other idle devices of the same type in the system, the process also needs to block and wait.

  • For example, three printers: If one of them, A, is occupied and another user wants to request it, it will be blocked. Even other types of computers will still not be able to use it.

2.3.5.2. Understand the logical device table

逻辑设备表(LUT): Establishes the mapping relationship between logical device names and physical device names.

image-20230720182525716

When a user process uses a device name for the first time, it uses the logical device name to send a request to the operating system. The operating system searches the system device table based on the device type (logical device name) specified by the user process, finds an idle device, and assigns it to the process, and assigns it to the process in the LUT. Add the corresponding table entry.

  • If the user process later requests to use the device through the same logical device name, the operating system can know which physical device the user process actually wants to use through the LUT table , and also knows the driver entry address of the device .

How to set up the logical device table :

  • There is only one LUT table in the entire system: the logical device names used by each user are not allowed to be repeated, and it is suitable for single-user operating systems.
  • One LUT table for each user: the logical device names of different users can be repeated, suitable for multi-user operating systems.

2.3.5.3. Improvement methods

Improvement method : Establish a mapping mechanism between logical devices and physical device names. Users only need to provide logical device names when programming.

  • Actual simplification: just use the physical device name at the beginning to use the logical device name to find it. After matching an entry of the SDT, add it to the logical device table LUT and add a new entry. After that, it will be exactly the same!

image-20230720182412913


Knowledge review and key summary

image-20230720183405232


2.4. Buffer management (device independence software layer implementation function 2)

image-20230720185509760


2.4.1. What is a buffer zone?

缓冲区: It is a storage area that can be composed of special hardware registers or can use memory as a buffer.

  • Buffer using hardware: Hardware can be used as a buffer. The capacity is relatively small and is generally only used in situations where speed requirements are very high (such as the association register (fast table) used in memory management uses a hardware-implemented buffer) ,Due to the high frequency of access to the page table, a fast ,associative register is used to store a copy of the page table ,entry.

For device independence software buffers refer to memory as buffers .

Under normal circumstances, more memory is used as a buffer. This section mainly uses memory as a buffer to expand.


2.4.2. What is the function of buffer (4 functions)

image-20230720185933471

Function 1: Ease the speed mismatch between CPUI and I/O devices.

CPU—>Buffer: The CPU can quickly put the output data into the buffer and then do other things.

I/O device—>buffer: Slow I/O devices can slowly take data from the buffer.

In this way, the speed mismatch between them can be resolved.

Function 2: Reduce the interrupt frequency to the CPU and relax the limit on the CPU interrupt response time.

image-20230720190147056

If the buffer is not used, then the speed of the CPU and the I/O device itself does not match. Furthermore, every time a certain unit of data is output, the I/O device needs to send an interrupt signal to the CPU, and it takes a certain amount of time for the interrupt signal. Overhead, so frequent processing of these interrupts by the CPU will reduce system performance.

  • If a buffer is used, the CPU only needs to intervene to handle the interrupt when all the data in the buffer is removed or the input data fills the buffer. This method can reduce the frequency of CPU interrupts and relax the response time to CPU interrupts. limits.

Here is an illustration without a buffer:

image-20230720190504307

Function 3: Solve the problem of data granularity mismatch.

  • For example: the output process can generate a piece of data each time, but the I/O device can only output one character at a time.
  • No buffer situation: If the output process running on the CPU can output a whole block of data at a time, but the I/O device can only output 1 character at a time, then if the buffer strategy is not adopted, the output process can only output one character. , one character to the I/O device to transmit data.
  • If there is a buffer: If there is a buffer, the output process can directly put a whole block of data into the buffer, and let the I/O device take out one character or one character from the buffer to process the data. Granularity mismatch problem.

Function 4: Provide parallelism between CPU and I/O devices.


2.4.3. Single buffering

2.4.3.1. Understanding single buffer

Suppose a user process requests a block device to read several blocks of data .

If a single buffering strategy is adopted, the operating system will allocate a buffer for it in the main memory [If the title does not specify otherwise, the size of a buffer is one block]

Note :

  1. When the buffer data is not empty, data cannot be rushed into the buffer, but data can only be transferred out from the buffer.
  2. When the buffer is empty, data can be rushed into the buffer, but the buffer must be filled before the data can be transferred out of the buffer.

2.4.3.2. Single buffer transmission process

Describe the process of a buffer :

① First, the system allocates a buffer of a certain size in the main memory for the user process.

image-20230720192305733

②At this time, the block device will generate a block of data and input it into this buffer. The time spent at this time is t.

image-20230720192538056

③At this time, the data needs to be transferred to the workspace of the user process (read into the memory) before it can be processed by the user process. By default, the workspace of the user process can accommodate this piece of data, which will take m to complete. Transfer the data in the buffer to the workspace of the user process.

image-20230720192809476

④The user process can then process this piece of data. It is assumed here that the calculation and processing time is c. When the processing is completed, the workspace of the user process can be vacated.

image-20230720192924748


2.4.3.3. Average time to process a piece of data under different situations (two situations)

Next, based on the time assumed above, calculate how long it takes on average to process each piece of data?

Case 1: Assume that the initial state is that the work area is full, the buffer is empty, T>C

image-20230720193059623

Since C time is short, we will execute C first. Since the initial state work area is full, we need to spend C time for processing. At the same time, the block device can also perform input operations in parallel for a duration of T.

Note : Due to the rules of the buffer, the transfer operation can only be performed after the buffer is filled , so you must wait for the end of T time before the transfer operation can be performed for M time.

  • The bolded part is C and T in the timing diagram below. When the filling area is full, T time must be executed before the M operation can be executed.

image-20230720193943581

Then the subsequent execution operations are basically the same, each execution takes T+M time, then the average time is T+M:

image-20230720194132152

Case 2: Assume that the initial state is that the work area is full, the buffer is empty, and T<C

At this time, we can first perform the input operation, which takes T time, and the workspace corresponding to the CPU's processing of the user process also takes C time. The two can be completed in parallel.

Since T time is short, the input operation will be completed first. At this time, the buffer is full. However, please note that the transfer operation cannot be performed at this time because the data has not been processed in the workspace of the user process. At this time, you need to wait. After all processing C is completed, the transmission operation can be performed. The corresponding timing diagram is as shown below:

image-20230720194531450

Then the subsequent operations are the same, so the average time is:C+M

image-20230720194615930


2.4.4. Double buffer

2.4.4.1. Understanding double buffers

If two machines are configured with double buffers :

If a double buffer structure is adopted, then the two hosts can simultaneously rush into their own sending buffers and insert the data they want to send out, and then they can simultaneously insert data into the other party's receiving buffer. At this time, It can realize the function of two-way transmission at the same time.

image-20230720202313918


2.4.4.2. Average time to process a piece of data under different situations (two situations)

Double buffering question: How long does it take the computer to process each piece of data on average?

Case 1: Assume that the initial state is that the work area is empty, one of the buffers is full, and the other buffer is empty. Assume that T>C+M

image-20230720195954992

  • Initially, buffer 1 is full, buffer 2 is empty, and the user process workspace is empty.

We can execute 缓冲区1->工作区2the transfer (M), and at the same time it can be completed in parallel 块设备—>内存的输入(T). In this process, if the transfer operation is completed, the processing © operation can be started immediately, so the final timing diagram is as shown below:

image-20230720200305726

When a data transmission is completed, our buffer will be filled with a piece of data only after T input is completed, so we need to complete T before we can execute the transmission (M). The following process is the same as the first The process is similar to:

image-20230720200453792

Then the average time it takes to process a piece of data at this time = Ttime.

Case 2: Assume that the initial state is that the work area is empty, one of the buffers is full, and the other buffer is empty. Assume that T<C+M

Similarly, 缓冲区1—>工作区and 块设备—>缓冲区2can be executed in parallel, and the processing operation can actually be executed after the transmission (M) is completed. Then the first round of operations is as shown in the figure below:

image-20230720200750598

As you can see in the figure, in the processing © operation stage, we cannot transfer the data in buffer 1 or 2 to the work area, because the current work area has not been processed, so we can only wait for the processing © to end before we can continue. Complete the transfer (M) operation.

At this time, after the execution of T is completed, you can continue to complete the writing of data in the device -> buffer 1. The transmission operation cannot be performed while the processing © is not completed. At this time, you can find that the second and third This time they are no longer regular, as shown in the figure below:

image-20230720201404123

However, according to the diagram, you can still clearly see that the time spent in each round is M+C, this is the average time.

Conclusion : Using the double buffering strategy, the average time T>m+ctaken T<C+Mto process a data block is Max(T,C+M).

2.4.4.3. Differences in communication using single/double buffering

Single/double buffering is not only used in data transmission between the host and the device, but can also be used when two hosts communicate. This buffering strategy can also be used for data reception and transmission.

If it is a single buffer : Only when the buffer of machine A is full, data can be sent to the buffer of machine B. Then the buffer of machine B can only be taken away when it is full, and data can be sent to the buffer of machine A only when the buffer of machine B is empty.

image-20230720202122875

Obviously: if two machines communicating with each other only set up a single buffer, only one-way transmission of data can be achieved at any time [the main reason for this is due to the characteristics of the buffer].


2.4.5. Circular buffer

For single/double buffers we need to calculate the average time to process a piece of data, and for circular buffers we only need to understand the general principle.

循环缓冲区: Link multiple buffers of equal size into a circular queue.

The figure below is a circular queue, where orange indicates a buffer that is full of data and green indicates an empty buffer:

image-20230720202516636

  • The in pointer points to an empty buffer. When the empty buffer is filled with data, it will move toward the arrow in the figure and point to a new empty buffer.
  • The out pointer points to a full buffer. When the buffer is filled with data, it will also move in the direction of the arrow and point to a full buffer.

2.4.6. Buffer pool

2.4.6.1. Understanding the buffer pool

The buffer pool consists of a series of buffers.

缓冲池: Composed of buffers shared in the system.

The buffers in the buffer pool can be divided into empty buffer queues, buffer queues filled with input data (input queue), and buffer queues filled with output data (output queue) according to their usage status .

image-20230720203531574


2.4.6.2. Buffer: Four working buffers set according to the role played.

At the same time, a buffer can play different functions in actual operation. Four working buffers are set up for this different function :

  • The working buffer ( ) used to hold input data hin.
  • The working buffer ( ) used to extract input data sin.
  • The working buffer ( ) used to hold output data hout.
  • The working buffer ( ) used to extract output data sout.

Four different scenes use the above four different functional roles .


Scenario 1: The input process requests input data

Process: Remove a piece of work buffer (hin) from the empty buffer queue as a working buffer (hin) to accommodate input data. After the buffer is filled with data, it is placed at the end of the input queue.

image-20230720205456118


Scenario 2: The computing process wants to obtain a piece of input data

Process: Obtain a buffer full of input data from the input queue as the "working buffer (sin) for extracting input data". After the buffer is read empty, it is hung into the empty buffer queue.

image-20230720210032230


Scenario 3: The computing process wants to flush prepared data into the buffer

Process: Take out a block from the empty buffer queue as a "working buffer (hout) to accommodate output data". When the data is full, the buffer is placed at the end of the output queue.

image-20230721160555689


Scenario 4: The output process requests output data.

Process: Obtain a buffer full of output data from the output queue as the "working buffer for extracting output data (sout)". After the buffer is read empty, it is hung into the empty buffer queue.

image-20230721161107447

The above scenarios are examples of inputting data from the peripheral to the buffer, the user process reading the input data, the user process outputting the results to the buffer, and the last one getting the buffer in the output queue to extract data. The left part is for containment I think it is the peripherals that perform input and output, and the right side is where the user process inputs and outputs data. In fact, it is a two-way transmission between the user process and the peripherals.


Knowledge overview

image-20230721161446241


3. Disk and SSD

3.1. Disk structure

image-20230721194438124


3.1.1. The concepts of disk, track and sector

The surface of the disk is composed of some magnetic materials, which can be used to record binary data.

image-20230721194544355

  • The round one is the disk of the disk. If you want to read the binary data accessed in this disk, then you need the magnetic head arm or simply the magnetic arm to drive the magnetic head to move and put the magnetic head in the corresponding position to read. The data stored at the specified location.

The surface of the disk will be divided into tracks , and such a "circle" is a track. A track is divided into sectors , each sector is a "disk block", and each sector stores the same amount of data (such as 1KB).

image-20230721194820103

There is a motor in the middle of the disk . When the motor rotates, it can drive the rotation of the entire disk.

We can number the sectors , sector 0, sector 1, sector 2, sector 3..., since the amount of data stored in each sector is the same, then the innermost sector area, its area is the smallest, but the amount of data it stores is the same as the other sectors, so the most memory sector has the highest data density.

image-20230721195229274


3.1.2. How to read/write data in the disk?

You need to move the "head" to the track where the sector you want to read/write is located, and then the disk will rotate with the motor, allowing the target sector to pass under the head to complete the read/write operation on the sector.

For example : If we want to read the data in the orange circle, then we can move the magnetic head to the orange circle position, and then the motor drives the rotation and sliding.

test


3.1.3. The concepts of disk and cylinder

In fact, there are many disk surfaces in the disk. The disk surfaces are arranged as follows. Each disk surface has a magnetic head, and the magnetic head is uniformly driven by the magnetic head arm to move inward and outward:

image-20230721201653280

Points to note :

① One disk may correspond to two disk surfaces, that is, the front side may be one disk surface, and the back may be another disk surface.

②All these magnetic heads are unified and connected to the same magnetic arm, so all magnetic heads can only "advance and retreat together". (For example: when one magnetic head moves inward, the other magnetic heads must also move inward)

③All these tracks with the same relative position among these disks form a cylinder. For example in the picture all the yellow tracks.


3.1.4. Physical address of disk

All the yellow tracks on the way have the same position on each disk, so these tracks form a cylinder. In fact, the physical address of the disk can be represented by such a ternary: address form (柱面号,盘面号,扇区号).

image-20230721201653280

Implement reading a "block" step based on this triplet :

  1. Move the magnetic arm according to the "cylinder number" so that the magnetic head points to the specified cylinder.
  2. Activate the head corresponding to the specified disk "disk number".
  3. During the rotation of the disk, it moves to the specified sector according to the "sector number", and the specified sector is passed under the magnetic head, thus completing the reading/writing of the specified sector.

3.1.5. Classification of disks

①Divided according to whether the magnetic head can move

活动头磁盘: The magnetic head can move, and the magnetic arm can stretch back and forth to drive the magnetic head to position the track. Each disk has a head .

image-20230721201953750

固定头磁盘: The magnetic head cannot be moved. This kind of disk has one magnetic head per track .

image-20230721202034089


②Classification based on whether the disc can be replaced

According to whether the disk can be replaced, it is divided into : replaceable disk, fixed disk

可换盘磁盘: The disc can be replaced.

固定盘磁盘: The disc cannot be replaced.

image-20230721202159362


Knowledge review and important test points

image-20230721202341784


3.2. Disk scheduling algorithm

image-20230721203621891


3.2.1. The time required for a disk read/write operation

Component 1: Seek time

Seek time (Seek time) Ts : The time it takes to move the head to a specified track before reading/writing data.

Contains two parts of time:

① It takes time to start the magnetic head arm, assuming it takes s.

②5 Moving the magnetic head also takes time. Assuming that the magnetic head is allowed to move, it takes m to cross a track each time, and a total of n tracks need to be crossed, then:

Seek time calculation : Ts = s + m * n.

  • Currently, it takes about 0.2ms for the hard disk to move one track, and the magnetic arm startup time is about 2ms.

test

  • It contains a startup time + moving head time.

Component 2: Delay time

Delay time T R : The time required to position the head to the target sector by rotating the disk. Assuming that the disk speed is r (unit: rev/second, or rev/min), the average required delay time calculation formula is: T n = (1/2) * (1/r) = 1/2r

  • Among them, 1/r refers to the time for one revolution. It takes half a revolution to find the target sector average, so the previous step will be multiplied by 1/2.

  • The typical rotation speed of a hard drive is 5400 rpm, or 7200 rpm. The higher the RPM, the better.

    test


Component 3: Transmission time

Transmission time T t : The time it takes to read data from or write data to the disk. Assume that the disk speed is r, the number of bytes read/written this time is b, and the number of bytes on each track is N, the final formula is as follows:

The transmission time calculation formula is : T t = (1 / r) * (b / N) = b / (rN)

  • (1/r) represents the time it takes to make one revolution. Since the number of bytes per track is N, the number of tracks is b/N.
  • Each track can store N bytes of data, so b bytes of data require b/N tracks to store, and the time required to read/write one track is exactly the time required for one revolution 1/r .

test


Total storage time combined

The final total access time is : T a = T s + 1/2r + b/(rN).

Conclusion : Both delay time and transmission time are related to disk rotation speed and are linearly related. The rotation speed is an inherent property of the hardware, so the operating system cannot optimize 延迟时间it 传输时间.

However, 寻道时间it can be optimized. The specific seek time is affected by the disk scheduling algorithm adopted by the operating system.


3.2.2. First come, first served algorithm (FCFS)

先来先服务算法(FCFS): Schedule according to the order in which processes request disk access.

Practical example :

Description : Assume that the initial position of the magnetic head is track 100, and multiple processes successively request access to 55, 58, 39, 18, 90, 160, 150, 38, and 184.

We first sort the tracks in the order before and after (the purpose is to better see the order, the sorting process is not included in the algorithm, and the algorithm will no longer include this step):

image-20230721222258637

  • You can see that the starting point is 100, the left is 90, and the right is 150.

Process : Then we follow the rules of FCFS and the order in which the requests arrive. The heads need to move to 55, 58, 39, 18, 90, 160, 150, 38, and 184. This is actually the order given in the question at the beginning. Finally, The sequence of moves is as shown below:

image-20230721222513682

The final calculation is: 45+3+19+21+72+70+10+112+146 = 498 tracks.

The number of requests is 9, so the average number of tracks needed to move in response to one request (average seek length): 498 / 9 = 55.3 tracks.

Advantages : Fair, if the tracks requested to be accessed are relatively concentrated, then the algorithm performance is still acceptable.

Disadvantages : If there are a large number of processes competing to use the disk and the tracks requested to be accessed are scattered, then FCFS will have poor performance and long seek times.


3.2.3. Shortest search time first (SSTF)

最短寻找时间优先(SSTF): This algorithm will prioritize the track closest to the current head . It can guarantee that each seek time is the shortest, but it does not guarantee that the total seek time is the shortest.

  • Essence: It is the idea of ​​​​a greedy algorithm. It just chooses the best at the moment, but it may not be the best overall.

Example :

Description : Assume that the initial position of the magnetic head is track 100, and multiple processes successively request access to tracks 55, 58, 39, 18, 90, 160, 150, 38, and 184.

The track for initial access is as shown below:

image-20230721223046255

Process: Starting from 100, first find the nearest track, which is 90, then find the closest track, and then continue with this strategy of 58, 55, 39, 38, 18, followed by 150, 160, and 184.

image-20230721223240068

The total number of tracks the magnetic head moves: (100-18) + (184-18) = 248 tracks.

Average seek length (average moves required to respond to a request): 248 / 9 = 27.5 tracks

Advantages : relatively good performance, short average search time.

Disadvantages : "Hungry" phenomenon may occur.

Case of starvation phenomenon : if after processing track 18, track 150 should be accessed corresponding to the picture above, but suddenly another access to track 38 comes. At this time, track 38 will be accessed first because it is closer. If some similar tracks come one after another, then for the relatively far tracks that come early, such as 150, 160, and 184, these tracks will be starved and will never be accessed and will never be satisfied. A "hungry" phenomenon occurred.

The cause of starvation : the magnetic head moves back and forth in a small area, resulting in, for example, that distant tracks cannot be accessed for a long time.

This question arose at this time 扫描算法.


3.2.4. Scanning algorithm (SCAN)

The reason for 最短寻找时间优先(SSTF)algorithm starvation is that the head may move back and forth in a small area. In order to prevent this problem, you can stipulate at this time:

  • 扫描算法(SCAN): Only when the magnetic head moves to the outermost track can it move inward, and when it moves to the innermost track can it move outward.

The way the magnetic head moves is very similar to an elevator, so it is also called 电梯算法.

Core : The moving direction of the magnetic head can only be changed when it reaches the outermost track.

Example :

Description : Assume that the tracks of a certain disk are 0-200, the initial position of the magnetic head is track 100, and at this time the magnetic head is moving in the direction of increasing track number, and multiple processes successively request access to 55, 58, and 39. , 18, 90, 160, 150, 38, 184 tracks.

image-20230722002017293

Process : The magnetic head moves to the right first to process track 150, and then moves to the right to process track 160, and the same is true after that. What is important to note is that the algorithm requires that the magnetic head must move to the outermost track before starting to move inward. Although track 184 There is no track number that needs to be requested. At this time, it will still move to the right until it reaches the outermost 200 tracks before starting to move inward. At this time, it will move to track 90 and start reading, followed by track 58, 55, 39, 38, and 18.

image-20230722002048605

The total number of tracks the magnetic head moves: (200-100) + (200-18) = 282 tracks.

Average seek length (average moves required to respond to a request): 282 / 9 = 31.3 tracks

  • The resulting seek time is much better than the first come, first served algorithm.

Advantages : better performance, shorter average seek time, and no starvation.

Disadvantages :

① Only when the outermost track is reached can the head movement direction be changed. In fact, after processing the access request for track 184, there is no need to move the head to the right.

②The SCAN algorithm responds to uneven frequencies of tracks at various locations.

  • For example: Assume that the head is moving to the right at this time and track 90 has just been processed. Then the next time the request for track 90 is processed, it will take a long time to wait for the head to move. However, after responding to the request for track 184, it will be processed again soon. Responded to the request for track 184 again.

Summary : Tracks near the edges are relatively better to be accessed repeatedly. Tracks in the middle can only be scanned after they have been accessed once. They can only wait until the edge is scanned before returning to scan.


3.2.5. Look scheduling algorithm (the first shortcoming improvement of the scanning algorithm)

Problem description : In the scanning algorithm (SCAN), the head movement direction can only be changed when it reaches the edge of the track. In fact, after processing the access request for track 184, there is no need to move the head to the right.

Look调度算法: In order to solve this problem, if there are no other requests in the head movement direction, the head movement direction can be changed immediately.

  • Observe while moving, hence the name LOOK.
  • Core description: If there are no other requests in the head movement direction, the head movement direction can be changed immediately.

For example :

Description: Assume that the disk's tracks are No. 0-200, the initial position of the magnetic head is track No. 100, and at this time the magnetic head is moving in the direction of increasing track numbers, and multiple processes have successively requested access to 55, 58, 39, and 18. ,90,160,150,38,184.

image-20230722003138828

Process : The magnetic head will also move to the right, and it will continue to move to the right like the scanning algorithm (SCAN), but the difference is that after accessing track 184, since there is no track number that needs to be requested on the right, then it will directly Change the moving direction of the magnetic head and start processing 90, 58, 55, 39, etc., as shown in the figure below:

image-20230722003326945

The total number of tracks the magnetic head moves: (184-100) + (184-18) = 250 tracks.

Average seek length (average moves required to respond to a request): 250 / 9 = 27.5 tracks

Advantages : Compared with the SCAN algorithm, there is no need to change the head direction every time it moves to the outermost or innermost position, further shortening the seek time.


3.2.6. Cyclic Scan Algorithm (C-SCAN): Improvement of the second shortcoming of the scanning algorithm

Problem description : In the scanning algorithm (SCAN), the response frequency of the tracks at each location is uneven, and the C-SCAN algorithm is designed to solve this problem.

循环扫描算法(C-SCAN): It is stipulated that the track access request will be processed only when the head moves in a certain direction, and when it returns, it will quickly move to the starting end without processing any request.

Description : Assume that the disk's tracks are No. 0-200, the initial position of the magnetic head is track No. 100, and at this time the magnetic head is moving in the direction of increasing track numbers, and multiple processes have successively requested access to 55, 58, 39, and 18. , 90, 160, 150, 38, 184 tracks.

image-20230722100505432

Process : The magnetic head will first serve the nearest track on the right, followed by 150, 160, and 184. Note that although 200 on the right is not the track requested for access, according to the algorithm rules, the direction of movement of the magnetic head can only be changed when it reaches the outermost track. . The head does not process any requests on its way back. Then when 200 is accessed, the head will be moved directly to track 0 (no processing will be performed), and then tracks 18, 38, 39, 55... will be processed in sequence.

image-20230722101324067

The total number of tracks the magnetic head moves: (200-100) + (200-0) + (90 - 0) = 390 tracks.

Average seek length (average moves required to respond to a request): 390 / 9 = 43.3 tracks

Where does this algorithm fit perfectly?

  • For example, when we access track No. 150 to the right, we will first reach the rightmost end, then return to the left to the starting point (this process will not access the track), and then start reading the track when we reach the leftmost initial position.

Advantages : Compared with SCAN, the response frequency of the track at each position is very average.

Disadvantages : The head movement direction can only be changed when it reaches the outermost track. In fact, after processing the access request for track 184, there is no need to move the head to the right; and when the head returns, it only needs to return to track 18. No. of tracks is enough, and there is no need to return to the edge track. In addition, compared with the SCAN algorithm, the average seek time is longer.


3.2.7. C-LOOK scheduling algorithm (solve the algorithm shortcomings of C-SAN)

In order to solve the shortcomings of the C-SCAN algorithm, the C-LOOK scheduling algorithm is proposed.

The main disadvantage of the C-SCAN algorithm is that the moving direction of the magnetic head can only be changed when it reaches the outermost track, and the magnetic head is not allowed to return to the outermost track when returning.

C-LOOK调度算法: Just to solve this problem, if there is no track access request in the direction of head movement, the head can be returned immediately, and the head only needs to return to the position where there is a track access request.

Description: Assume that the disk's tracks are No. 0-200, the initial position of the magnetic head is track No. 100, and at this time the magnetic head is moving in the direction of increasing track numbers, and multiple processes have successively requested access to 55, 58, 39, and 18. , 90, 160, 150, 38, 184 tracks.

image-20230722101856276

Process : The magnetic head will first serve the nearest track on the right, which are 150, 160, and 184 in order. Since the 200 track on the right does not need to be read, then it will turn around and move directly to the one on the left to be accessed. The first position (without any processing, although the 0 on the left is a boundary but does not require access), the head will turn around before processing the following tracks 18, 38, 39, 55... in sequence.

image-20230722102141359

The total number of tracks the magnetic head moves: (184-100) + (184-18) + (90 - 18) = 322 tracks.

Average seek length (average moves required to respond to a request): 322/9 = 35.8 tracks

Advantages : Compared with the C-SCAN algorithm, it does not need to move to the outermost or innermost position every time to change the direction of the magnetic head, further shortening the seek time.

Evaluation : Very similar to the LOOK algorithm: if there are no track access requests in the direction of head movement, the head can be returned immediately at this time.


Review of knowledge points and important test points

image-20230721214109625


3.3. Reduce disk latency

3.3.1. Understand the adverse effects of logically adjacent sectors and physically adjacent sectors? (increase delay time)

The time required for a disk read/write operation :

image-20230722121854196

Assume : Sectors 2, 3, and 4 in the orange area are to be read continuously: after the magnetic head reads a piece of content (that is, the content of a sector), it takes a short period of time to process, and the disk is constantly rotating. Therefore, if sectors No. 2 and No. 3 are arranged adjacent to each other, sector No. 3 cannot be read continuously after reading sector No. 2.

Let's look at the picture below. In the picture below, the magnetic head has finished reading sector No. 2, and it still needs some time to process (so you can't read sector No. 3 directly after reading sector No. 2), and the disk keeps reading. Rotatingly, the actual magnetic head is still processing the data of sector 2. The time of processing data is as follows:

test

  • You can see the time it takes to process sector 2. Since the disk is still rotating, the initial position of sector 3 has already been crossed at this time!

So how can we read sector 3?

  • You must wait for the disk to continue rotating, move to the initial position of sector 3 and cross sector 3 to complete sector reading.

test

Conclusion : After the magnetic head reads a sector of data, it takes a short period of time to process. If logically adjacent sectors are also physically adjacent, reading several consecutive logical sectors may require a long "delay time" ".

Core reason : Logically adjacent sectors are also physically adjacent.


3.3.2. Solution to reduce delay time: alternate numbering (solve the delay problem in Chapter 3.3.1)

Solution : Logically adjacent, but not physically adjacent with a gap.

Process : The first thing to read is sector No. 2. First, you need to rotate the disk so that sector No. 2 is placed under the head. Then, while the disk continues to rotate, the data of sector No. 2 can be read in. , after reading sector No. 2, the head needs a short period of time to make intermediate preparations. During this period, the head cannot read any data.

Due to the alternating numbering strategy, the head is ready to read data for the next time before sector No. 3 crosses the head . At this time, the disk continues to rotate, and sector No. 3 passes under the magnetic head. At this time, the magnetic head can also successfully read the data in sector No. 3.

test

  • Three moving steps : ① First move to the initial position of disk No. 2 and prepare to start reading data. ②Read sector No. 2. ③ Processing of data time of sector No. 2.

Effect : Using the strategy of alternating numbering, the delay time can be greatly reduced. That is, allowing logically adjacent sectors to have a certain physical interval can make the delay time required to read consecutive logical sectors smaller.

  • Time-saving point: After reading sector No. 2 and then reading sector No. 3, because the magnetic head is not ready yet, the disk needs to rotate one more time before it can continue to read sector No. 3. Using alternating numbering can leave time for the disk to rotate once and reposition.

3.3.3. Design of disk address structure

(disk number, cylinder number, sector number) binary design and the number of rotations required to read one circle of data?

Binary design of (disk number, cylinder number, sector number)

Assume that a disk has 8 cylinders/tracks (assuming the innermost cylinder/track number is 0), 4 disk surfaces, and 8 sectors. How many bits are used to represent each?

  • Then 3 binary digits can be used to represent the cylinder, 2 binary digits can represent the disk surface, and 3 binary digits can represent the sector.

The following only shows an illustration of the disk:

image-20230722122708889

The number of turns required to read one circle of data

First of all, let’s make it clear how many turns do we actually need to rotate for the inner circle of data?

  • It takes two turns to read one full circle of data.

Explanation : According to our previous plan to reduce the delay time in Chapter 3.3.2, we can only read sectors 0, 1, 2, and 3 in one actual rotation, and read 4, 5, 6, and 7 in the second rotation. sectors.

  • Logically continuous, physically discontinuous.

test


Why not use the structure of (disk number, cylinder number, sector number)?

Question : Suppose a disk has 8 cylinders/tracks (assuming the innermost cylinder/track number is 0), 4 disk surfaces, and 8 sectors. 3 binary bits can be used to represent the cylinder, and 2 binary bits can be used to represent the disk surface. , 3 binary bits represent sectors.

Requirement : What if the physical structure is (disk number, cylinder number, sector number), and you need to continuously read sectors with physical addresses (00,000,000)-(00,001,111)?

Among them, for (00,000,000)-(00,000,111), we can actually rotate it twice.

For (00,000,111) - (00,001,000), since 001 in the middle is transferred to cylinder No. 1, the magnetic head will be moved at this time. This movement of the magnetic head will take extra time !

test

That is to say, if the disk is placed in the middle, it will be easy to move the head when continuously reading a region of data (the number changes in the middle position).


Why use the structure of (cylinder number, disk number, sector number)?

Question : Suppose a disk has 8 cylinders/tracks (assuming the innermost cylinder/track number is 0), 4 disk surfaces, and 8 sectors. 3 binary bits can be used to represent the cylinder, and 2 binary bits can be used to represent the disk surface. , 3 binary bits represent sectors.

Requirement : What if the physical structure is (cylinder number, disk number, sector number), and you need to continuously read sectors with physical addresses (00,000,000)-(00,001,111)?

At this time, we are also reading (00,000,000) - (00,001,111). We directly fast forward to the key step (00,000,111) - (00,001,000). The 001 in the middle is to select disk No. 1. The operation that the disk will do at this time is to activate The magnetic head on disk No. 1 .

image-20230722124340300

Since the cylinder number/track number is the same, but the disk surface number is different, there is no need to move the head arm, and only the head on the adjacent disk surface can be activated . This time is much less than the time of moving the magnetic head!


Think about the difference and essence of these two physical addresses

Difference :

  • If it is used (盘面号,柱面号,扇区号), then execute consecutive operations such as 000 00 000 - 000 10 111. Since the cylinder number in the middle is used, the head needs to be moved, which will take extra time. The key is that this cylinder number is in the middle of the entire binary. Is it Many situations will affect the entire head movement;
  • If used (柱面号,盘面号,扇区号), the head will only move when the top number changes to 01 000 000 or 10 000 000. If it is like 000 10 111, since the middle number is the disk number, you only need to activate the corresponding disk. The magnetic head can start reading data, so it will be better than the previous design.

Answer : When reading disk blocks with consecutive addresses, (柱面号,盘面号,扇区号)the address structure used can reduce the time consumed by head movement.


3.3.4. Method to reduce delay time: Misplaced naming (point out the cause of the problem)

Problem : If multiple disks are placed at the same relative position and have the same sector number, the delay time will increase.

Example : If adjacent disks have the same relative position and the same sector number, all disks will rotate together.

The first is to read the sector data at the physical location (000,00,111), which requires two rotations:

test

After the second round of rotation, the data of the second round will actually be processed for a short period of time. Note that the position of the magnetic head has moved to sector No. 4 at this time (the magnetic heads are multiple disk surfaces rotating at the same time):

test

Next, we are going to read the data of sector (000,01,000). At this time, disk No. 1 will be activated to start reading data. You will find that the current head is already at the position of sector No. 4 (because before processing (Affected by the data loss time of the second circle). If you want to read sector 0 at this time, you need to make another turn and move to the initial position of sector 0! ! !

A solution can be adopted at this time :错位命名

Solution: The sector numbers corresponding to different disk surfaces are not the same.

For example, the current disk No. 0 starts from the initial position of sector No. 0, and sector No. 1 starts from the initial position of disk No. 7.

image-20230722130128426

At this point, let's review it again. After reading two rounds and adding the time to process the second round, where will the head of disk No. 1 rotate to at this time?

test

It can be seen that after the processing time of the second round is finally added, the magnetic head of disk No. 1 can move exactly to the position of disk No. 0 (physically misaligned naming ), and the data can be read directly at this time, thus reducing the time required. delay.


Review of knowledge points and important test points

image-20230722121721556


3.4. Disk management

image-20230722132055973


3.4.1. Disk initialization

Disk initialization process :

image-20230722132758562

Step1 : Perform low-level formatting (physical formatting) and divide each track of the disk into sectors. The various data structures required to manage sectors are generally stored in the head and tail parts, including sector check codes (such as even parity, CRC cyclic redundancy check code, the check code is used to verify Check whether the data in the sector has errors).

  • A sector can usually be divided into three parts: header, data area (such as 512B size), and tail.

Step2 : Partition the disk, and each partition is composed of several cylinders (that is, divided into the familiar C drive, D drive, and E drive).

image-20230722132842121

Step3 : Perform logical formatting and create a file system. Including creating the root directory of the file system and initializing data structures used for storage space management, such as bitmaps and free partition tables.

Note: Sector division is performed during low-level formatting (Step 1).


3.4.2. Boot block

After the disk has been physically formatted, partitioned, and logically formatted, the operating system-related data can be written to the disk. This is also a process of installing the operating system by yourself. When the computer is turned on, a process needs to be performed. The series of initialization work includes initializing the CPU, initializing the memory, and some emergency components such as initializing registers.

This initialization process requires the execution of a so-called initialization program, also called a bootloader . When the computer is turned on, these programs in the ROM will be read and executed to complete the initialization work.

引导块: When the computer is turned on, a series of initialization work is required, which is completed by executing the initialization program (bootloader).

image-20230722133338852

The bootloader is usually stored in ROM, which is read-only memory. The data in ROM has been written to ROM when it leaves the factory and cannot be modified later.

Note: ROM is usually integrated into the motherboard when leaving the factory.

There is a problem at this time: there are some problems in placing the initialization program (bootloader) program in the ROM. If the bootloader needs to be updated, it will be very inconvenient because the data in the ROM cannot be changed. How to solve it?

Solution: Only a small "bootloader" is actually stored in ROM. The complete bootloader will be placed on the boot block (ie boot block/boot partition) of the disk, and the boot block is located at a fixed location on the disk.

image-20230722133916336

Execution process : When the computer is turned on, it will first execute the bootloader in the ROM. During the process of executing the bootloader, the CPU can find the boot block, and then read the complete "bootloader" memory, the initialization is completed.

Benefit : Updating the bootloader becomes very convenient.

**Boot disk (system disk, C: drive): **A disk with a boot partition (boot block boot block).


3.4.3. Management of disk bad blocks (two methods)

坏块: Refers to sectors that are damaged and cannot be used normally are "bad blocks". It is a hardware failure that cannot be repaired by the operating system. Bad blocks should be marked to prevent them from being used incorrectly. Bad blocks are no longer allocated to any file.

There are generally two ways to manage bad blocks : marking in the FAT table and maintaining a linked list of bad blocks.


Method 1: FAT table indication

Strategy : For simple disks, you can check the entire disk for bad blocks during logical formatting (when creating a file system) and indicate which sectors are bad sectors, for example, on the FAT table.

Whether it is transparent to the operating system : not transparent to the operating system.


Method 2: Maintain the bad block list

Strategy : For complex disks, the disk controller maintains a bad block list. When the disk is low-level formatted (physical formatted) before leaving the factory, the fast link is initialized.

  • 磁盘控制器: Refers to a hardware component inside the disk device.

For some bad blocks, some "spare sectors" will be reserved at this time to replace the bad blocks. This scheme is called sector sparing.

image-20230722135129933

  • The outermost circle is the spare sector, and the red part is the bad sector.

Whether to be transparent to the operating system : Transparent to the operating system.


Review of knowledge points and important test points

image-20230722135216764


3.5. Solid state drive SSD

New outline:

image-20230722135731978

The characteristics of the multiple-choice test are listed in the mind map below:

image-20230722135815181


3.5.1. Mechanical hard drive VS solid state drive

Mechanical hard disks store data based on magnetic substances on the small disk surface to record binary 0s and 1s; solid-state hard disk storage media are based on flash memory technology (the same is true for USB flash drives).

image-20230611153538957

  • The black blocks in the solid-state drive are flash memory chips.

3.5.2. Composition of solid state drive

The logical address is sent through the IO bus, and then mapped to the corresponding physical address through the flash memory translation layer. The flash memory translation layer does the work of address conversion.

image-20230611153610464

Then dig into the internal structure of the flash memory chip. A flash memory chip is composed of several data blocks, and the size of a block ranges from 16KB to 512KB.

image-20230611153622378

Each block can be broken down into pages, each page size is 512B-4KB.

image-20230611153632294

Note: The system reads and writes the solid-state drive in pages. Read/write one page at a time.

  • If it is a disk, then one read and write corresponds to one block or sector. A page of a solid-state drive is equivalent to a sector of a disk. One block of a solid-state drive is equivalent to one track. A track contains multiple sectors.

3.5.3. Read/write characteristics

image-20230611153946746

For one of the features : the solid-state drive will be erased in units of blocks. Each page of the erased block can be written once and read unlimited times.

If several pages are written first in a block, then modifying those pages is not allowed unless the entire block is erased before it can be rewritten.

What if I just want to rewrite one page, do I have to erase the entire page? In fact, the solid-state drive will write the specified pages except that page to other blocks, that is, first copy them to another block, then write the new page to another block, and then erase the original block. Just remove it.

  • At this time, the flash translation layer will re-map the specified logical address to a new physical address, which is the new block.

image-20230611154008828

Due to this feature, solid-state drives read quickly but write slowly.


3.5.4. Wear leveling technology

Regarding the difference between the positioning of mechanical hard disks and solid-state hard disks : solid-state hard disks are quickly positioned through circuits; mechanical hard disks rotate through moving magnetic arms.

  • Solid-state drives support random access, and access to any address takes the same time. If the physical address of the mechanical hard disk is far away from the magnetic arm, it needs to be moved and rotated.

Disadvantages of solid-state drives : If a block is frequently erased and rewritten, it will cause damage.

image-20230611154044100

According to this shortcoming, there is a solution: wear leveling technology . That is to find a way to evenly distribute erasures on each block to improve the service life. For data blocks with more reads and fewer writes, the data can be migrated to older blocks. The main reason is that if you read more and write less, there will be less need for erasure.

  • It will monitor how many times each block is read/written in the background and perform appropriate migration based on actual needs.

Extension: SSD lifespan

image-20230611154109269


Summarize

image-20230722135815181


Organized by: Long Road Time: 2023.7.19-22

Guess you like

Origin blog.csdn.net/cl939974883/article/details/131854613