process_stat.h:
/** @file
* @brief 进程统计信息函数的声明
* @author 张亚霏
* @date 2009/05/03
* @version 0.1
*
*/
#ifndef PROCESS_STAT_H
#define PROCESS_STAT_H
#ifdef __cplusplus
extern "C" {
#endif
typedef long long int64_t;
typedef unsigned long long uint64_t;
/// 获取当前进程的cpu使用率,返回-1失败
int get_cpu_usage();
/// 获取当前进程内存和虚拟内存使用量,返回-1失败,0成功
int get_memory_usage(uint64_t* mem, uint64_t* vmem);
/// 获取当前进程总共读和写的IO字节数,返回-1失败,0成功
int get_io_bytes(uint64_t* read_bytes, uint64_t* write_bytes);
#ifdef __cplusplus
}
#endif
#endif/*PROCESS_STAT_H*/
process_stat.cpp:
/** @file
* @brief 进程统计信息函数的实现
* @author 张亚霏
* @date 2009/05/03
* @version 0.1
*
* 部分代码来自MSDN的例子
* 部分代码来自google chromium项目
*
* 需要连接到psapi.lib
*/
#include <windows.h>
#include <psapi.h>
#include <assert.h>
#include "process_stat.h"
/// 时间转换
static uint64_t file_time_2_utc(const FILETIME* ftime)
{
LARGE_INTEGER li;
assert(ftime);
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()
{
//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);
if (!GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time,
&kernel_time, &user_time))
{
// We don't assert here because in some cases (such as in the Task
Manager)
// we may call this function on a process that has just exited but
we have
// not yet received the notification.
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))
{
// First call, just set the last values.
last_system_time_ = system_time;
last_time_ = time;
return -1;
}
system_time_delta = system_time - last_system_time_;
time_delta = time - last_time_;
assert(time_delta != 0);
if (time_delta == 0)
return -1;
// We add time_delta / 2 so the result is rounded.
cpu = (int)((system_time_delta * 100 + time_delta / 2) / time_delta);
last_system_time_ = system_time;
last_time_ = time;
return cpu;
}
int get_memory_usage(uint64_t* mem, uint64_t* vmem)
{
PROCESS_MEMORY_COUNTERS pmc;
if(GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
{
if(mem) *mem = pmc.WorkingSetSize;
if(vmem) *vmem = pmc.PagefileUsage;
return 0;
}
return -1;
}
int get_io_bytes(uint64_t* read_bytes, uint64_t* write_bytes)
{
IO_COUNTERS io_counter;
if(GetProcessIoCounters(GetCurrentProcess(), &io_counter))
{
if(read_bytes) *read_bytes = io_counter.ReadTransferCount;
if(write_bytes) *write_bytes = io_counter.WriteTransferCount;
return 0;
}
return -1;
}
使用方法:
/** @file
* @brief 进程统计信息函数的测试
* @author 张亚霏
* @date 2009/05/03
* @version 0.1
*
*/
#include "process_stat.h"
#include <stdio.h>
#include <Windows.h>
int main()
{
while(1)
{
int cpu;
uint64_t mem, vmem, r, w;
cpu = get_cpu_usage();
get_memory_usage(&mem, &vmem);
get_io_bytes(&r, &w);
printf("CPU使用率: %u\n",cpu);
printf("内存使用: %u 字节\n", mem);
printf("虚拟内存使用: %u 字节\n", vmem);
printf("总共读: %u 字节\n", r);
printf("总共写: %u 字节\n", w);
Sleep(1000);
}
return 0;
}
注:需要用到psapi.h和psapi.lib文件。在我的资源里有完整的代码和必需文件的下载:
http://download.csdn.net/detail/yanzhibo/5319906
转载自:http://zhangyafeikimi.iteye.com/blog/378658
相关推荐
Windows下用C语言获取进程CPU使用率,内存使用,IO情况。 可以准确的获取当前进程的CPU使用率。
Windows下用C语言获取进程CPU使用率,内存使用,IO情况。 可以准确的获取当前进程的CPU使用率。
linux c程序获取cpu使用率及内存使用情况
C/C++实现linux和Windows查看系统、进程、服务的内存和CPU使用情况的源代码,生成的是静态链接库,Windows和Linux下经测试都可用,查看系统、指定名称的服务、指定名称的进程占用CPU和内存,查看方式不是通过程序中...
使用VB C语言 获取电脑CPU使用率,小巧实用,小内存使用,堪比鲁大师(说笑而已),完全代码公开,全解注释,直接F5运行,完全自己手动编写,适合有兴趣的猿员人们研究,桌面显示实时监控!!CPU使用率源代码,CPU使用...
Linux和Windows下使用C语言获取各种硬件信息,包括CPU序列号、硬盘序列号、网卡信息(包括网卡名字、IP地址、MAC地址、网卡是否插入网线等)。
平台:linux。 语言:c。 功能:查看当前系统cpu使用率(可多核),1秒刷新一次。
linux下C语言编程4-使用共享内存实现进程间通信
使用windows提供的API实现的CPU温度检测,显示在控制台上。使用VS2017版本开发。
使用C语言获取windows操作系统的MAC列表,已实测可以运行
写一个小程序在后台记录每个进程的CPU使用情况,揪出锁屏后占用CPU的进程,于是自己写了一个C++类CPUusage,方便地监视不同进程的CPU占用情况。本人编程还只是个新手,如有问题请多多指教
获取CPU序列号 硬盘序列号 vs2010工程 支持64位
Linux系统下使用C语言实现进程借助共享内存实现通信
C语言编写的获取屏幕刷新率源代码。
每隔1秒扫描一次当前进程列表,终止非授权当前用户下的任何进程,抓取屏幕另存为bmp文件,获取进程用户名,关机函数。由于我菜鸟,注释不一定对。下载了觉得不好的,就对不起了 环境:vc6.0 vs2010 系统:xp 仅授予...
写的一个c语言函数获取windows下的日期和时间的类,相对mfc的CTime类增加了一些常用的操作,更重要的一点是它不需要对mfc的支持,可在任何windows下的c、c++开发的工程中使用。
本程序是用C语言实现获取CPU信息的程序。嵌套汇编完成。我在VC6.0上试过了。
linux下C语言编程4-使用共享内存实现进程间通信.pdf
9. Windows下C语言程序的内存布局(内存模型) 10. 用户模式和内核模式 11. 栈(Stack)是什么?栈溢出又是怎么回事? 12. 一个函数在栈上到底是怎样的? 13. 函数调用惯例(Calling Convention)
Windows平台C语言获取文件的一些属性