Why does the OS event listening not occupy 100% of the CPU?

Source: Internet
Author: User
Tags signal handler sleep function
I tried to detect Keyboard Events in python by placing a function event. get () in pygame IN THE while loop. The result is that the cpu usage is always 100%. However, the event listening functions in the operating system and other languages (such as C #) basically do not occupy the cpu. How do they do this? Does it sacrifice the real-time response of events? I tried to detect Keyboard Events in python by placing a function event. get () in pygame IN THE while loop. The result is that the cpu usage is always 100%. However, the event listening functions in the operating system and other languages (such as C #) basically do not occupy the cpu. How do they do this? Does it sacrifice the real-time response of events? Reply: The event listening of the operating system is completed by collaboration with the CPU. This mechanism is called Hardware interruption(Interrupt ).
Under normal circumstances, the CPU follows its internal Program counter (Program counter ) Refers to the Instruction (Instruction ) In sequence, or if the instruction is a jump class instruction, such X86Class CALLOr JMP,Go to the place indicated by the instruction and continue the execution in sequence.
In only four cases, the CPU will not be executed sequentially, including the hardware interruptions mentioned above, Trap (computing) ), Fault, and Abort.

When the hardware is interrupted, the CPU executes the hardware interrupt processing function instead. Trap is x86 INTCommand, which is called by the kernel System (System call ), Which causes the CPU to jump to the system function (Exception Table) of the operating system kernel ). If the error is caused by a Page failure ), Or even memory protection error implementation depends on.

It means that the CPU will immediately put down the command at hand and then handle the four situations, after the four cases are processed, the CPU jumps back to the command to continue processing. This is why even if the single-core CPU occupies 100% to process another process task, as long as your process has a high priority, you can stop the CPU when a keyboard event occurs and then execute your process.

Why does listening for keyboard events not occupy 100% of the CPU or even 0 of the CPU? Because the status (Idle (CPU) ), Hardware interruption will still start the CPU to start executing the Interrupt processing function (Interrupt handler ).

In particular, when you press Ctrl + C, the keyboard hardware will interrupt the CPU, including an Exception Number ), before the CPU receives this exception code, it is called immediately by the operating system Kernel (operating system) The interrupt handler is a registered interrupt handler. The interrupt handler calls the keyboard driver in the kernel, and the keyboard driver calls the display terminal (Computer terminal ). ) The driver displays "^ C" on the terminal and uses the scheduler to wake up related processes (Process (computing) ). * Nix also generates signals (Unix signal ) To the current Process group ), These processes will execute SIGINTIn this case, the CPU runs from the Kernel Mode ) To the User mode ).

If your process uses Blocking to wait for a keyboard response before this event occurs, and the user does not have any program to execute. In most cases, the CPU is HLTThe command is switched to idle state, and will only be periodically awakened by the hardware interruption of the clock to see if the scheduler can do anything. When the keyboard time occurs, your process will be awakened, and the code will be returned from the wait function. You will also see on the screen that the CPU usage is still 0.

Reference: Computer Systems: A Programmer's Perspective (3rd Edition)

This problem involves two methods for the system to handle events. Let me organize the language. One isInterruptThe CPU interruption mechanism is used to remind the operating system of what happened. The other method requires the active operation of the operating system.PollingTo check whether an application has an event. The operating system generally provides services to applications through the CPU interrupt mechanism. The reason is obvious. In the second method, the operating system will continuously occupy CPU resources to check whether there is any event. In addition, the second method has poor real-time performance. Imagine that a user program can enter the kernel only after it is segmented by timer interrupt, and then the State of all the processes will be poll one by one after the kernel processes some tasks, the latency in the middle must be much larger than directly triggering an interrupt.


There are four types of CPU interrupt mechanisms,Trap,Fault,Interrupt,Abort. There are very subtle differences between them. These four types of interruptions can be divided into two categories. One is synchronization, including trap, fault, and abort, collectively referred to as exception. The other is asynchronous interrupt. Trap is a mechanism for applications to actively apply for services from the system, and syscall is one of them. Therefore, we generally say a user process traps into kernel. Abort refers to some unexpected situations in program execution, which usually cannot be restored, such as hardware errors. Fault also refers to some abnormal conditions in program execution, but it can usually be resumed, such as page fault. At last, interrupt is usually triggered by external input hardware, such as the keyboard, mouse, touch screen, and timer. For more detailed explanations, see section 8.1 of the second version of CSAPP.


