C++获得进程CPU使用率
来源:互联网 发布:安阳师范学院软件类 编辑:程序博客网 时间:2024/05/18 02:56
主要使用的API为 GetProcessTimes,经测试,发现和任务管理器中还是比较接近的
#include <stdio.h> #include <Windows.h>typedef long long int64_t;typedef unsigned long long uint64_t;/// 时间转换static uint64_t file_time_2_utc(const FILETIME* ftime){LARGE_INTEGER li;li.LowPart = ftime->dwLowDateTime;li.HighPart = ftime->dwHighDateTime;return li.QuadPart;}/// 获得CPU的核数static int get_processor_number(){SYSTEM_INFO info;GetSystemInfo(&info);return (int)info.dwNumberOfProcessors;}int get_cpu_usage(int pid){ //cpu数量static int processor_count_ = -1;//上一次的时间static int64_t last_time_ = 0;static int64_t last_system_time_ = 0;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;int cpu = -1;if(processor_count_ == -1){processor_count_ = get_processor_number();}GetSystemTimeAsFileTime(&now);HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid);if (!GetProcessTimes(hProcess, &creation_time, &exit_time, &kernel_time, &user_time)){return -1;}system_time = (file_time_2_utc(&kernel_time) + file_time_2_utc(&user_time)) / processor_count_; time = file_time_2_utc(&now);if ((last_system_time_ == 0) || (last_time_ == 0)){last_system_time_ = system_time;last_time_ = time;return -1;}system_time_delta = system_time - last_system_time_;time_delta = time - last_time_;if (time_delta == 0)return -1;cpu = (int)((system_time_delta * 100 + time_delta / 2) / time_delta);last_system_time_ = system_time;last_time_ = time;return cpu;}int main() { while(1) { int cpu;// 参数为进程idcpu = get_cpu_usage(1120);printf("CPU使用率: %d%%\n",cpu);Sleep(500);} return 0;}
转载于:http://www.sizeof.cn/html/2010/365.html
- C++获得进程CPU使用率
- C# 获得CPU使用率
- C#获得Cpu使用率
- 获得系统中某个进程的cpu使用率!(ZZ)
- 获得系统中某个进程的cpu使用率
- 获取进程cpu使用率
- C#获得Cpu使用率类
- linux下获得CPU使用率
- 获取单个进程CPU使用率
- VB.NET通过PID(ProcessID)获得某进程的CPU使用率
- 用java获得cpu,内存使用率
- 获得应用程序内存占用 cpu使用率
- C语言控制cpu使用率
- C语言控制cpu使用率
- Linux下限制进程cpu使用率
- cidaemon.exe进程cpu使用率100%
- linux 下查看进程 及CPU使用率
- 获取进程对应的CPU使用率
- arm驱动程序——点亮led(韦东山的视频总结及针对linux-2.6.30)
- 十四、探索虚拟内存
- linux下gnuplot安装方法
- jdbc 事务的隔离级别
- awk 使用重定向符>的问题。
- C++获得进程CPU使用率
- 学习网址
- 详解Windows内存分页机制
- mvc3[1]DropDownList
- Eclipse 编译java项目不能生成class文件
- 第十四天:可以下载安装了
- poj 1743 Musical Theme(最长不重叠重复子串 后缀数组+二分)
- 希尔排序
- flex HttpService请求