




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 P
2、ROCESS_STAT_H 9. #define PROCESS_STAT_H 10. 11. 12. #ifdef _cplusplus 13. extern "C" 14. #endif 15. 16. typedef long long
3、; int64_t; 17. typedef unsigned long long uint64_t; 18. 19. 20. / 获取当前进程的cpu使用率,返回-1失败 21. int
4、;get_cpu_usage(); 22. 23. 24. / 获取当前进程内存和虚拟内存使用量,返回-1失败,0成功 25. int get_memory_usage(uint64_t* mem, uint64_t* vmem); 26. 27. 28.
5、; / 获取当前进程总共读和写的IO字节数,返回-1失败,0成功 29. int get_io_bytes(uint64_t* read_bytes, uint64_t* write_bytes); 30. 31. 32. 33. 34. #ifdef _cplusplus 35.
6、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. *
7、;部分代码来自MSDN的例子 8. * 部分代码来自google chromium项目 9. * 10. * 需要连接到psapi.lib 11. */ 12. 13. 14. #include <windows.h> 15. #include <psapi.h> 16. #include <assert.h> 17. #in
8、clude "process_stat.h" 18. 19. 20. 21. / 时间转换 22. static uint64_t (const * ftime) 23. 24. LARGE_INTEGER li; 25. 26.
9、; assert(ftime); 27. li.LowPart = ftime->dwLowDateTime; 28. li.HighPart = ftime->dwHighDateTime; 29. return li.QuadPart; 30. 31. &
10、#160; 32. 33. / 获得CPU的核数 34. static int get_processor_number() 35. 36. SYSTEM_INFO info; 37. GetSystemInfo(&info); 38. return
11、(int)info.dwNumberOfProcessors; 39. 40. 41. 42. 43. 44. int get_cpu_usage() 45. 46. /cpu数量 47. static int processor_count_ =
12、60;-1; 48. /上一次的时间 49. static int64_t last_time_ = 0; 50. static int64_t last_system_time_ = 0; 51. 52. 53.
13、 now; 54. creation_time; 55. exit_time; 56. kernel_time; 57. user_time; 58. int64_t&
14、#160;system_time; 59. int64_t time; 60. int64_t system_time_delta; 61. int64_t time_delta; 62. 63. int cpu = -1;
15、 64. 65. 66. if(processor_count_ = -1) 67. 68. processor_count_ = get_processor_number(); 69.
16、60; 70. 71. GetSystemTimeAs(&now); 72. 73. if (!GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time, 74. &kern
17、el_time, &user_time) 75. 76. / We don't assert here because in some cases (such as in the Task 77.
18、;78. Manager) 79. / we may call this function on a process that has just exited but 80. 81. we have 82.
19、60; / not yet received the notification. 83. return -1; 84. 85. system_time = (&kernel_time) + (&am
20、p;user_time) 86. 87. / 88. processor_count_; 89. time = (&now); 90. 91. if (last_system_time_ = 0)
21、60;| (last_time_ = 0) 92. 93. / First call, just set the last values. 94. last_system_time_
22、;= system_time; 95. last_time_ = time; 96. return -1; 97. 98. 99. system_time_d
23、elta = system_time - last_system_time_; 100. time_delta = time - last_time_; 101. 102. assert(time_delta != 0); 103. 104.
24、;if (time_delta = 0) 105. return -1; 106. 107. / We add time_delta / 2 so the result is rounded. 108.
25、; cpu = (int)(system_time_delta * 100 + time_delta / 2) / time_delta); 109. last_system_time_ = system_time; 110. last_time_ = time; 111.
26、 return cpu; 112. 113. 114. 115. 116. int get_memory_usage(uint64_t* mem, uint64_t* vmem) 117. 118. PROCESS_MEMORY_COUNTERS pmc; 1
27、19. if(GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc) 120. 121. if(mem) *mem = pmc.WorkingSetSize; 122.
28、 if(vmem) *vmem = pmc.Page; 123. return 0; 124. 125. return -1; 126. 127. 128.
29、160;129. 130. int get_io_bytes(uint64_t* read_bytes, uint64_t* write_bytes) 131. 132. IO_COUNTERS io_counter; 133. if(GetProcessIoCounters(GetCurrentProcess(), &io_counter)
30、 134. 135. if(read_bytes) *read_bytes = io_counter.ReadTransferCount; 136. if(write_bytes) *write_bytes = io_counter
31、.WriteTransferCount; 137. return 0; 138. 139. return -1; 140. 可以这样使用: C代码 1. /* file 2. * brief 进程统计信
32、息函数的测试 3. * author 张亚霏 4. * date 2009/05/03 5. * version 0.1 6. * 7. */ 8. 9. #include "process_stat.h" 10. #include <stdio.h> 11. #include <Windows.h>
33、12. 13. int main() 14. 15. while(1) 16. 17. int cpu; 18. uint64_t mem, vmem, r, w; 19. 20. 21.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 健康蔬菜菌类有营养课件
- 健康绳子课件下载手机版
- 国一高考数学试卷
- 健康素养知识讲座课件
- 健康精灵村绘本课件
- 贵州联考文科数学试卷
- 2025年中国燃气轮机整体行业市场前景预测及投资战略咨询报告
- 2024-2030年中国大连市房地产行业市场全景分析及投资策略研究报告
- 中国何首乌提取物行业市场调研及未来发展趋势预测报告
- 矫形器可行性研究报告
- 健身房消防管理制度
- 高血压病并发心力衰竭的治疗策略
- 垃圾处理焚烧培训课件
- 国家中小学智慧教育平台培训专题讲座
- GMP附录-细胞治疗产品
- 2025年中国烘焙食品行业发展深度分析及行业发展趋势报告
- 2025江苏省惠隆资产管理限公司招聘30人易考易错模拟试题(共500题)试卷后附参考答案
- 《农村基层干部廉洁履行职责规定》解读与培训
- 招标代理服务服务方案
- 学术规范与论文写作讲述课件
- 欧亨利介绍及其作品
评论
0/150
提交评论