Operating System 13: Interrupt Handlers and Device Drivers

Table of contents

1. Interrupt handler

(1) Interruption and trapping

(2) The processing of the interrupt handler

2. Set the driver

(1) Functions of the device driver

(2) The processing of the device driver

(4) Control methods for I/O devices

4.1 - Polling Programmable I/O Mode

4.2 - Interrupt Programmable I/O Mode

4.3 - DMA Controller

4.4 - I/O Passage

3. Device-independent I/O software

(1) Physical device name, logical device name

(2) Functional composition of device-independent software

(3) Realization of logical device name to physical device name mapping

4. I/O software of user layer

(1) System calls and library functions

1.1 - What are system calls?

1.2 - What are library functions?

(2) Spooling system

2.1 - What is spooling technology?

2.2 - How the spooling system works

2.3 - Components of the spooling system

2.4 - Using daemons


1. Interrupt handler

        Interrupt has a special and important position in the operating system, and it is the basis for the realization of multiprogramming . Without interrupts, multiprogramming would not be possible, because switching between processes is done through interrupts. On the other hand , interrupts are also the basis of device management . In order to improve the utilization rate of processors and realize parallel execution of CPU and I/O devices, interrupt support is also necessary. The interrupt handler is the lowest layer in the I/O system, and it is the basis of the entire I/O system. //Importance of interruption

(1) Interruption and trapping

  • Interrupt : refers to the CPU's response to the interrupt signal sent by the I/O device. The CPU suspends the program being executed, and after the CPU environment is reserved, it automatically transfers to execute the interrupt handler of the I/O device. After execution, return to the breakpoint and continue to execute the original program.
  • Trapped (trap): Interrupts caused by internal events of the CPU, such as overflow or underflow in the operation of the process, or program errors, such as illegal instructions, address out-of-bounds, and power failure.

        The main difference between an interrupt and a trap is the source of the signal, whether it is from outside the CPU or inside the CPU.

(2) The processing of the interrupt handler

        When a process requests an I/O operation, the process will be suspended until the I/O device completes the I/O operation, and the device controller sends an interrupt request to the CPU. After the CPU responds, it turns to the interrupt handler. The processing program executes the corresponding processing, and releases the blocking state of the corresponding process after processing.

        The processing of the interrupt handler can be divided into the following steps:

  1. Determine if there are unacknowledged interrupt signals.
  2. Protect the CPU environment of the interrupted process .
  3. Transfer to the corresponding equipment processing procedure.
  4. Interrupt handling.
  5. Restore the context of the CPU and exit the interrupt.

//It is necessary to save the CPU environment of the interrupted process before interrupt processing, which is consistent with the idea of ​​context switching

        Interrupted processing flow:

2. Set the driver

        The device handler is usually also called the device driver. It is a communication program between the upper layer of the I/O system and the device controller . Its main task is to receive abstract I/O requirements from the upper layer software, such as read or write commands. , and then convert it into specific requirements, send it to the device controller, start the device to execute; otherwise, it will also transmit the signal sent by the device controller to the upper layer software. // Purpose of the driver

        Because drivers are closely related to hardware, there should usually be one driver for each type of device. For example, printers and monitors require different drivers. //The driver is provided by the device supplier, the system provides a unified interface, and the device supplier implements it, thus shielding the complexity at the system layer

(1) Functions of the device driver

  1. Receive commands and parameters sent by device-independent software , and convert the hit abstract requirements into device-related low-level operation sequences.
  2. Check the legitimacy of the user's I/O request , understand the working status of the I/O device, pass the parameters related to the I/O device operation, and set the working mode of the device.
  3. Issue an I/O command , if the device is idle, start the I/O device immediately to complete the specified I/O operation; if the device is busy, hang the requester's request block on the device queue and wait.
  4. Respond in time to the interrupt request sent by the device controller , and call the corresponding interrupt handler for processing according to the interrupt type.

(2) The processing of the device driver

        The main task of the device driver is to start the specified device and complete the I/O work specified by the upper layer . However, the necessary preparatory work should be completed before starting, such as detecting whether the device status is "busy" and so on. After completing all preparations, a start command is sent to the device controller.

        The following is the processing procedure of the device driver:

  1. Convert abstract requirements into concrete requirements.
  2. Validate service requests. Check whether the user's I/O request is capable of being performed by the device.
  3. Check the status of the device. Check if the I/O device is ready to receive. If yes, start its device controller, otherwise just wait.
  4. Pass the necessary parameters.
  5. Start the I/O device.

        In a multiprogramming system, once the driver issues an I/O command and starts an I/O operation, the driver returns control to the I/O system and blocks itself until it is woken up when an interrupt arrives. The specific I/O operation is carried out under the control of the device controller. Therefore, when the device is busy transmitting data, the processor can do other things, realizing the parallel operation of the processor and the I/O device . // Driver -> Block and wake up