To describe why the system listens to Keyboard Events with almost no CPU resources, you must explain how the computer system handles them.External interrupt. Suppose we ran a program in shell and then press Ctrl + C to exit. First, when we press Ctrl + C, the keyboard controller will initiate an interrupt and forward it to the CPU. CPU view its ownIDT(Interrupt descriptor table) to find the interrupt handler corresponding to keyboard interrupt. The CPU will automatically set the stack pointer of the process (shell) that triggers keyboard interrupt, instruction pointer and other information are pushed to the kernel stack, and then transferred to the kernel stack and the previously found interrupt handler to start execution. The kernel will continue to push all the register values for all the remaining interruptions to the kernel stack to form a trapframe, so that the shell can be resumed after the interruption is processed. Then, the kernel selects an interrupt handler encapsulated in the kernel based on the interrupt information given by the CPU to continue the execution. The interrupt handler will get the shell context and then sendINTSignal and run shell again. After the shell receives this signal, it will go to its ownINTSignal handler continues. Then the handler will call a system callKillTo close the sub-process running on foreground (the process using the keyboard must run on foreground), and then wait for the user to enter the next command. So farHardware + operating system + shellThe process of cooperating with each other to process a keyboard input is over. This process omitted thousands of characters =, =. It seems complicated, but the system is like this, ensuring that hardware resources are shared while maximizing efficiency, and there is also a sufficient protection mechanism =, =


EDIT: modified the second paragraph's explanation of the four interrupt mechanisms, which is more accurate. However, these terms are not completely unified. I think we can understand how to handle a certain interruption, such as page fault, external interrupt, syscall, and pide by zero, without worrying about the terms.

Because the function you call is not blocked, and there is no waiting, it will occupy the cpu.

If round-robin occurs, insert wait in the loop (sleep is one of them), in other words, let the cpu rest.

If a Round-Robin Takes 0.1 milliseconds and the wait time is increased by 10 milliseconds, the CPU will rest for 10 milliseconds every 0.1 milliseconds. In this way, the cpu usage will be reduced to 1%.

The above is a very intuitive explanation. Polling or interruption can be encapsulated into blocking calls. That is to say, you don't have to deal with the problem of how to let the cpu rest. When there is no message, it will always block the rest of the function, the result must be returned when the function returns. If the function you are using is blocked, you do not need to consider cpu usage.

Blocking calls solve the problem of cpu Full load, because they already contain operations related to cpu rest.

However, blocking calls give rise to a new question: how to query multiple different messages at the same time? How can I round robin multiple different things at the same time?

So you accidentally discovered the core of asynchronous communication. To put it simply, it is generally to listen to multiple sources in a function at the same time. Any type of message is fed back to you. If the function is blocked, it must return a message.

But sometimes you want to add a bit of private goods in addition to messages, so the function will not be blocked. In this case, you still need to add a wait in the loop to avoid full cpu load.

The asynchronous event processing machine is made mainstream for modern Server programming, because only the asynchronous processing mechanism can process a large number of requests in a short period of time, but it does not occupy resources, multi-threaded/multi-process mechanisms cannot be implemented.

