In the previous article, we learned that we had a lot of sophisticated designs to allow flexible software to control computers. The CPU can parse and execute some column commands and command combinations to form a program. The program is loaded into the memory, and the CPU automatically performs one-by-one parsing and cleverly implements the command jump. At the same time, a device controller (device adapter) is cleverly introduced to control the device. The CPU and the device controller are connected through the bridge and bus and interact through the bus protocol. By interacting with the Device Manager, the CPU can read, write, and control devices. This looks perfect. Under this mechanism, we can write ever-changing software to use our hardware features. However, some problems are hidden.
Why do we need an interruption mechanism?
If we do not need to interact with external systems during software execution, the program will always be executed step by step, without intervention or human-computer interaction, there is indeed a mechanism above. However, we found that our requirements are not limited. For example, after we start the system, we sometimes want to access the Internet, sometimes listen to songs, and sometimes write a blog. This idea is random and unexpected, and the program cannot be known in advance. To address this need, we must provide human-computer interaction means. For example, during software execution, someone dynamically notifies the software about what to do for me.
How can we implement Human-Computer Interaction? The classic configuration is the introduction of input devices such as keyboards and mouse, and the introduction of output devices such as monitors. Some people may ask, if I/O control is introduced to the Device Manager of such devices, can the CPU not control them? Is there anything different? It's a little different. The previous method is suitable for reading and writing devices at any time, while the keyboard and mouse can only be read, and not every read has a value, only when a person writes a value in an occasional rise can he read it. Of course, we can read it once every other time, which does work, but is the efficiency a little low?
Therefore, we have introduced the idea of interrupt control for the control of such devices. One line on the device interface is the interrupt request line. When a keyboard click event occurs, the hardware will automatically trigger the interrupt request signal. This request signal is sent to the CPU. After the CPU executes each command, it will "sense" the request signal to interrupt. If yes, the interrupt processing process will be triggered. This is generally the case. First, save the current running environment, and then the hardware mechanism gets the requested interrupt and the interrupt number, find the corresponding interrupt handling program (software part) based on the interrupt number, and then assign the execution right to the interrupt handling program for execution. Restore the previous running environment after execution.
Keyboard interruption as an Example
Take the keyboard as an example. We can control it like this. First, the system should prepare a keyboard interrupt processing program. After a key event is triggered, the keyboard interrupt processing program will save the key event to the memory buffer. At the same time, when the program needs to obtain the key information, It queries the buffer to see if there is any key information, and keeps polling until there is button information. We found that polling is also required here, and there is an additional interrupt processing process. Is it meaningful? Of course. Previously, I/O space of the device controller was poll. Each query takes up the bus, which consumes many resources and is slow. Now I have poll the memory space, which is fast and does not occupy the I/O bus, reduces the burden on the system.
Through this article, we should have a preliminary understanding of the interrupt mechanism. As for how the software part uses the hardware interrupt mechanism to serve the application, it is within the scope of software architecture considerations. At the same time, we can feel the beauty of computer design.