editorloop 占用_为什么一个无限循环会使 CPU 占用达到 100%

引言

之前用 while 写了一个无限循环的脚本会不停占用 CPU,直到 100%,当时提到了原因与解释。

这里重新复习理解下。

分析与解释

一个空的死循环不会做任何事情,但是会不断向 CPU 申请时间片,直到把每一片时间都占用完。

这样 CPU 就没有空余的时间片来做其他有用的事情了。

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.

Ref

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值