如何通过C语言编程获取AIX的CPU,内存使用率

发布网友 发布时间:2022-04-22 17:12

我来回答

1个回答

热心网友 时间:2023-10-17 05:18

得到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.

#define _WIN32_WINNT   0x0501

#include <Windows.h>

#include <iostream> 

using   namespace   std;

__int CompareFileTime ( FILETIME time1, FILETIME time2 )

{

__int a = time1.dwHighDateTime << 32 | time1.dwLowDateTime ;

__int 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 ;

}

}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com