Input and output (IO)

Input and output (IO)

I/O input and output can input data to the device or receive the computer output data external device

Insert picture description here
Insert picture description here
Insert picture description here

I/O controller

The mechanical parts of the I/O device are mainly used to perform specific I/O operations. For example, we can see and touch the buttons of the mouse/keyboard; the LED screen of the display; 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 inserted into the expansion slot of the motherboard

The CPU cannot directly control the mechanical components of the I/O device. Therefore, the I/O device must have an electronic component as an "intermediary" between the CPU and the mechanical components of the I/O device to realize the control of the device by the CPU.

This electronic component is the I/O controller, also known as the device controller. The CPU can control the I/O controller, and the I/O controller controls the mechanical parts of the device.

Insert picture description here
Insert picture description here

①One I/O controller may correspond to multiple devices;

② 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 operations. Some computers make these registers occupy part of the memory address, which is called memory mapped I/O; other computers use I/O dedicated addresses, that is, register independent addressing.

Insert picture description here

I/O controller

Direct program control mode (polling)

Insert picture description here

Advantages: Simple to implement. After the read/write instruction, add a series of instructions to realize the loop check (hence the name "program direct control method")

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

Read and write one word at a time

Interrupt-driven

Introduce an interrupt mechanism. Due to the slow speed of I/O devices, after the CPU issues a read/write command, the process waiting for I/O can be blocked and switched to another process for execution. When the I/O is completed, the controller will send an interrupt signal to the CPU. After the CPU detects the interrupt signal, it will save the operating environment information of the current process and then execute the interrupt handler to handle the interrupt. In the process of processing the interrupt, the CPU reads a word of data from the I/O controller and transfers it to the CPU register, and then writes it to the main memory. Then, the CPU restores the operating environment of the process (or other processes) waiting for I/O, and then continues execution.

Insert picture description here

Advantages: Compared with the "program direct control mode", in the "interrupt drive 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. CPU and I/O devices can work in parallel, and CPU utilization has been significantly improved.

Disadvantages: Each word transfer between I/O device and memory needs to pass through the CPU. Frequent interrupt processing will consume more CPU time.

DMA method

DMA mode (Direct Memory Access, direct memory access. Mainly used for I/O control of block devices) has several improvements:

① The unit of data transmission is "block". It is no longer a word, word transmission;

②The flow of data is from the device directly into the memory, or from the memory directly to the device. No need for CPU anymore

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

Insert picture description here
Insert picture description here

DR (Data Register): Temporarily store data from the device to the memory, or from the memory to the device.

MAR (Memory Address Register): During input, MAR indicates where the data should be placed in the memory; during output, 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 (Command Register, command/status register): Used to store the I/O commands sent by the CPU or the status information of the device.

Advantages: Data transmission takes "block" as the unit, and the CPU intervention frequency is further reduced. Data transmission no longer needs to go through the CPU and then write to the memory, and the data transmission efficiency is further increased. The parallelism of CPU and I/O devices has been improved.

Disadvantage: Each time the CPU issues an I/O instruction, it can only read/write one or more consecutive data blocks. If you want to read/write multiple discretely stored data blocks, or write data to different memory areas, the CPU has to issue multiple I/O instructions and perform multiple interrupt processing to complete.

Channel control method

Channel: A piece of hardware that can recognize and execute a series of channel instructions

Insert picture description here

Disadvantages: complicated to implement and requires dedicated channel hardware support

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

I/O software hierarchy

User-level software I/O system or I/O core subsystem (device independence software device driver interrupt handler) hardware

User level software

Realizes the interface for interacting with the user, and the user can directly use the library functions related to I/O operations provided by this layer to operate the device

Device independence software

Also known as device-independent software. Almost all functions that have nothing to do with the hardware characteristics of the device are implemented in this layer.

① Provide a unified call interface to the upper layer (such as read/write system call)

② Equipment protection: The principle is similar to that of file protection. The device is regarded as a special file, and different users have different access rights to each file. Similarly, the access rights to the device are also different.

③Error handling

④ Distribution and recycling of equipment

⑤Data buffer management: buffer technology can be used to shield the difference in data exchange unit size and transmission speed between devices

⑥Establish the mapping relationship between the logical device name and the physical device name; select and call the corresponding driver according to the device type

