请教下:C语言获取计算机系统CPU使用率,内存使用情况的思路或流程是怎样的?!

作者&投稿:书馥 (若有异议请与网页底部的电邮联系)
如何使用C或C++编程获取电脑CPU使用率?如何使用C或C++获取指定进程的CPU使用率?~

得到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
#include
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 ;
}
}

Windows系统可以搜索MSDN参考,Windows System Information这个章节下都是这一类的函数;Linux和Windows的系统调用完全不同

内存情况比较简单
MEMORYSTATUSEX mstx;
mstx.dwLength = sizeof (mstx);
GlobalMemoryStatusEx( &mstx );

int iMemeryUsePercentage = mstx.dwMemoryLoad;
int iTotalPhysMB = mstx.ullTotalPhys/1024/1024;
int iAvailPhysMB = mstx.ullAvailPhys/1024/1024;
int iTotalPageFileMB = mstx.ullTotalPageFile/1024/1024;
int iAvailPageFileMB = mstx.ullAvailPageFile/1024/1024;

char LogBuff[128];
memset( LogBuff , 0 , 128 );
sprintf( LogBuff , "MemAvailPct=%d%% Phys=%d/%d PageFile=%d/%d" , 100 - iMemeryUsePercentage , iAvailPhysMB , iTotalPhysMB , iAvailPageFileMB , iTotalPageFileMB );
printf("%s\n",LogBuff);
以上程序分别输出可用百分比,可以用物理内存/总物理内存,可用页面文件/总页面文件

获取CPU的比较复杂,我这边只有获取单个进程CPU占用的方法,不过可以遍历所有进程分别获取再求和就是整个cpu占用率了。
#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;
// 参数为进程id
cpu = get_cpu_usage(5160);
printf("CPU使用率: %d%%\n",cpu);

Sleep(1000);
}
return 0;
}

查WINDOW提供的接口是什么,然后用C去调用获取当前数值,就好了,然后设置一个轮询,每间隔一段时间轮询一次就好了。


