近來發現筆記本在關閉屏幕后風扇轉得特別快,打開屏幕后看任務管理器,風扇馬上減速,也沒有發現大量占用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)
#include <Windows.h> //原理:調用GetProcessTimes(),并與上次調用得到的結果相減,即得到某段時間內CPU的使用時間 //C++ 獲取特定進程規定CPU使用率 原文:http://blog.csdn.net/liuqx97bb/article/details/52058657 class CPUusage { private: typedef long long int64_t; typedef unsigned long long uint64_t; HANDLE _hProcess; int _processor; //cpu數量 int64_t _last_time; //上一次的時間 int64_t _last_system_time; // 時間轉換 uint64_t file_time_2_utc(const FILETIME* ftime); // 獲得CPU的核數 int get_processor_number(); //初始化 void init() { _last_system_time = 0; _last_time = 0; _hProcess = 0; } //關閉進程句柄 void clear() { if (_hProcess) { CloseHandle(_hProcess); _hProcess = 0; } } public: CPUusage(DWORD ProcessID) { init(); _processor = get_processor_number(); setpid(ProcessID); } CPUusage() { init(); _processor = get_processor_number(); } ~CPUusage() { clear(); } //返回值為進程句柄,可判斷OpenProcess是否成功 HANDLE setpid(DWORD ProcessID) { clear(); //如果之前監視過另一個進程,就先關閉它的句柄 init(); return _hProcess= OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, ProcessID); } //-1 即為失敗或進程已退出; 如果成功,首次調用會返回-2(中途用setpid更改了PID后首次調用也會返回-2) float get_cpu_usage(); };實現:(CPUusage.cpp)
float CPUusage::get_cpu_usage() { FILETIME now; FILETIME creation_time; FILETIME exit_time; FILETIME kernel_time; FILETIME user_time; int64_t system_time; int64_t time; int64_t system_time_delta; int64_t time_delta; DWORD exitcode; float cpu = -1; if (!_hProcess) return -1; GetSystemTimeAsFileTime(&now); //判斷進程是否已經退出 GetExitCodeProcess(_hProcess, &exitcode); if (exitcode != STILL_ACTIVE) { clear(); return -1; } //計算占用CPU的百分比 if (!GetProcessTimes(_hProcess, &creation_time, &exit_time, &kernel_time, &user_time)) { clear(); return -1; } system_time = (file_time_2_utc(&kernel_time) + file_time_2_utc(&user_time)) / _processor; time = file_time_2_utc(&now); //判斷是否為首次計算 if ((_last_system_time == 0) || (_last_time == 0)) { _last_system_time = system_time; _last_time = time; return -2; } system_time_delta = system_time - _last_system_time; time_delta = time - _last_time; if (time_delta == 0) { return -1; } cpu = (float)system_time_delta * 100 / (float)time_delta; _last_system_time = system_time; _last_time = time; return cpu; } CPUusage::uint64_t CPUusage::file_time_2_utc(const FILETIME* ftime) { LARGE_INTEGER li; li.LowPart = ftime->dwLowDateTime; li.HighPart = ftime->dwHighDateTime; return li.QuadPart; } int CPUusage::get_processor_number() { SYSTEM_INFO info; GetSystemInfo(&info); return info.dwNumberOfProcessors; }測試代碼:
#include "CPUusage.h" int _tmain(int argc, _TCHAR* argv[]) { CPUusage usg(12316); for (int i = 0; i < 10; i++) { float cpu = usg.get_cpu_usage(); printf("Taskmgr.exe: %.2f%%/n", cpu); Sleep(500); } usg.setpid(11084); for (int i = 0; i < 10; i++) { float cpu = usg.get_cpu_usage(); printf("devenv.exe: %.2f%%/n", cpu); Sleep(1000); } return 0; }這篇文章就結束到這了,需要的朋友可以參考一下。
新聞熱點
疑難解答
圖片精選