What is the computer doing when it freezes

Hits: 0

Hello everyone, I’m the factory manager.

Take a few minutes today to share with you a very interesting and knowledge-enhancing question: what are you doing when your computer freezes?

Computer crashes, it should be experienced by every friend who comes into contact with computers.

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

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

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

Therefore, in theory, there are two types of crashes. One is that the CPU has stopped working, and the next instruction will not be executed. This is a hardware crash, but this situation will basically not happen.

It is more of a crash at the software level, that is, the CPU is trapped somewhere and cannot come out, resulting in the program that should be executed not being executed, and it seems to be a crash.

If you are smart, you may immediately think that if you write an infinite loop and trap the CPU in it and cannot get out, 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 will still work normally and not freeze.

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

This has to mention a concept: interruption .

interrupt

The concept of interruption is definitely one of the greatest inventions in the history of computing.

Interrupts, as the name suggests, are used to interrupt the normal work of the CPU and allow it to execute instructions elsewhere.

The reason why the operating system can control the overall situation is due to a series of interrupt processing functions (such as our most common clock interrupt) that are inserted into the CPU when it starts, so that the operating system can periodically reclaim the execution right of the CPU and schedule another thread 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.

If you want to use an infinite loop to kill the computer, it is naturally impossible.

In fact, if you think about it, if it kills you so easily, then this operating system is too bad. If a novice is just learning programming, can’t force restart the computer more than a dozen times a day?

Stepping back 10,000 steps, even if the impact of interrupts is not considered, most of the current CPUs are multi-core, and one thread enters an infinite loop, but there are other cores that can participate in system scheduling, and they will still not crash.

So back to the question at the beginning, where is the CPU stuck and can’t get out, and even interrupts can’t do anything about it?

There are actually two cases:

1. Interrupt really can’t take it

Many people know the concept of interrupts, but many people do not know that interrupts also have priorities.

This is easy to understand, such as the CPU is executing a program, and suddenly an interrupt event occurs. The CPU saves the current execution context and turns around to process the interrupt event, but just halfway through the process, a new interrupt event comes, so what should the CPU do? Do you want to respond?

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

With this premise, imagine what will happen if you fall into an infinite loop when processing an interrupt, such as a spin lock, due to improperly written operating system kernel code?

Because when processing interrupts, the CPU runs at a very high priority, and ordinary interrupts cannot grab the execution right, which causes the CPU core to become a “vegetative person”, no matter how it is called it does not respond. .

2. The interrupt can grab the CPU, but it is found that no thread can be scheduled

Programmers should not be unfamiliar with the concept of deadlock. Two threads A wait for B, B waits for A, and the two threads wait for each other to make concessions (release locks), forming a deadlock situation, which eventually becomes a deadlock.

If the deadlock occurs at the application level, it is not a big problem, at most two programs 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 threads that want it must enter the waiting queue, which is cool.

Quoting a description from another great god, Tim Chen (I think it is a special image):

Hello, I am Tan Qingbo, the director of the factory. I am studying in Harbin Institute of Technology. I am a student of computer science. I have been studying for 20 years. From rural areas to C9 doctorate, click the blue word to see my growth path.
At the same time, I am also a Zhihu blogger: Tan Qingbo, who shares the knowledge of high-quality programmers, learning materials, technical dry goods, etc. on a daily basis. Welcome to watch.

Finally, an important thing, I recently established a [Promoted Programmers] community, which aims to help novice Xiaobai quickly get started and find the fun of programming. It has accumulated a lot of programming-related experience tutorials and learning materials. At present, the community The number of people has exceeded 2500, everyone is welcome to join:

You may also like...

Leave a Reply

Your email address will not be published.