-
Notifications
You must be signed in to change notification settings - Fork 1
/
11.13 CPU_Heartbeat.cpp
64 lines (63 loc) · 1.57 KB
/
11.13 CPU_Heartbeat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <windows.h>
#include <cmath>
#define PI 3.1415926535
using namespace std;
const double _1000MILLI = 1000;
void per_second_cpu(__int64 start_time, double busy_time_rate) {
start_time = GetTickCount();
while ((GetTickCount() - start_time) <= busy_time_rate * _1000MILLI) {}
Sleep((1.0 - busy_time_rate) * _1000MILLI);
}
void line_cpu(double busy_time_rate) {
__int64 start_time = NULL;
while (true) {
per_second_cpu(start_time, busy_time_rate);
}
}
void sin_cpu() {
__int64 start_time = NULL;
while (true) {
int t = 15;
double busy_time_rate = NULL;
for (int i = 0; i < t; ++i) {
if (i == 5)
busy_time_rate = 0.9;
else if (i == 7)
busy_time_rate = 0;
else
busy_time_rate = 0.4;//*sin(i * 2 / (t * 1.0) * PI) / 2.0 +0.5;
per_second_cpu(start_time, busy_time_rate);
}
}
}
DWORD WINAPI handle_line(LPVOID lpParameter) {
line_cpu(*(double*)lpParameter);
return 0;
}
DWORD WINAPI handle_sin(LPVOID lpParameter) {
sin_cpu();
return 0;
}
void begin_line(double busy_time_rate) {
SetProcessAffinityMask(GetCurrentProcess(), 255);
for (int i = 0; i < 7; ++i) {
HANDLE handleProcess = CreateThread(NULL, 0, handle_line, &busy_time_rate, 0, NULL);
CloseHandle(handleProcess);
}
line_cpu(busy_time_rate);
}
void begin_sin() {
SetProcessAffinityMask(GetCurrentProcess(), 255);
for (int i = 0; i < 7; ++i) {
HANDLE handleProcess = CreateThread(NULL, 0, handle_sin, NULL, 0, NULL);
CloseHandle(handleProcess);
}
sin_cpu();
}
int main() {
// double busy_time_rate=0.5;
// begin_line(busy_time_rate);
begin_sin();
return 0;
}