In a word: non-blocking round robin. Please wait. Do not want to wait. Use blocking call. Round Robin of multiple events at the same time and asynchronous event processing mechanism is used. There are two ways to monitor a person's whereabouts:
1. Don't let him know. You stare at him all day long. This will take up your time, but it has no effect on him. This is called polling.
2. tell him that you pay attention to him and ask him to notify you when he does the actions you care about. This takes almost no time, but for him, there will be one more thing that is not too troublesome. This is called to interrupt the business of my previous job company. My team lead not only needs to deal with the affairs of this group, to respond to the needs of other groups. This is the background.
At first, we sent emails to communicate with each other. The team leader's work method is: to deal with a round of tasks in this group, check the emails to see the external requirements, and then process the tasks in this group after the emails are processed, and directly process the tasks in this group if no emails are found. This is called round robin.
Suddenly one day, the manager was in a hurry to find him and sent an email to him. It took about half an hour to get a response. The big leaders were very angry and I had a chat with the team lead. He summed up the lesson and found that some things should be responded in time, so he told people who might be in urgent need to call in the future. After receiving the call, he will put down his work and immediately handle the call demand. This is called an interruption.
Then one day, HR called him and asked him to pick up the materials at HR and hand them over to HR. He ran to pick up the materials, and was told to handle it within three days when handing it over to HR. He thought about it carefully. Some of the first tasks were very urgent, such as getting a table. They were not so urgent and troublesome. When receiving some calls, he immediately finishes processing the first half, then drops it and chooses to handle it. This is called separation of hard interrupt and Soft Interrupt.
Then... There are too many express deliveries in the company. The big leader asked him to take delivery from everyone. As a result, he received calls from numerous couriers in a day. Tired to death. This is called an interruption storm.
So the next day he gave a greeting to the master of the house and asked the master to temporarily store the parcel. When the first parcel arrived, he made a call to him. He wrote down the incident in the first half. Don't bother him if he doesn't deliver anything in a day. He took the one-day express delivery from the warehouse when he got off work. This is called Soft Interrupt polling.
So the subject, as mentioned above, gets a response in a more timely manner than polling when busy. What you did was keep refreshing emails when I was so smart as a group leader ...... Yes, it sacrifices the timeliness of event processing. The other two answers are correct. event detection relies on polling or interruption. I will explain why your code occupies 100% CPU:
Taking the CPU usage display in the Win job manager as an example, we know that Windows implements multithreading/process by assigning time slices to each thread for execution in turn, each time slice is about several milliseconds to dozens of milliseconds. By default, the Win8 Task Manager draws the CPU usage curve at a speed of 1 second, that is, the percentage of time used by the system idle process in the past 1000ms. If your Python uses the while loop, the thread will always use the allocated time slice, so the CPU usage will be 100%. In fact, you only need to add a time. sleep (0.001) while the CPU can be lowered.
The sleep function tells the operating system that this thread will discard the currently unused time slice and do not assign time slice to me in the next specified time. In fact, the processes in the entire operating system are waiting for sleep for most of the time. After a while, check for user input, message delivery, and other events, so the CPU usage is not high. The more sleep, the lower the CPU usage per unit time. There are two ways to monitor whether an event occurs. One is polling, the other is interruption, and the other is in a Windows environment, which can also be called a hook, understand it ).

Round Robin is the method you write in python, A while review keeps checking and keeps asking: No, no......

What does a hook mean? In Windows, any event is sent to all Windows in the form of event broadcast. The window receives the event and then processes it. For a key (keyboard) event, the general process is like this (the process of the XP-WIN7 era ):

1) Hardware interruption/hardware port data
// WinIO can be simulated, or the IDT can be modified at this layer.
2) keyboard Port Drive (USB or PS/2)
// Filter Driver
// KeyboardClassServiceCallback is also called at this layer
3) kbdclass driver
// Handle keyboard layout and keyboard language. Some high-end viruses also work here
4) Windows Kernel boundary (zwCreate/zwReadFile)
---------------------- (System Call )----------------------
5) Windows Kernel boundary (zwCreate/zwReadFile)
Win32k of 6)csrss.exe! RawInputThread reads and converts scancode and vk.
// Setmediawhook works here (global)
// Kbd_event works here
7)csrss.exe calls functions such as DispatchMessage to send messages ( Broadcasting keyboard messages starts here)
// Setmediawhook works here (process)
// PostMessage and SendMessage are available here
8) various processes (window threads) process messages

In step 1, if a hook is attached, all common key messages will be received theoretically. When no key message is sent, this hook function will not be called and executed, so it will not occupy the CPU either.

The same principle applies to other hooks: The event has not occurred, and the hook has not been executed, so it does not occupy the CPU.. Don't call me, I will call you... to add, in fact, interruption is necessary, but not adequate. Imagine if there is no interrupt source event except the clock, the CPU is still running. The difference is that you can set it to a low-power state at this time. Think of the timer latency and hard code latency in the previous Single-Chip Microcomputer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.