c语言实现cpu使用率 windows,windows下用c语言获取进程cpu使用率内存使用io情况

Windows 下用 C 语言获取进程 cpu 使用率,内存使用,IO 情况一个项目需要,特地写了这些功能的函数。 process_stat.h 的内容如下: C 代码 1. /** @file 2. * @brief 进程统计信息函数的声明 3. * @author 张亚霏 4. * @date 2009/05/03 5. * @version 0.1 6. * 7. */ 8. #ifndef PROCESS_STAT_H 9. #define PROCESS_STAT_H 10. 11. 12. #ifdef __cplusplus 13. extern “C“ { 14. #endif 15. 16. typedef long long int64_t; 17. typedef unsigned long long uint64_t; 18. 19. 20. /// 获取当前进程的 cpu 使用率,返回-1 失败 21. int get_cpu_usage(); 22. 23. 24. /// 获取当前进程内存和虚拟内存使用量,返回-1 失败,0 成功 25. int get_memory_usage(uint64_t* mem, uint64_t* vmem); 26. 27. 28. /// 获取当前进程总共读和写的 IO 字节数,返回-1 失败,0 成功 29. int get_io_bytes(uint64_t* read_bytes, uint64_t* write_bytes); 30. 31. 32. 33. 34. #ifdef __cplusplus 35. } 36. #endif 37. 38. #endif/*PROCESS_STAT_H*/ process_stat_win.c 的内容如下: C 代码 1. /** @file 2. * @brief 进程统计信息函数的实现 3. * @author 张亚霏 4. * @date 2009/05/03 5. * @version 0.1 6. * 7. * 部分代码来自 MSDN 的例子 8. * 部分代码来自 google chromium 项目 9. * 10. * 需要连接到 psapi.lib 11. */ 12. 13. 14. #include 15. #include 16. #include 17. #include “process_stat.h“ 18. 19. 20. 21. /// 时间转换 22. static uint64_t file_time_2_utc(const FILETIME* ftime) 23. { 24. LARGE_INTEGER li; 25. 26. assert(ftime); 27. li.LowPart = ftime-dwLowDateTime; 28. li.HighPart = ftime-dwHighDateTime; 29. return li.QuadPart; 30. } 31. 32. 33. /// 获得 CPU 的核数 34. static int get_processor_number() 35. { 36. SYSTEM_INFO info; 37. GetSystemInfo( 38. return (int)info.dwNumberOfProcessors; 39. } 40. 41. 42. 43. 44. int get_cpu_usage() 45. { 46. //cpu 数量 47. static int processor_count_ = -1; 48. //上一次的时间 49. static int64_t last_time_ = 0; 50. static int64_t last_system_time_ = 0; 51. 52. 53. FILETIME now; 54. FILETIME creation_time; 55. FILETIME exit_time; 56. FILETIME kernel_time; 57. FILETIME user_time; 58. int64_t system_time; 59. int64_t time; 60. int64_t system_time_delta; 61. int64_t time_delta; 62. 63. int cpu = -1; 64. 65. 66. if(processor_count_ == -1) 67. { 68. processor_count_ = get_processor_number(); 69. } 70. 71. GetSystemTimeAsFileTime( 72. 73. if (!GetProcessTimes(GetCurrentProcess(), 84. } 85. system_time = (file_time_2_utc( 89. time = file_time_2_utc( 90. 91. if ((last_system_time_ == 0) || (last_time_ == 0)) 92. { 93. // First call, just set the last values. 94. last_system_time_ = system_time; 95. last_time_ = time; 96. return -1; 97. } 98. 99. system_time_delta = system_time - last_system_time_; 100. time_delta = time - last_time_; 101. 102. assert(time_delta != 0); 103. 104. if (time_delta == 0) 105. return -1; 106. 107. // We add time_delta / 2 so the result is rounded. 108. cpu = (int)((system_time_delta * 100 + time_delta / 2) / time_delta); 109. last_system_time_ = system_time; 110. last_time_ = time; 111. return cpu; 112. } 113. 114. 115. 116. int get_memory_usage(uint64_t* mem, uint64_t* vmem) 117. { 118. PROCESS_MEMORY_COUNTERS pmc; 119. if(GetProcessMemoryInfo(GetCurrentProcess(), 122. if(vmem) *vmem = pmc.PagefileUsage; 123. return 0; 124. } 125. return -1; 126. } 127. 128. 129. 130. int get_io_bytes(uint64_t* read_bytes, uint64_t* write_bytes) 131. { 132. IO_COUNTERS io_counter; 133. if(GetProcessIoCounters(GetCurrentProcess(), 136. if(write_bytes) *write_bytes = io_counter.WriteTransferCount; 137. return 0; 138. } 139. return -1; 140. } 可以这样使用: C 代码 1. /** @file 2. * @brief 进程统计信息函数的测试 3. * @author 张亚霏 4. * @date 2009/05/03 5. * @version 0.1 6. * 7. */ 8. 9. #include “process_stat.h“ 10. #include 11. #include 12. 13. int main() 14. { 15. while(1) 16. { 17. int cpu; 18. uint64_t mem, vmem, r, w; 19. 20. 21. cpu = get_cpu_usage(); 22. get_memory_usage( 23. get_io_bytes( 24. 25. printf(“CPU 使用率 : %u\n“,cpu); 26. printf(“内存使用 : %u 字节\n“, mem); 27. printf(“虚拟内存使用 : %u 字节\n“, vmem); 28. printf(“总共读: %u 字节\n“, r); 29. printf(“总共写: %u 字节\n“, w); 30. 31. Sleep(1000); 32. } 33. return 0; 34. }摘自:http://zhangyafeikimi.iteye.com/blog/378658

展开阅读全文

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值