Windows下用C语言获取进程cpu使用率,内存使用,IO情况

/** @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*/

/** @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; 
}

获取Linux系统各个CPU核心的使用率,可以使用C语言编写程序,通过读取/proc/stat文件中的信息来获取。 下面是一个简单的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #define MAX_CPU_NUM 8 // 假设最多有8个CPU核心 int main() { int i; long int user1, nice1, sys1, idle1, iowait1, irq1, softirq1, steal1, guest1, guest_nice1; long int user2, nice2, sys2, idle2, iowait2, irq2, softirq2, steal2, guest2, guest_nice2; double usage[MAX_CPU_NUM]; while (1) { // 读取/proc/stat文件,获取CPU使用情况 FILE* fp = fopen("/proc/stat", "r"); if (fp == NULL) { perror("fopen"); exit(1); } fscanf(fp, "cpu %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld\n", &user1, &nice1, &sys1, &idle1, &iowait1, &irq1, &softirq1, &steal1, &guest1, &guest_nice1); fclose(fp); // 等待一段时间,再次读取/proc/stat文件,获取CPU使用情况 sleep(1); fp = fopen("/proc/stat", "r"); if (fp == NULL) { perror("fopen"); exit(1); } fscanf(fp, "cpu %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld\n", &user2, &nice2, &sys2, &idle2, &iowait2, &irq2, &softirq2, &steal2, &guest2, &guest_nice2); fclose(fp); // 计算每个CPU核心的使用率 for (i = 0; i < MAX_CPU_NUM; i++) { double total1 = user1 + nice1 + sys1 + idle1 + iowait1 + irq1 + softirq1 + steal1 + guest1 + guest_nice1; double total2 = user2 + nice2 + sys2 + idle2 + iowait2 + irq2 + softirq2 + steal2 + guest2 + guest_nice2; double idle_delta = idle2 - idle1; double total_delta = total2 - total1; usage[i] = 100.0 * (1.0 - idle_delta / total_delta); user1 = user2; nice1 = nice2; sys1 = sys2; idle1 = idle2; iowait1 = iowait2; irq1 = irq2; softirq1 = softirq2; steal1 = steal2; guest1 = guest2; guest_nice1 = guest_nice2; } // 输出每个CPU核心的使用率 for (i = 0; i < MAX_CPU_NUM; i++) { printf("CPU%d usage: %.2f%%\n", i, usage[i]); } printf("\n"); } return 0; } ``` 这个程序会不断循环读取/proc/stat文件,计算每个CPU核心的使用率,并输出结果。注意,程序中假设最多有8个CPU核心,如果实际系统中CPU核心数不足8个,可以根据实际情况修改代码。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值