编写代码使CPU使用率显示正弦波

发布于 2024-07-13 07:16:18 字数 212 浏览 7 评论 0原文

用您最喜欢的语言编写代码 并让 Windows 任务管理器代表 CPU 使用历史记录中的正弦波。

这是微软中国的技术面试测验。 我认为这是一个好问题。 尤其值得了解的是候选人如何理解并找出解决方案。

编辑:如果可能涉及多核(cpu)情况,这是一个好点。

Write code in your favorite language
and let Windows Task Manager represent
a sine wave in CPU Usage History.

This is a technical interview quiz from Microsoft China.
I think it's a good question. Especially it's worth knowing how candidate understand and figure out the solution.

Edit: It's a good point if may involve multi-core(cpu) cases.

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

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

发布评论

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

评论(5

筱武穆 2024-07-20 07:16:18

Windows 中的线程时间片为 40 毫秒,iirc,因此这可能是用作 100% 标记的好数字。

unsigned const TIME_SLICE = 40;
float const PI = 3.14159265358979323846f;
while(true)
{
    for(unsigned x=0; x!=360; ++x)
    {
        float t = sin(static_cast<float>(x)/180*PI)*0.5f + 0.5f;
        DWORD busy_time = static_cast<DWORD>(t*TIME_SLICE);
        DWORD wait_start = GetTickCount();
        while(GetTickCount() - wait_start < busy_time)
        {
        }
        Sleep(TIME_SLICE - busy_time);    
    }
}

这将给出大约 14 秒的时间。 显然,这假设系统中没有其他重要的 CPU 使用情况,并且您仅在单个 CPU 上运行它。 这些在现实中都不那么常见。

A thread time slice in Windows is 40ms, iirc, so that might be a good number to use as the 100% mark.

unsigned const TIME_SLICE = 40;
float const PI = 3.14159265358979323846f;
while(true)
{
    for(unsigned x=0; x!=360; ++x)
    {
        float t = sin(static_cast<float>(x)/180*PI)*0.5f + 0.5f;
        DWORD busy_time = static_cast<DWORD>(t*TIME_SLICE);
        DWORD wait_start = GetTickCount();
        while(GetTickCount() - wait_start < busy_time)
        {
        }
        Sleep(TIME_SLICE - busy_time);    
    }
}

This would give a period of about 14 seconds. Obviously this assumes there is no other significant cpu usage in the system, and that you are only running it on a single CPU. Neither of these is really that common in reality.

浮生面具三千个 2024-07-20 07:16:18

这是一个稍微修改过的@flodin的解决方案< Python 中的 /a>:

#!/usr/bin/env python
import itertools, math, time, sys

time_period = float(sys.argv[1]) if len(sys.argv) > 1 else 30   # seconds
time_slice  = float(sys.argv[2]) if len(sys.argv) > 2 else 0.04 # seconds

N = int(time_period / time_slice)
for i in itertools.cycle(range(N)):
    busy_time = time_slice / 2 * (math.sin(2*math.pi*i/N) + 1)
    t = time.perf_counter() + busy_time
    while t > time.perf_counter():
        pass
    time.sleep(time_slice - busy_time);    

可以使用 time_periodtime_slice 参数微调 CPU 曲线。

Here's a slightly modified @flodin's solution in Python:

#!/usr/bin/env python
import itertools, math, time, sys

time_period = float(sys.argv[1]) if len(sys.argv) > 1 else 30   # seconds
time_slice  = float(sys.argv[2]) if len(sys.argv) > 2 else 0.04 # seconds

N = int(time_period / time_slice)
for i in itertools.cycle(range(N)):
    busy_time = time_slice / 2 * (math.sin(2*math.pi*i/N) + 1)
    t = time.perf_counter() + busy_time
    while t > time.perf_counter():
        pass
    time.sleep(time_slice - busy_time);    

A CPU-curve can be fine-tuned using time_period and time_slice parameters.

怎会甘心 2024-07-20 07:16:18

好吧,我有一个不同的,可能比我的第一个答案更好的解决方案。

不要试图操纵 CPU,而是挂接到任务管理器应用程序中,强制它绘制您想要的内容而不是 CPU 结果。 接管绘制图形等的 GDI 对象。有点“作弊”,但他们没有说你必须操纵 CPU

,甚至没有从任务管理器中挂钩获取 CPU % 的调用,而是返回正弦结果。

Ok I have a different, probably BETTER solution than my first answer.

Instead of trying to manipulate the CPU, instead hook into the task manager app, force it to draw what you want it to instead of CPU results. Take over the GDI object that plots the graph, etc. Sort of "Cheating" but they didnt say you had to manipulate the CPU

Or even hook the call from task manager that gets the CPU %, returning a sine result instead.

你的背包 2024-07-20 07:16:18

如今一台 PC 运行着数百(数千?)个线程,我认为接近的唯一方法是尽快轮询 CPU 使用率,并且使用率百分比是否低于曲线上应有的位置,触发一个仅搅动数字的简短方法。 这至少会在需要的地方带来典型的低使用率,但我想不出一种好方法来降低它,而不以某种方式控制其他线程,并做一些事情,例如强制降低优先级。

With the literally hundreds (thousands?) of threads a PC runs today, the only way I can think to even come close would be to poll CPU usage as fast as possible, and if the usage% was below where it should be on the curve, to fire off a short method that just churns numbers. That will at least bring the typical low usage UP where needed, but I can't think of a good way to LOWER it without somehow taking control of other threads, and doing something such as forcing thier priority lower.

巴黎夜雨 2024-07-20 07:16:18

像这样的事情:

while(true)
{
    for(int i=0;i<360;i++)
    {
       // some code to convert i into radians if needed
       ...
       Thread.Sleep(Math.Sin(i)*something_that_makes_it_noticeable_number_of_ms+something_that_makes_it_non_negative)
       // some work to make cpu busy, may be increased to bigger number to see the influence on the cpu.
       for(j=0;j<100;j++);
    }
}

Something like this:

while(true)
{
    for(int i=0;i<360;i++)
    {
       // some code to convert i into radians if needed
       ...
       Thread.Sleep(Math.Sin(i)*something_that_makes_it_noticeable_number_of_ms+something_that_makes_it_non_negative)
       // some work to make cpu busy, may be increased to bigger number to see the influence on the cpu.
       for(j=0;j<100;j++);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文