Write code to make CPU usage display a sine wave

后端 未结 5 597
无人及你
无人及你 2020-12-12 18:31

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

This is a technical in

5条回答
  •  臣服心动
    2020-12-12 19:04

    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(x)/180*PI)*0.5f + 0.5f;
            DWORD busy_time = static_cast(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.

提交回复
热议问题