1. The CPU usage curve is a sine function curve:
Step 1: To set the CPU usage curve to a function curve, first understand the relationship between the Function Curve and the CPU usage curve. Function y = f (x ), corresponding to the CPU usage curve, x indicates the elapsed time, and y indicates the CPU usage. We know that the CPU usage is 0%-100%, that is to say, the value range of the function is [0-1], but the value range of the sine function is [-]. Therefore, the sine function must be transformed to []. after y = 1/2 + 1/2sinx transformation, the value range is.
Step 2: For CPU usage y = 1/2 + 1/2 * sinx, that is to say, at a certain time point x, the CPU usage time is the ratio of busy time to total time y;
By following these two points, the algorithm for calculating the CPU usage curve as the sine function curve can be easily solved.
The Code is as follows:
# Include "Windows. h"
# Include "stdlib. h"
# Include "math. h"
Constdouble SPLIT = 0.01;
Const int COUNT = 100;
Const double PI = 3.14159265;
Const int INTERVAL = 300;
Int main ()
{
DWORD busySpan [COUNT];
DWORD idleSpan [COUNT];
Int half = INTERVAL/2;
Double radian = 0.0;
For (int I = 0; I <COUNT; I ++)
{
BusySpan [I] = DWORD (half + half * sin (PI * radian); // y = 1/2 + 1/2 * (sinx)
IdleSpan [I] = INTERVAL-busySpan [I];
Radian + = SPLIT;
}
DWORD starttime = 0;
Int J = 0;
While (true)
{
J = J % count;
Starttime = gettickcount ();
While (gettickcount-starttime <= busyspan [J]);
Sleep (idlespan [J]);
J ++;
}
Return 0;
}