windows下用c语言获取进程cpu使用率内存使用io情况_第1页
windows下用c语言获取进程cpu使用率内存使用io情况_第2页
windows下用c语言获取进程cpu使用率内存使用io情况_第3页
windows下用c语言获取进程cpu使用率内存使用io情况_第4页
windows下用c语言获取进程cpu使用率内存使用io情况_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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 使用率 : %un“,cpu); 26. printf(“内存使用 : %u 字节n“, mem

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论