如何限制特定进程的CPU使用率?
我如何将 cpu 使用率限制为 10%,例如 Windows C++ 中的特定进程?
How i can limit cpu usage to 10% for example for specific process in windows C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
Sleep(x)
- 会减慢程序执行速度,但会释放 CPU 周期其中 x 是以毫秒为单位的时间
You could use
Sleep(x)
- will slow down your program execution but it will free up CPU cyclesWhere x is the time in milliseconds
这是很少需要的,也许线程优先级是更好的解决方案,但既然你问了,你应该做的是:
Sleep()
for(100/percent - 1)*twork
毫秒,其中percent
是您所需的负载,为了使其正常工作,您必须非常小心地选择计算的“部分”有多大,并且某些任务很难拆分。单个分数应该花费 40 到 250 毫秒左右,如果花费的时间更少,睡眠和测量的开销可能会变得很大,如果花费的时间更多,使用 10% CPU 的错觉就会消失,看起来你的线程正在运行。在 0 到 100% CPU 之间振荡(无论如何都会发生这种情况,但如果你做得足够快,那么看起来你只占用了任何百分比)。另外需要注意的两件事:首先,正如我之前提到的,这是在线程级别而不是进程级别;其次,你的工作必须是真正的CPU工作,磁盘/设备/网络I/O通常涉及大量等待并且不会占用太多CPU。
This is rarely needed and maybe thread priorities are better solution but since you asked, what you should is:
twork
millisecondsSleep()
for(100/percent - 1)*twork
milliseconds wherepercent
is your desired loadIn order for this to work well, you have to be really careful in selecting how big a "fraction" of a calculation is and some tasks are hard to split up. A single fraction should take somewhere between 40 and 250 milliseconds or so, if it takes less, the overhead from sleeping and measuring might become significant, if it's more, the illusion of using 10% CPU will disappear and it will seem like your thread is oscillating between 0 and 100% CPU (which is what happens anyway, but if you do it fast enough then it looks like you only take whatever percent). Two additional things to note: first, as mentioned before me, this is on a thread level and not process level; second, your work must be real CPU work, disk/device/network I/O usually involves a lot of waiting and doesn't take as much CPU.
这是操作系统的工作,你无法控制它。
That is the job of OS, you can not control it.
您无法精确限制为 10%,但您可以降低其优先级并将其限制为仅使用一个 CPU 核心。
You can't limit to exactly 10%, but you can reduce it's priority and restrict it to use only one CPU core.