#ifndef PROCESS_STAT_H
#define PROCESS_STAT_H
#ifdef __cplusplus
extern
“C” {
#endif
typedef
long
long
int64_t;
typedef
unsigned
long
long
uint64_t;
int
get_cpu_usage();
int
get_memory_usage(uint64_t* mem, uint64_t* vmem);
int
get_io_bytes(uint64_t* read_bytes, uint64_t* write_bytes);
#ifdef __cplusplus
}
#endif
#endif/*PROCESS_STAT_H*/
#include
#include #include
#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;
}
static
int
get_processor_number()
{
SYSTEM_INFO info;
GetSystemInfo(&info);
return
(
int
)info.dwNumberOfProcessors;
}
int
get_cpu_usage()
{
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))
{
Manager)
we have
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_;
assert
(time_delta != 0);
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
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;
}
#include “process_stat.h”
#include
#include
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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
2014-06-12 JDK1.6官方下载_JDK6官方下载_JDK1.6API(chm)下载_JDK6APICHM中文参
2014-06-12 简简单单安装debian桌面工作环境
2014-06-12 oracle11G在linux环境下的卸载操作
2014-06-12 Debian5.04安装oracle11g 笔记
2014-06-12 debian7安装oracle11g
2014-06-12 unix fopen和fopen_s用法的比较
2014-06-12 QT的三种协议说明