C语言计算pi
include<stdio.h> include<math.h> void main(){ int sign=1;\/\/ 符号变量 因为要正1-3\/1 所以 在进行第一步运算的时候正1-某某 int i=1;\/\/ 此时的i 也可以等于count 用来存储运算次数 建议用count 我在这里就不改了 double pi=0.0;\/\/ 我们要求的结果 pi double n=1.0;\/\/ 我们...

请问在C语言里怎么获取当前时间和日期(精确到毫秒)?
time ( &rawtime ); -- 获取时间,以秒计,从1970年1月一日起算,存于rawtime localtime ( &rawtime ); -- 转为当地时间,tm 时间结构 asctime ()-- 转为标准ASCII时间格式:星期 月 日 时:分:秒 年 === 你要的格式可这样输出:printf ( "%4d-%02d-%02d %02d:%02d:%02d\\n",190...

C语言中如何获取当前系统时间的小时
程序主要通过当前系统日历的struct tm结构体获得,主要代码如下,include <stdio.h> include \/\/程序功能输出当前时间在24H下的小时数 int main(int argc, char *argv[]){ struct tm *ptr;time_t lt;time(<);\/\/当前系统时间 ptr=localtime(<);\/\/获取本地日历时间指针 printf("hour=%d(...

用C语言,输入一行数字字符,请用数组元素作为计数器来统计每个数字字符的...
\/*当输入的是英文字母时变量letters加1*\/。5、判断是否是空格:elseif (c == ' ') space++; \/*当输入的是空格时变量space加1*\/。6、输出结果:printf("char=%d space=%d digit=%d others=%d\\n",letters,space,digit,others); \/*将最终统计结果输出*\/。7、最后得到最后结果。

c语言,输出科学计数法
find2:是否找到首个非零数位置,0:表示未找到 int index; scanf("%s",num);len = strlen(num); \/\/读取字符串长度 for(i = 0;i < len;++i) \/\/寻找原字符串中小数点与首个非零数的位置 {if(find1 && find2)break;if(num[i] == '.'){s1 = i;find1 = 1;}else if...

21世纪普通高校计算机公共课程规划教材:C语言程序设计基本信息_百度...
本书是由王行恒、江红等编著的《21世纪普通高校计算机公共课程规划教材:C语言程序设计基本信息》。本书由清华大学出版社出版,ISBN号为9787302221029,出版时间是2010年3月1日,这是第一版,共计234页。装帧为平装,开本为16开。本书主要属于图书分类中的计算机与互联网编程语言与程序设计类。本书作为...

用C语言如何开51单片机的计数器,最好编个实例程序
void main(){ uint k;TMOD=0X50;\/\/设置模式为1,计数模式 TH1=0X00;TL1=0X00;\/\/初始值设定 IE=0X00;\/\/关全局中断 TR1=1;\/\/开定时计数器1运行 while(1){ if(TL1==16)\/\/进行判断,为16时,计数值归零 TL1=0X00;k=TL1;P0=LED7Code[k];\/\/数码管进行显示 } } 此程序目的见http:\/...

C语言统计一个字符串的字符个数,for (; getchar() != '\\n'; n++...
getchar是从你的输入内容里读取一个字符值,它的返回值是你输入的那个字符的ascii码,for (; getchar() != '\\n'; n++); 意思就是说当读到换行符时就结束循环,相当于 while(getchar()!='\\n') n++;也就是相当于读入一个字符,n就自加1,最终实现的效果就是对你输入内容的计数了。

C语言如何读取文件中指定的某一段
使用fread函数读取指定长度的字符串,即使包含\\n也会被读取,可以首先使用fseek定位到文件结尾,然后ftell函数返回的值就是文件的大小,这样就可以用循环多次读取文件,直到读取所有内容 FILE *file = NULL;char szFile[1025] = {0};int nHadRead = 0;file = fopen( "file.txt", "r+");if ( ...

C语言中strlen函数是怎么计数的?
请其它老师帮助解释)字符5:\\n 4. printf("%d \\n",strlen("stop\\0\\n\\""));字符1、2、3、4:stop 因为strlen()函数遇到\\0 就结束了,后面的字符就不去判断了。C语言中的字符串 是用\\0作为 结束符。希望我的解答 对你有所帮助,很高兴和你一起讨论和学习C语言。

新城子区19378302249: C语言怎么取CPU的各项信息? -
邰美磷酸: X86处理器的型号,信息处理器家庭,高速缓存尺寸,时钟速度(频率)和制造商codename 等,存放在处理器的CPU ID寄存器组中.通过执行CPU ID指令集查询,即可获取处理器的相关信息.CPU ID汇编指令使用使用eax作为输入参数(...

新城子区19378302249: C语言怎么取CPU的各项信息?
邰美磷酸: intel IA32架构下CPU提供了获取CPU信息的指令CPUID,用该指令能获取CPU信息,使用方法和说明如下.要注意的是,一下如有小于号的,都不是英文的小于号,要用都得该为英文的小于号. 1、什么是cpuid指令 CPUID指令是intel IA32架构...

新城子区19378302249: 如何用C语言编写探测cpu的程序 -
邰美磷酸: 参考下面的文章:利用利用CPUID 汇编指令(机器码:0FH A2H, 如果你的编译器不支持CPUID 指令,只有emit 机器码了), 该指令可以被如下CPU识别:Intel 486 以上的CPU, Cyrix M1 以上的CPU, AMD Am486 以上的CPU (1) 取CPU ...

新城子区19378302249: 在C程序中如何获取CPU序列号. -
邰美磷酸: 取CPU序列号没有意义,绝大多数多数情况下取不到CPU序列号~~~

新城子区19378302249: 用C语言怎么得到电脑的CPU序列号,硬盘序列号等信息 -
邰美磷酸: 获取CPU序列号要使用 汇编指令 比较麻烦 static DWORD g_eax; // 存储返回的eax static DWORD g_ebx; // 存储返回的ebx static DWORD g_ecx; // 存储返回的ecx static DWORD g_edx; // 存储返回的edx void Executecpuid(DWORD veax) ...

新城子区19378302249: 如何获取电脑CPU,内存,硬盘的信息.用C语言编写的. -
邰美磷酸: ctrl+n

新城子区19378302249: 请问怎么用c语言获取电脑(windows)的cpu,内存,硬盘利用率等信息?Linux系统一样吗? -
邰美磷酸: Windows系统可以搜索MSDN参考,Windows System Information这个章节下都是这一类的函数;Linux和Windows的系统调用完全不同

新城子区19378302249: C语言程序怎样判断计算机的CPU大小端 -
邰美磷酸: 写一个C函数,若处理器是Big_endian的,则返回false;若是Little_endian的,则返回true.bool IsLitte_Endian(){union w{int a;char b;}c;c.a=1;return (c.b==1); }

新城子区19378302249: linux下用c语言实现查cpu类型和型号等信息 -
邰美磷酸: 都在/proc/ 下面 cpu信息在/proc/cpuinfo 启动时间在/proc/uptime 单位是s /proc/stat 里面有cpu执行的时间,用户态,系统态,空闲都有

新城子区19378302249: 如何用c语言程序获取计算机信息 -
邰美磷酸: 开机密码不能获取哦.用户名很简单.只要用个GetUserName这个API就行了.详细的代码我给你看看://#include "stdafx.h"//#include "GetSysInfo.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILE static char THIS_FILE[] = __...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网