收藏 分销(赏)

C--设计CPU占有率.doc

上传人:仙人****88 文档编号:11731579 上传时间:2025-08-11 格式:DOC 页数:2 大小:15.22KB 下载积分:10 金币
下载 相关 举报
C--设计CPU占有率.doc_第1页
第1页 / 共2页
C--设计CPU占有率.doc_第2页
第2页 / 共2页
本文档共2页,全文阅读请下载到手机保存,查看更方便
资源描述
得到cpu占有率的API函数: GetSystemTimes 得到内存使用情况的API函数: GlobalMemoryStatusEx Function Retrieves information about the system's current usage of both physical and virtual memory. GetPerformanceInfo Function Retrieves the performance values contained in the PERFORMANCE_INFORMATION structure 获取特定程序的内存使用情况用: GetProcessMemoryInfo Function Retrieves information about the memory usage of the specified process. 我写的一个cpu使用率例子: #define _WIN32_WINNT 0x0501 #include <Windows.h> #include <iostream> using namespace std; __int64 CompareFileTime ( FILETIME time1, FILETIME time2 ) { __int64 a = time1.dwHighDateTime << 32 | time1.dwLowDateTime ; __int64 b = time2.dwHighDateTime << 32 | time2.dwLowDateTime ; return (b - a); } void main() { HANDLE hEvent; BOOL res ; FILETIME preidleTime; FILETIME prekernelTime; FILETIME preuserTime; FILETIME idleTime; FILETIME kernelTime; FILETIME userTime; res = GetSystemTimes( &idleTime, &kernelTime, &userTime ); preidleTime = idleTime; prekernelTime = kernelTime; preuserTime = userTime ; hEvent = CreateEvent (NULL,FALSE,FALSE,NULL); // 初始值为 nonsignaled ,并且每次触发后自动设置为nonsignaled while (1){ WaitForSingleObject( hEvent,1000 ); //等待500毫秒 res = GetSystemTimes( &idleTime, &kernelTime, &userTime ); int idle = CompareFileTime( preidleTime,idleTime); int kernel = CompareFileTime( prekernelTime, kernelTime); int user = CompareFileTime(preuserTime, userTime); int cpu = (kernel +user - idle) *100/(kernel+user); int cpuidle = ( idle) *100/(kernel+user); cout << "CPU利用率:" << cpu << "%" << " CPU空闲率:" <<cpuidle << "%" <<endl; preidleTime = idleTime; prekernelTime = kernelTime; preuserTime = userTime ; } } 参考资料:
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服