(4) Control methods for I/O devices

For the control of the device, the programmable I/O method of polling         was used in the early stage , and later developed into the programmable I/O method of using interrupt . With the emergence of the DMA controller , the transfer is performed in units of data blocks instead of bytes, which greatly improves the I/O performance of block devices. The emergence of I/O channels enables the organization of I/O operations and data transfer to be carried out independently without CPU intervention.

        Throughout the development of the I/O control method, there has always been such a purpose: to minimize the intervention of the host on I/O control, and to free the host from the complicated I/O control affairs , so that more can be done. Complete data processing tasks.

4.1 - Polling Programmable I/O Mode

        The processor sends an I/O command to the controller, which needs to continuously test the state of the I/O controller in a loop. //polling

        Defect: Most of the time of the CPU is in the loop test waiting for the I/O device to complete the data I/O, resulting in a great waste of CPU. // In this mode, there is no interrupt mechanism in the CPU

4.2 - Interrupt Programmable I/O Mode

        The CPU sends an I/O command to the device controller of the I/O device, and immediately returns to continue executing the original task. The device controller controls the specified I/O devices according to the command requirements. For example, when inputting, once the I/O data is ready, the controller sends an interrupt signal to the CPU through the control line to notify the CPU to obtain the data. //CPU and I/O devices execute in parallel

        In this mode, both the CPU and I/O devices are in a busy state, which improves the resource utilization and throughput of the entire system .

4.3 - DMA Controller

        Interrupt-driven I/O performs I/O in units of bytes . Whenever a byte of I/O is completed, the controller will request an interrupt from the CPU. That is to say, in order to read a 1KB data block from the disk, the CPU needs to be interrupted 1K times. //reasons to use direct memory access

        Features of DMA (Direct Memory Access) mode:

  1. The basic unit of data transfer is the data block .
  2. The transferred data is brought directly into memory from the device, or vice versa.
  3. CPU intervention is required only at the beginning and end of the transmission of one or more data blocks, and the transmission of the entire block of data is completed under the control of the controller.

4.4 - I/O Passage

        In DMA mode, each time the CPU issues an I/O command, it can only read and write a continuous data block . When it is necessary to read and write multiple data blocks at a time, the CPU must issue multiple I/O instructions and perform multiple interrupt processing to complete. // The background of the appearance of the I/O channel

        The I/O channel method is the development of the DMA method, which can further reduce the intervention of the CPU, that is, the intervention of reading (or writing) a data block is reduced to the reading (or writing) and processing of a group of data blocks. Interventions related to the control and management of the unit . At the same time, it can realize the parallel operation of CPU, channel and I/O device, so as to improve the resource utilization rate of the whole system more effectively. //The read and write unit is converted from a data block to a group of data blocks

// The development of control methods can be summed up as: no interruption -> interruption (byte) -> data block -> multiple data blocks

3. Device-independent I/O software

        In order to facilitate the user and improve the adaptability and scalability of the OS, in the I/O system of the modern OS, the device-independent I/O software is added without exception to achieve device independence, also known as Device independence. //Think about registry

        Its basic meaning is: the equipment used in the application is not limited to use a specific physical equipment. The device driver configured for each device is software closely related to the hardware. In order to achieve device independence, a layer of software must be set on top of the device driver, called device-independent I/O software , or device-independent software.

(1) Physical device name, logical device name

        Using the physical name of the device directly associates the application with the physical device on the system. The system can only allocate according to the physical name of the device, and cannot use another identical device (different physical device name). If the physical device is changed, the application directly related to the old device name will no longer be able to run, so this method Very inflexible. //Similar to the communication of binding a fixed IP address

        Logical device name : A logical device is an abstract device name that does not specify a specific device. I/O redirection is also possible using logical device names. The so-called I/O redirection means that the device used for I/O operations can be replaced (ie redirected) without changing the application program . // Data structure of I/O redirection: logical device table

        The concept of logical device name and physical device name is very similar to the concept of logical address and physical address in memory management . When the program is executed, the logical address must be converted into a physical address first. Similarly, in order to realize the conversion from the logical device name to the physical device name, a logical device table needs to be configured in the system .

(2) Functional composition of device-independent software

  1. Uniform interface for device drivers : abstraction and concrete implementation are separated. // polymorphism
  2. Buffer management : ease the conflict between CPU and I/O devices, and improve CPU utilization.
  3. Error control : network retransmission, disk retransmission, bad block recording, etc.
  4. Allocation and recycling of equipment : In order to avoid competition for exclusive equipment by processes, exclusive equipment must be allocated by the system uniformly, and an application must be made before a process uses equipment.
  5. Device-independent logical data block : used to shield the differences in data exchange units, read and transfer rates of different devices.

