如何限制特定进程的CPU使用率?

发布于 2024-10-30 19:32:12 字数 49 浏览 0 评论 0原文

我如何将 cpu 使用率限制为 10%,例如 Windows C++ 中的特定进程?

How i can limit cpu usage to 10% for example for specific process in windows C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一个人的旅程 2024-11-06 19:32:12

您可以使用 Sleep(x) - 会减慢程序执行速度,但会释放 CPU 周期
其中 x 是以毫秒为单位的时间

You could use Sleep(x) - will slow down your program execution but it will free up CPU cycles
Where x is the time in milliseconds

﹏半生如梦愿梦如真 2024-11-06 19:32:12

这是很少需要的,也许线程优先级是更好的解决方案,但既然你问了,你应该做的是:

  1. 做你的“扎实”工作的一小部分,即计算
  2. 测量步骤 1) 花费的时间,假设它是 twork< /code> 毫秒
  3. Sleep() for (100/percent - 1)*twork 毫秒,其中 percent 是您所需的负载,
  4. 返回到 1。

为了使其正常工作,您必须非常小心地选择计算的“部分”有多大,并且某些任务很难拆分。单个分数应该花费 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:

  1. do a small fraction of your "solid" work i.e. calculations
  2. measure how much time step 1) took, let's say it's twork milliseconds
  3. Sleep() for (100/percent - 1)*twork milliseconds where percent is your desired load
  4. go back to 1.

In 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.

江城子 2024-11-06 19:32:12

这是操作系统的工作,你无法控制它。

That is the job of OS, you can not control it.

猛虎独行 2024-11-06 19:32:12

您无法精确限制为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文