What is the CPU doing when the computer freezes?

A few days ago, someone in the group chatted about a question:Why does the computer crash? What is the CPU doing when it crashes?

Computer crashes should be experienced by everyone who comes into contact with computers.

Especially in the early years, when the computer configuration was not as high as it is now, if you open a few heavyweight applications, the crash will come as scheduled. Even if you press CTRL + ALT + DELETE on the keyboard, the task manager will I couldn’t get out, so in the end I could only press and hold the shutdown button silently with tears in my eyes to force a shutdown.

So, have you ever thought about what the computer is doing when it crashes?

As we all know, the core of the computer is the central processing unit (CPU). Students who have taken basic computer courses have been taught that the CPU is a brainless machine that only knows how to fetch instructions and execute them one by one until it shuts down.

So in theory, there are two types of crashes. One is that the CPU stops working and no longer executes the next instruction. This is a hardware crash, but this situation basically does not happen.

It is more of a crash at the software level, that is, the CPU is trapped in a certain place and cannot get out, causing the program that should be executed not to be executed, and it looks like it has crashed.

If you are smart, you may immediately think that if you write an infinite loop and trap the CPU in it, will it crash? For example:

void dead_loop() {
  while (1) {
    ...
  }
}

You can try it and you will find that the fan cooling the CPU may spin up, but the computer can still work normally and does not freeze.

Infinite loop, shouldn’t the CPU keep spinning here? Won’t it crash?

This requires mentioning a concept: Interruption.

Interruption

The concept of interrupt is definitely one of the greatest inventions in the history of computers.

Interrupt, as the name suggests, is used to interrupt the normal work of the CPU and let it execute instruction programs elsewhere.

The reason why the operating system can control the overall situation is due to the series of interrupt processing functions it arranges for the CPU when it starts (such as our most common clock interrupt), so that the operating system can periodically take back the execution rights of the CPU and schedule Other threads to execute.

Therefore, even if one of your threads enters an infinite loop, after your time slice is used up, you have to hand over the CPU obediently and let other programs execute it.

It is naturally impossible to crash the computer using an infinite loop.

Actually, thinking about it, if it crashes your computer so easily, then the operating system is too bad. If a novice just learns programming, he has to force restart the computer more than ten times a day?

Taking a step back, even if the impact of interrupts is not considered, most current CPUs have multiple cores. If a thread enters an infinite loop, there are other cores that can participate in system scheduling and it will still not crash.

So back to the original question, where is the CPU trapped and unable to get out? Even interrupts can’t do anything about it?

There are actually two situations:

1. There is really nothing you can do about interruption

Many people know the concept of interrupts, but many people don’t know that interrupts also have priorities.

This is easy to understand. For example, the CPU is executing a program and an interrupt event suddenly occurs. The CPU saves the current execution context and turns around to handle the interrupt event. But just halfway through processing, a new interrupt event comes. What should the CPU do? Do you want to respond?

Therefore, interrupts also have priorities. Low-priority interrupts cannot interrupt high-priority interrupts.

With this premise in mind, imagine what would happen if the operating system kernel code was improperly written and fell into an infinite loop when processing an interrupt, such as a spin lock?

Because when processing interrupts, the CPU runs at a very high priority, and ordinary interrupts cannot take over the execution rights. This causes the CPU core to become a “vegetative state” and no matter how you call it, it does not respond. .

2. The interrupt can grab the CPU, but it is found that there is no thread to schedule

Programmers should be familiar with the concept of deadlock. Two threads A wait for B and B waits for A. The two threads wait for each other to give in (release the lock), forming a stalemate and eventually becoming a deadlock.

If the deadlock occurs at the application level, it is not a big problem. At most, two programs will die. But what if it happens in the kernel?

For example, in the Windows operating system kernel, there are a large number of global locks. If one accidentally causes a deadlock, other interested threads will have to enter the waiting queue, which will be a disaster.

Quoting a description from another great master, Tim Chen (I find it particularly vivid):


———————————END——————- ——–

Digression

In this era of big data, how can you keep up with scripting without mastering a programming language? Python, the hottest programming language at the moment, has a bright future! If you also want to keep up with the times and improve yourself, please take a look.

Interested friends will receive a complete set of Python learning materials, including interview questions, resume information, etc. See below for details.

1. Python learning routes in all directions

The technical points in all directions of Python have been compiled to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the following knowledge points to ensure that you learn more comprehensively.

img
img

2. Essential development tools for Python

The tools have been organized for you, and you can get started directly after installation! img

3. Latest Python study notes

When I learn a certain basic and have my own understanding ability, I will read some books or handwritten notes compiled by my seniors. These notes record their understanding of some technical points in detail. These understandings are relatively unique and can be learned. to a different way of thinking.

img

4. Python video collection

Watch a comprehensive zero-based learning video. Watching videos is the fastest and most effective way to learn. It is easy to get started by following the teacher’s ideas in the video, from basic to in-depth.

img

5. Practical cases

What you learn on paper is ultimately shallow. You must learn to type along with the video and practice it in order to apply what you have learned into practice. At this time, you can learn from some practical cases.

img

6. Interview Guide

Resume template

If there is any infringement, please contact us for deletion