I / O interrupt principle



I / O interrupt principle

Foreword

In Windows kernel principle - synchronous and asynchronous IO IO and "high performance network communications principle" two articles, have emerged interrupt this term. This article will interrupt the operation principle will be described.

What is interrupted

Interrupt means that when the need arises, CPU temporarily stops execution of the current program instead of implementation, and execution process of the new situation. That program is running, the system appeared to be a case processed by the CPU immediately, this time, the CPU temporarily suspend the implementation of the program in favor of the new situation to deal with this process is called the interrupt.

We know that CPU instruction is executed in the order, the operating system approximately every 15ms will once thread scheduling (under Windows) occurred, according to a high-priority thread to thread priority scheduling. But the reality is not so simple, if we receive a request to the network, only to deal with the network if the request after executing the current thread to wait 15ms or thread scheduling, network card buffers is likely to be filled at this time occurs packet loss.

Interrupt type

Interrupts are divided into hardware interrupts and software interrupts.

Hardware interrupt

Hardware interrupt signal that is sent by the hardware, such as I / O interrupts and hardware interrupts failure.

  • I / O Interrupts: generated by the I / O controller for transmitting signaling signals and the like to complete the operation.
  • Hardware failure interrupts: failure such as power failure or memory parity error and the like.

Software interrupts

Software interrupt signal is the non-hardware issue, such as a program interruption and clock interrupts.

  • Interrupt procedure: Exception generated some instructions (e.g., arithmetic removed, the divisor is 0, etc.).
  • Clock Interrupt: generated by the internal timer of the processor, allowing the operating system to perform certain procedures function.

    We mentioned about the operating system every 15ms will conduct a thread scheduling, is the use of the clock interrupt to achieve.

I / O interrupt process

This article is mainly explained mentioned in previous articles I / O interrupt explanation, so only I / O interrupts, for example, but the interruption of the principles and processes are similar.

I / O interrupts the processor performs interrupt operation by an interrupt. When the external device I / O module is ready, it sends an interrupt signal to the CPU, CPU will "immediately" respond to pause the current program processing program to serve the I / O devices.

May not be immediate, such as the simultaneous presence of multiple interrupt, the interrupt according to the actual decision algorithm is interrupted by the order to execute the interrupt operation, or press to interrupt priority.
When the I / O interrupt hardware interrupt, requires hardware support to receive an interrupt signal.

Without interruption

In order to better explain the interruption caused by performance, we first describe how the program does not handle I / O operation is interrupted.

20191214121307.png

  • When we need a program to read a file from the hard disk, the data will first check whether there is the kernel cache, if there is no data, then perform the actual I / O operations. When I / O operations performed, our users thread blocks waiting for data from the hard disk is written to memory. For users, the thread is blocked.
  • In the course of the actual I / O operation, if not interrupt operation, CPU will continue to check the polling I / O operation is complete whether, if the I / O operation is not completed then proceeds to schedule other threads, check back later. When the operation is complete, CPU to the thread, the thread in the ready queue and restore the thread context information.
  • Thread in the ready queue, the OS scheduler can be executed to continue the read operation, it will read the data from the operating system kernel buffer to the user buffer.

Interruptions

20191214121702.png

  • When we need a program to read a file from the hard disk, the data will first check whether there is the kernel cache, if there is no data, then perform the actual I / O operations. When I / O operations performed, our users thread blocks waiting for data from the hard disk is written to memory. For users, the thread is blocked.
  • In the actual process of the I / O operation, the CPU sends a read command to the I / O module (DMA controller), and then went to schedule other threads.
  • When the I / O module (DMA controller) I / O execution is completed, an interrupt signal is generated in the notification CPU, CPU to the thread, the thread in the ready queue and restore the thread context information.
  • Thread in the ready queue, the OS scheduler can be executed to continue the read operation, it will read the data from the operating system kernel buffer to the user buffer.

It can be seen, there is no break or interruption for the user thread is blocked, the operating system kernel, proactive notification interrupt the CPU by way of reducing the thread polling judgment, improve the efficiency of the thread.
Of course, in order to further improve the utilization of the thread, then we can perform I / O operations through the asynchronous operation API.
For example, the .Net4.5 asyncand awaitkeywords, when the call asynchronous operation, the internal state machine API preserve relevant information (callback information), the thread continues to perform other operations, when the operating system kernel to read data is completed, the thread calls the callback method to recover the awaitsubsequent operations. Throughout the process thread it will not clog bring cause performance loss.

Interrupt processing

When the I / O devices complete an I / O operation, the following events occur:

  • Before the start I / O operation, the processor processing, such as information about the current instruction address, saved to stack the necessary state information, etc., so that execution can be resumed after the interruption.
  • After the I / O operation is complete, the device sends an interrupt signal to the processor.
  • Processor responds to the interrupt signal.
  • The processor determines the interrupt signal, if not the interrupt response is present, the transmission apparatus to generate an interrupt acknowledgment signal, the acknowledgment signal causes the device to cancel its interrupt signal.
  • The processor before the interrupt routine transfers control to the interrupt program previously stored information acquired from the stack, so that subsequent operations can proceed when the I / O completion.
  • Processor interrupt routine entry address is loaded into the program counter, so that the processor can continue to execute the next instruction cycle.

Related documents

  1. "Operating system - the essence of the design principles"
  2. Clock interrupt thread scheduler is the driving force of the rt-thread

20191127212134.png
Micro-channel sweep the two-dimensional code technology sharing Jiege attention subscription number
Source: https://www.cnblogs.com/Jack-Blog/p/12038716.html
Author: Jiege busy
As used herein, "CC BY 4.0" Creative Commons protocol. Welcome to reprint, please indicate the source and link in a prominent location.

Guess you like

Origin www.cnblogs.com/Jack-Blog/p/12038716.html