When the user or user-level software issues a system call related to the I/O operation, it needs to specify the logical device name of the I/O device to be operated this time (eg: when going to the school print shop to print, you need to select Printer 1/Printer 2 /Printer 3, in fact, these are all logical device names) The device independence software needs to determine the physical device corresponding to the logical device through the "logical device table (LUT, Logical Unit Table)", and find the device driver corresponding to the device

Logical device table

Only one LUT is only applicable to single user operating system

Set a LUT for each user

Device driver

It is mainly responsible for the specific control of hardware devices, and converts a series of commands (such as read/write) issued by the upper layer into a series of operations that the specific device can "understand". Including setting device registers; checking device status, etc.

Different I/O devices have different hardware characteristics, and only the manufacturer of the device knows the details. Therefore, manufacturers need to design and provide corresponding drivers according to the hardware characteristics of the device.

Interrupt handler

When the I/O task is completed, the I/O controller will send an interrupt signal, and the system will find and execute the corresponding interrupt handler according to the type of the interrupt signal.

Insert picture description here

I/O core subsystem

Spooling technology (SPOOLing technology) needs to request the service of the device independence software of the "disk device", so generally speaking, the spooling technology is implemented in user-level software. But some textbooks say it is the function of the I/O core subsystem

I/O scheduling, device protection, device allocation and recycling, buffer management (ie buffering and cache) are implemented by device-independent software

I/O scheduling: Use a certain algorithm to determine a good order to process each I/O request.

Device protection: different users have different access rights to each file

Spooling technology

Offline technology: out of the control of the host, input and output operations

Spooling technology: Simulate offline technology with software

Insert picture description here

Must have multi-program technical support. The system will establish "input process" and "output process".

Shared printer:

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

(2) Apply for a blank print request form for the user process, and fill in the user's print request in the form (in fact, it is used to explain the storage location of the user's print data and other information), and then hang the form to spool Machine file queue.

(3) When the printer is idle, the output process will take out a print request form 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 form, and then output to the printer for printing. In this way, all printing tasks can be processed in sequence

Allocation and recycling of equipment

Factors to consider in equipment allocation

Equipment inherent attributes :

Device exclusive (only one process can be allocated in a time period)

Shared equipment (can be allocated to multiple processes at the same time, shared on a macro level)

Virtual device (using spooling technology can be allocated to multiple processes at the same time)

Allocation algorithm of equipment

First-come, first-served, the highest priority, short task priority

Device allocation security

Safe allocation method : After allocating equipment to the process, it enters process blocking, and the process wakes up after I/O is completed

Advantages: destroy the "request and hold" condition, no deadlock

Disadvantages: For a process, CPU and I/O devices can only work serially

Unsafe allocation method : After a process sends an I/O request, the system allocates an I/O device for it, the process can continue to execute, and then a new I/O request can be issued. Only when a certain I/O request is not satisfied will the process be blocked.

Advantages: The calculation tasks and I/O tasks of the process can be processed in parallel, so that the process can advance rapidly

Disadvantages: deadlock may occur (deadlock avoidance, deadlock detection and release)

Static allocation : allocate all required resources to the process before running, and return the resources after running

**Dynamic allocation: **Dynamic application of equipment resources during process operation

Data structure in equipment management

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

Device Control Table (DCT) : The system configures a DCT for each device to record the status of the device

Device type : such as: printer/scanner/keyboard

Device identifier : the physical device name, the physical device name of each device in the system is unique

Device status : busy/idle/faulty...

Pointer to the controller table : each device is controlled by a controller, the pointer can find the information of the corresponding controller

Repeated execution times : when repeated I/O operations are still unsuccessful, the I/O is considered as failed

The head pointer of the device queue: Point to the process queue (queue composed of process PCB) waiting for the device.
Controller control table (COCT) : Each device controller corresponds to a COCT. The operating system operates and manages the controller based on the COCT information.

Controller identifier Unique ID of each controller

Controller status Busy/Idle/Fault...

Pointer to the channel table Each controller is controlled by a channel, the pointer can find the information of the corresponding channel

The head pointer of the controller queue points to the process queue waiting for the controller (the queue is composed of the process PCB)

Tail pointer of the controller queue

Channel Control Table (CHCT) : Each channel corresponds to a CHCT. The operating system operates and manages the channel according to the CHCT information.

**Channel Identifier: **Unique ID of each channel

**Channel status: **Busy idle fault

The first address of the controller table connected to the channel : the pointer can be used to find all controller-related information (COCT) managed by the channel

The head of the channel queue