(3) Realization of logical device name to physical device name mapping

        In order to achieve independence from the device, when the application program requests to use the I/O device, it should use the logical device name. However, the system only recognizes physical device names, so a logical device table needs to be configured in the system to map logical device names to physical device names . //Idea: logic -> physics, all require a conversion process, usually using an intermediate table to achieve

        Logical Unit Table LUT (Logical Unit Table)

        The logical device table includes the following three items: logical device name , physical device name and the entry address of the device driver .

        When a process requests allocation of an I/O device with a logical device name, the system allocates a corresponding physical device for it. At the same time, create an entry on the logical device table, fill in the logical device name used in the application program, the physical device name assigned by the system, and the entry address of the device driver. // build on first use

        When other processes use the logical device name to request I/O operations, the system can find the physical device corresponding to the logical device and the driver of the device by searching the LUT. //Reuse previous information

4. I/O software of user layer

Generally speaking, most of the I/O software is placed inside the operating system, but a small part is still in the user layer, including library functions         linked with user programs , and spoolers that run completely outside the kernel system etc.

(1) System calls and library functions

1.1 - What are system calls?

        In order to protect the security of the device, the system does not allow the process running in the user state to directly call the OS process running in the core state, but introduces an intermediary process in the user layer—system call, through which the application program can indirectly call The I/O process in the OS operates on I/O devices .

        In fact, all functions provided by the OS to the user must be obtained by the user process through system calls. System calls are the only way for applications to obtain all the services of the OS .

1.2 - What are library functions?

        The relationship between the kernel and library functions: the kernel provides the basic functions of the OS, and the library functions extend the OS kernel so that users can easily obtain the services of the operating system. // The library function is an API open to users, through which the OS kernel can be directly accessed

        In many modern OSs, the system calls themselves are already written in C and provided as functions, so they can be used directly in user programs written in C. //In the early days, assembly language was used

(2) Spooling system

A physical CPU can be virtualized as multiple logical CPUs         through multiprogramming technology , allowing multiple users to share a single host. //virtual technology

        Through the spooling technology , one physical I/O device can be virtualized into multiple logical I/O devices, thus allowing multiple users to share one physical I/O device. //Spool technology, one physical machine maps multiple logical machines

2.1 - What is spooling technology?

        When the multi-program technology is introduced into the system, use one of the programs to simulate the function of the peripheral control machine during offline input , and transfer the data on the low-speed I/O device to the high-speed disk. Then use another program to simulate the function of the peripheral control machine during offline output, and transfer the data from the disk to the low-speed output device. In this way, the previous off-line input and output functions can be realized under the direct control of the host. //Start multiple processes at the same time to operate on I/O devices

        At this time, the peripheral operation and the processing of data by the CPU are carried out at the same time. We call this technology spooling technology . SPOOLing (Simultaneaus Peripheral Operating OnLine)

2.2 - How the spooling system works

        Spooling technology is a simulation of offline input/output system, which is based on channel technology and multiprogramming technology .

2.3 - Components of the spooling system

        input wells and output wells . These are two storage areas opened up on the disk . The input well simulates the disk during offline input, and is used to store the data input by the I/O device. The output well simulates the disk during offline output, and is used to store the output data of the user program. The data in the input/output well is generally organized and managed in the form of files, and we call these files well files . A file only stores the input or output data of one process, and the data input or output files of all processes are linked into an input or output queue .

        input buffer and output buffer . These are two buffers created in memory to ease the speed mismatch between the CPU and disk. The input buffer is used to temporarily store the data sent by the input device before sending it to the input well. The output buffer is used to temporarily store data transferred from the output well before being transferred to the output device.

        Input process and output process . The input process is used to simulate the peripheral control machine during offline input, and transfer the data required by the user from the input device to the input buffer, and then store it in the input well. When the CPU needs an input device, it reads directly from the input well into memory. The output process is used to simulate the peripheral control machine during offline output, transfer and store the data input by the user from the memory to the output well, and then output the data in the output well to the output device through the output buffer when the output device is free .

        Well Management Program . Used to control the exchange of information between jobs and disk wells. When an input or output operation request is sent to the equipment during operation execution, the operating system invokes the well management program, which controls the reading of information from the input well or the output of information to the output well.

2.4 - Using daemons

        Idea: In fact, whenever an exclusive device needs to be transformed into a device that can be shared by multiple processes, a daemon process and a spool file queue (directory) must be configured for the device . //Such as printer daemons, server daemons, network daemons, etc.

        Equally, daemon process is the unique process that allows to use this independent equipment, and all other processes can not use this equipment directly, can only write the usage requirement of this equipment in a file, be placed in the spool directory. The daemon process completes the requests of the processes for the device in turn according to the files in the directory , so that an exclusive device is transformed into a device that can be shared by multiple processes. // How the daemon works

// This idea is like executing a timing program, or communicating between two processes through an intermediate mailbox

Guess you like

Origin blog.csdn.net/swadian2008/article/details/131642289