为什么无意的无限循环会增加CPU的使用?

时间:2023-02-12 08:46:44

I know an infinite loop to the unintended kind usually causes a high CPU usage. But, I don't quite understand why. Can anyone explain that to me?

我知道无意识的无限循环通常会导致高CPU使用率。但是,我不太明白为什么。任何人都可以向我解释一下吗?

4 个解决方案

#1


19  

The CPU cannot do anything else while it's executing that loop (which never ends). Even if you're using a pre-emptive multi-tasking system (so that infinite loop will only clog forever its own process or thread), the loop will "eat" its time slice each time the OS's pre-emptive scheduler hands it the CPU for the next slice -- doing nothing, but eating up one slice's worth of CPU time each and every time, so that much CPU is lost to all other threads which could otherwise be doing useful work.

CPU在执行该循环(永不结束)时无法执行任何其他操作。即使你正在使用一个先发制人的多任务系统(这样无限循环只会永远阻塞它自己的进程或线程),每次操作系统的先发制人调度程序交给它时,循环都会“吃掉”它的时间片。用于下一个切片的CPU - 什么都不做,但每次都吃掉一片片的CPU时间,这样就会给所有其他线程丢失很多CPU,否则这些线程可能会做有用​​的工作。

#2


6  

Infinite loops are no different than any other code running. The computer doesn't know that the infinite loop isn't a complicated calculation that just requires a lot of iterations.

无限循环与运行的任何其他代码没有什么不同。计算机不知道无限循环不是一个复杂的计算,只需要大量的迭代。

Unless the infinite loop contains code that calls some system functions that yield time back to the OS, the OS treats it as a process that is actively working on something and gives it time to execute. If no other processes are running, it will eat up 100% of the CPU (on a single core system).

除非无限循环包含调用某些系统函数的代码,这些函数会将时间返回给操作系统,否则操作系统会将其视为正在积极处理某些事情并为其提供执行时间的进程。如果没有其他进程正在运行,它将占用100%的CPU(在单核系统上)。

#3


6  

Infinite loops in themselves aren't a problem at all. Most applications that interact with a user are infinite loops. They repeatedly wait for user, act on it, and perform the cycle again. The operating system itself is an infinite loop. These kinds of infinite loop are said to be 'productive' because despite repeating something indefinitely, they periodically output something useful to the user.

无限循环本身并不是问题。与用户交互的大多数应用程序都是无限循环。他们反复等待用户,对其采取行动,然后再次执行循环。操作系统本身是一个无限循环。这些无限循环被认为是“有效的”,因为尽管无限期地重复某些东西,它们会定期输出对用户有用的东西。

I guess your concern is with unproductive infinite loops. But it's clear why these are a problem. They have all of the disadvantages of productive loops (consume power, use CPU time and so on) with none of the advantages ie. they don't produce anything useful.

我想你关心的是无效的无限循环。但很清楚为什么这些都是一个问题。它们具有高效循环(消耗功率,使用CPU时间等)的所有缺点,没有任何优点,即。他们没有产生任何有用的东西。

#4


0  

You are probably referring to the Halt and Catch Fire instruction

你可能指的是Halt和Catch Fire指令

#1


19  

The CPU cannot do anything else while it's executing that loop (which never ends). Even if you're using a pre-emptive multi-tasking system (so that infinite loop will only clog forever its own process or thread), the loop will "eat" its time slice each time the OS's pre-emptive scheduler hands it the CPU for the next slice -- doing nothing, but eating up one slice's worth of CPU time each and every time, so that much CPU is lost to all other threads which could otherwise be doing useful work.

CPU在执行该循环(永不结束)时无法执行任何其他操作。即使你正在使用一个先发制人的多任务系统(这样无限循环只会永远阻塞它自己的进程或线程),每次操作系统的先发制人调度程序交给它时,循环都会“吃掉”它的时间片。用于下一个切片的CPU - 什么都不做,但每次都吃掉一片片的CPU时间,这样就会给所有其他线程丢失很多CPU,否则这些线程可能会做有用​​的工作。

#2


6  

Infinite loops are no different than any other code running. The computer doesn't know that the infinite loop isn't a complicated calculation that just requires a lot of iterations.

无限循环与运行的任何其他代码没有什么不同。计算机不知道无限循环不是一个复杂的计算,只需要大量的迭代。

Unless the infinite loop contains code that calls some system functions that yield time back to the OS, the OS treats it as a process that is actively working on something and gives it time to execute. If no other processes are running, it will eat up 100% of the CPU (on a single core system).

除非无限循环包含调用某些系统函数的代码,这些函数会将时间返回给操作系统,否则操作系统会将其视为正在积极处理某些事情并为其提供执行时间的进程。如果没有其他进程正在运行,它将占用100%的CPU(在单核系统上)。

#3


6  

Infinite loops in themselves aren't a problem at all. Most applications that interact with a user are infinite loops. They repeatedly wait for user, act on it, and perform the cycle again. The operating system itself is an infinite loop. These kinds of infinite loop are said to be 'productive' because despite repeating something indefinitely, they periodically output something useful to the user.

无限循环本身并不是问题。与用户交互的大多数应用程序都是无限循环。他们反复等待用户,对其采取行动,然后再次执行循环。操作系统本身是一个无限循环。这些无限循环被认为是“有效的”,因为尽管无限期地重复某些东西,它们会定期输出对用户有用的东西。

I guess your concern is with unproductive infinite loops. But it's clear why these are a problem. They have all of the disadvantages of productive loops (consume power, use CPU time and so on) with none of the advantages ie. they don't produce anything useful.

我想你关心的是无效的无限循环。但很清楚为什么这些都是一个问题。它们具有高效循环(消耗功率,使用CPU时间等)的所有缺点,没有任何优点,即。他们没有产生任何有用的东西。

#4


0  

You are probably referring to the Halt and Catch Fire instruction

你可能指的是Halt和Catch Fire指令