The tail pointer of the channel queue : points to the process queue that is waiting for the channel (the queue is composed of the process PCB)

System Equipment Table (SDT) : Records the conditions of all equipment in the system, and each equipment corresponds to an entry.

Insert picture description here

Equipment allocation steps

① Search SDT according to the physical device name requested by the process (Note: The physical device name is the parameter provided when the process requests to allocate the device)

②Find DCT according to SDT, if the device is busy, hang the process PCB in the device waiting queue, and assign the device to the process if it is not busy.

③ Find COCT according to DCT, if the controller is busy, hang the process PCB in the controller waiting queue, and assign the controller to the process if it is not busy.

④ Find CHCT according to COCT, if the channel is busy, hang the process PCB in the channel waiting queue, and assign the channel to the process if it is not busy.

Disadvantages:

①Users must use the "physical device name" when programming. The underlying details are not transparent to the user and are not convenient for programming. ②If a physical device is changed, the program cannot run. ③If the physical device requested by the process is busy, even if the Type of equipment, the process must also be blocked waiting

Improvement method: Establish a mapping mechanism between logical device name and physical device name, users only need to provide the logical device name when programming

①Search SDT according to the logical device name requested by the process (Note: The logical device name provided by the user during programming is actually the "device type")

②Search SDT, find the idle equipment of the type specified by the user process, and assign it to the process. The operating system adds an entry to the logical device table (LUT).

③ Find COCT according to DCT, if the controller is busy, hang the process PCB in the controller waiting queue, and assign the controller to the process if it is not busy.

④ Find CHCT according to COCT, if the channel is busy, hang the process PCB in the channel waiting queue, and assign the channel to the process if it is not busy.

Buffer management

The buffer is a storage area, which can be composed of dedicated hardware registers, or the memory can be used as a buffer.

The cost of using hardware as a buffer is relatively high, and the capacity is small. It is generally only used in occasions with very high speed requirements.

In general, memory is used as a buffer. The buffer management of "device independence software" is to organize and manage these buffers.

Insert picture description here

Single buffer

Did not specify that a buffer is a block

When the buffer data is not empty, data cannot be flushed into the buffer, but data can only be transferred out from the buffer; when the buffer is empty, data can be flushed into the buffer, but the buffer must be filled. Transfer data from the buffer.

Calculate the time to process a piece of data

Assuming that the workspace is full and the buffer is empty

Input to buffer time T, buffer to work area transfer M, processing time C

When T>C, then T+M T<C, then C+M

Double buffering

If the single buffer strategy is adopted, the operating system will allocate a buffer for it in main memory

calculating time

Assuming the initial state: the workspace is empty, one is full and the other is empty

Assuming T>C+M processing time T'

Assuming T<C+M processing time C+M

The difference between using single/double buffering in communication

If two mutually communicating machines only have a single buffer, only one-way data transmission can be realized at any one time

If two mutually communicating machines are equipped with double buffers, two-way data transmission can be realized at the same time.

Note: The "pipe" in pipe communication is actually a buffer. To achieve two-way transmission of data, two pipelines must be set up

Circular buffer

Link multiple buffers of equal size into a circular queue. Note: In the following figure, orange indicates a buffer full of data, and green indicates an empty buffer.

Insert picture description here

Buffer pool

The buffer pool consists of buffers shared in the system. These buffers can be divided into: empty buffer queues, buffer queues filled with input data (input queues), and buffer queues filled with output data (output queues).

According to the different functions of a buffer in actual operations, four working buffers are set up: working buffer (hin) for storing input data, working buffer (sin) for extracting input data, and Working buffer (hout) for storing output data, and working buffer (sout) for extracting output data

Input process requests input data

Take out a work buffer (hin) from the empty buffer queue as a working buffer (hin) for holding input data. After flushing the data, hang the buffer to the end of the input queue

The calculation process wants to get a piece of input data

Obtain a buffer full of input data from the input queue as the "working buffer for extracting input data (sin)". After the buffer is empty, hang to the empty buffer queue

The calculation process wants to flush the prepared data into the buffer

Take a piece from the empty buffer queue as a "working buffer (hout) for storing output data". After the data is full, the buffer is hung to the end of the output queue

The output process requests output data

Washing area (hin), working buffer (sin) for extracting input data, working buffer (hout) for holding output data, and working buffer (sout) for extracting output data

Guess you like

Origin blog.csdn.net/weixin_44518702/article/details/110735116