转自:http://blog.csdn.net/liuqx97bb/article/details/52058657 近来发现笔记本在关闭屏幕后风扇转得特别快,打开屏幕后看任务管理器,风扇马上减速,也没有发现大量占用CPU的进程。于是想写一个小程序在后台记录每个进程的CPU使用情况,揪出锁屏后占用CPU的进程。于是自己写了一个C++类CPUusage,方便地监视不同进程的CPU占用情况。本人编程还只是个新手,如有问题请多多指教( •̀ ω •́ )! 计算原理为调用GetProcessTimes(),与上次调用得到的结果相减得到CPU占用时间,再除以两次调用的时间差,从而得到占用百分比。其中OpenProcess需要的权限为PROCESS\_QUERY\_LIMITED\_INFORMATION,因此没有管理员权限也可以使用。

使用方法:

初始化:

可以在构造函数中指定pid,也可以用setpid()指定pid。

查看CPU占用情况:

setpid()函数:

指定一个需要监视的进程的PID。

get\_cpu\_usage()函数:

查看CPU占用情况。打开进程失败,或者查看CPU占用情况失败,以及被监视的进程退出后,都会返回-1。每次使用setpid()指定新的pid后首次调用都会返回-2。指定PID后从第二次调用开始,会返回一个0~100的float,为此次调用与上一次调用这段时间内的CPU平均占用率。

代码:

CPUusage类:(CPUusage.h)

[cpp] view plaincopy

 #include <Windows.h>
//原理:调用GetProcessTimes(),并与上次调用得到的结果相减,即得到某段时间内CPU的使用时间
//C++ 获取特定进程规定CPU使用率 原文:http://blog.csdn.net/liuqx97bb/article/details/52058657
4. class CPUusage {
5. private:
6. typedeflonglong int64\_t;
7. typedef unsigned longlong uint64\_t;
8. HANDLE \_hProcess;
9. int \_processor; //cpu数量
10. int64\_t \_last\_time; //上一次的时间
11. int64\_t \_last\_system\_time;
14. // 时间转换
15. uint64\_t file\_time\_2\_utc(const FILETIME\* ftime);
17. // 获得CPU的核数
18. int get\_processor\_number();
20. //初始化
21. void init()
22. {
23. \_last\_system\_time = 0;
24. \_last\_time = 0;
25. \_hProcess = 0;
26. }
28. //关闭进程句柄
29. void clear()
30. {
31. if (\_hProcess) {
32. CloseHandle(\_hProcess);
33. \_hProcess = 0;
34. }
35. }
37. public:
38. CPUusage(DWORD ProcessID) {
39. init();
40. \_processor = get\_processor\_number();
41. setpid(ProcessID);
42. }
43. CPUusage() { init(); \_processor = get\_processor\_number(); }
44. ~CPUusage() { clear(); }
46. //返回值为进程句柄,可判断OpenProcess是否成功
47. HANDLE setpid(DWORD ProcessID) {
48. clear(); //如果之前监视过另一个进程,就先关闭它的句柄
49. init();
50. return \_hProcess= OpenProcess(PROCESS\_QUERY\_LIMITED\_INFORMATION, false, ProcessID);
51. }
53. //-1 即为失败或进程已退出; 如果成功,首次调用会返回-2(中途用setpid更改了PID后首次调用也会返回-2)
54. float get\_cpu\_usage();
55. };

实现:(CPUusage.cpp)

**\[cpp\]** [view plain](http://blog.csdn.net/liuqx97bb/article/details/52058657# "view plain")[copy](http://blog.csdn.net/liuqx97bb/article/details/52058657# "copy")

<embed />

1. float CPUusage::get\_cpu\_usage()
2. {
4. FILETIME now;
5. FILETIME creation\_time;
6. FILETIME exit\_time;
7. FILETIME kernel\_time;
8. FILETIME user\_time;
9. int64\_t system\_time;
10. int64\_t time;
11. int64\_t system\_time\_delta;
12. int64\_t time\_delta;
14. DWORD exitcode;
16. float cpu = -1;
18. if (!\_hProcess) return -1;
20. GetSystemTimeAsFileTime(&now);
22. //判断进程是否已经退出
23. GetExitCodeProcess(\_hProcess, &exitcode);
24. if (exitcode != STILL\_ACTIVE) {
25. clear();
26. return -1;
27. }
29. //计算占用CPU的百分比
30. if (!GetProcessTimes(\_hProcess, &creation\_time, &exit\_time, &kernel\_time, &user\_time))
31. {
32. clear();
33. return -1;
34. }
35. system\_time = (file\_time\_2\_utc(&kernel\_time) + file\_time\_2\_utc(&user\_time))
36. / \_processor;
37. time = file\_time\_2\_utc(&now);
39. //判断是否为首次计算
40. if ((\_last\_system\_time == 0) || (\_last\_time == 0))
41. {
42. \_last\_system\_time = system\_time;
43. \_last\_time = time;
44. return -2;
45. }
47. system\_time\_delta = system\_time - \_last\_system\_time;
48. time\_delta = time - \_last\_time;
50. if (time\_delta == 0) {
51. return -1;
52. }
54. cpu = (float)system\_time\_delta \* 100 / (float)time\_delta;
55. \_last\_system\_time = system\_time;
56. \_last\_time = time;
57. return cpu;
58. }
60. CPUusage::uint64\_t CPUusage::file\_time\_2\_utc(const FILETIME\* ftime)
61. {
62. LARGE\_INTEGER li;
64. li.LowPart = ftime->dwLowDateTime;
65. li.HighPart = ftime->dwHighDateTime;
66. return li.QuadPart;
67. }
69. int CPUusage::get\_processor\_number()
70. {
71. SYSTEM\_INFO info;
72. GetSystemInfo(&info);
73. return info.dwNumberOfProcessors;
74. }

测试代码:

**\[cpp\]** [view plain](http://blog.csdn.net/liuqx97bb/article/details/52058657# "view plain")[copy](http://blog.csdn.net/liuqx97bb/article/details/52058657# "copy")

<embed />

1. #include "CPUusage.h"
2. int \_tmain(int argc, \_TCHAR\* argv\[\])
3. {
5. CPUusage usg(12316);
6. for (int i = 0; i < 10; i++)
7. {
8. float cpu = usg.get\_cpu\_usage();
9. printf("Taskmgr.exe: %.2f%%\\n", cpu);
10. Sleep(500);
11. }
13. usg.setpid(11084);
14. for (int i = 0; i < 10; i++)
15. {
16. float cpu = usg.get\_cpu\_usage();
17. printf("devenv.exe: %.2f%%\\n", cpu);
18. Sleep(1000);
19. }
21. return 0;
22. }

发表评论