为什么意外的无限循环会增加CPU使用率? [英] why does an infinite loop of the unintended kind increase the CPU use?

查看:196
本文介绍了为什么意外的无限循环会增加CPU使用率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道对意外类型的无限循环通常会导致较高的CPU使用率.但是,我不太明白为什么.有人可以向我解释吗?

解决方案

CPU在执行该循环(永远不会结束)时无法执行其他任何操作.即使您正在使用抢先式多任务系统(这样无限循环将永远永远阻塞其自己的进程或线程),每次操作系统的抢先式调度程序将其占用"其时间片时,下一个切片的CPU-什么也不做,但是每次都消耗一个切片的CPU时间,因此,很多其他线程会损失很多CPU,这本来可以做有用的工作. /p>

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?

解决方案

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使用率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