码迷,mamicode.com
首页 > 系统相关 > 详细

获取内存使用率(Linux C)

时间:2018-07-11 20:38:58      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:code   proc   rate   ble   1.0   double   can   return   pen   

#define SYS_MEM_NAME_LEN 20 #define SYS_MEM_BUFF_LEN 256 #define SYS_100_PERSENT 100 int get_sysMemUsage() { FILE *fd; char buff[SYS_MEM_BUFF_LEN]; double mem_used_rate; char name01[SYS_MEM_NAME_LEN]; unsigned long mem_total; char name02[SYS_MEM_NAME_LEN]; char name11[SYS_MEM_NAME_LEN]; unsigned long mem_free; char name12[SYS_MEM_NAME_LEN]; fd = fopen ("/proc/meminfo", "r"); fgets (buff, sizeof(buff), fd); sscanf (buff, "%s %lu %s\n", name01, &mem_total, name02); fgets (buff, sizeof(buff), fd); sscanf (buff, "%s %lu %s\n", name11, &mem_free, name12); mem_used_rate = (1.0 - (double)mem_free/(double)mem_total) * SYS_100_PERSENT; fclose(fd); return mem_used_rate; }

获取内存使用率(Linux C)

标签:code   proc   rate   ble   1.0   double   can   return   pen   

原文地址:http://blog.51cto.com/qiaopeng688/2140584

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!