64位电脑获取CPU使用率(使用c语言)
使用32位的电脑的CPU获取可以用:NtQuerySystemInformation(systemPerformanceInformation,&sysPerfInfo,...
使用32位的电脑的CPU获取可以用:NtQuerySystemInformation(systemPerformanceInformation, &sysPerfInfo, sizeof(sysPerfInfo), NULL);但是64位的就不能使用这个获取了,求高手指点!
展开
展开全部
GlobalMemoryStatusEx
获得系统时间:
CopyBOOL WINAPI GetSystemTimes(
__out_opt LPFILETIME lpIdleTime, //空闲时间
__out_opt LPFILETIME lpKernelTime, //内核时间
__out_opt LPFILETIME lpUserTime //用户时间
);
#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);//(总的时间-空闲时间)/总的时间=占用cpu的时间就是使用率
int cpuidle = ( idle) *100/(kernel+user);
cout << "CPU利用率:" << cpu << "%" << " CPU空闲率:" <<cpuidle << "%" <<endl;
preidleTime = idleTime;
prekernelTime = kernelTime;
preuserTime = userTime ;
}
}
获得系统时间:
CopyBOOL WINAPI GetSystemTimes(
__out_opt LPFILETIME lpIdleTime, //空闲时间
__out_opt LPFILETIME lpKernelTime, //内核时间
__out_opt LPFILETIME lpUserTime //用户时间
);
#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);//(总的时间-空闲时间)/总的时间=占用cpu的时间就是使用率
int cpuidle = ( idle) *100/(kernel+user);
cout << "CPU利用率:" << cpu << "%" << " CPU空闲率:" <<cpuidle << "%" <<endl;
preidleTime = idleTime;
prekernelTime = kernelTime;
preuserTime = userTime ;
}
}
快又稳
2024-10-28 广告
2024-10-28 广告
在Linux环境下配置基于域名的虚拟主机,需安装Apache或Nginx等Web服务器,并编辑配置文件。以Apache为例,需创建虚拟主机配置文件,指定域名、文档根目录等,然后启用该配置文件并重启Apache服务。同样,Nginx也需在相应...
点击进入详情页
本回答由快又稳提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询