获取linux的CPU,内存,磁盘的C源代码

引自:http://blog.csdn.net/wangjiannuaa/article/details/6585136
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <sys/time.h>  
  4. #include <unistd.h>  
  5.   
  6. #include <sys/vfs.h>  
  7. #include <error.h>  
  8.   
  9. #define Gsize (1024.00*1024.00*1024.00)  
  10. #define Msize (1024.00*1024.00)  
  11.   
  12. #ifndef EXT2_SUPER_MAGIC  
  13. #define EXT2_SUPER_MAGIC 0xef53  
  14. #endif  
  15.   
  16.   
  17. double time_so_far()  
  18. {  
  19. struct timeval tp;  
  20.   
  21. if (gettimeofday(&tp, (struct timezone *) NULL) == -1)  
  22. perror("gettimeofday");  
  23. return ((double) (tp.tv_sec)) +(((double) tp.tv_usec) * 0.000001 );  
  24. }  
  25.   
  26.   
  27. int main()  
  28. {  
  29. FILE *f1;  
  30. double ti, tf;  
  31.   
  32. char c[10],d[10];  
  33. int i1,i2,i3,i4,i5,i6;  
  34.   
  35. /*cpu information*/  
  36. ti = time_so_far();  
  37. f1 = fopen("/proc/stat""r");  
  38. fscanf(f1, "%s\t%d\t%d\t%d\n", c, &i1, &i2, &i3);  
  39. fclose(f1);  
  40.   
  41. usleep(1000000);  
  42.   
  43. tf = time_so_far();  
  44. f1 = fopen("/proc/stat""r");  
  45. fscanf(f1, "%s\t%d\t%d\t%d\n", c, &i4, &i5, &i6);  
  46. fclose(f1);  
  47.   
  48. int t = (i4+i5+i6)-(i1+i2+i3);  
  49. printf("cpu usage: %.1f%%\n", (t / ((tf-ti) * 100)) * 100);  
  50.   
  51.   
  52. /*memory information*/  
  53. f1 = fopen("/proc/meminfo","r");  
  54. fscanf(f1, "%s\t%d\t%s",c,&i1,d);  
  55. printf("mem total: %d\n", i1);  
  56.   
  57. fscanf(f1, "%s\t%d\t%s",c,&i1,d);  
  58. printf("mem free: %d\n", i1);  
  59.   
  60. fclose(f1);  
  61.   
  62.   
  63. /*disk information*/  
  64.   
  65. long long blocks,bfree;  
  66. struct statfs fs;  
  67. if(statfs("/",&fs)<0)  
  68. {  
  69. perror("statfs");  
  70. exit(0);  
  71. }  
  72.   
  73. blocks=fs.f_blocks;  
  74. bfree=fs.f_bfree;  
  75.   
  76. if(fs.f_type==EXT2_SUPER_MAGIC)  
  77. {  
  78. printf("Total size of / is %f G\n",blocks*fs.f_bsize/Gsize);  
  79. printf("Free size of / is %f G\n",bfree*fs.f_bsize/Gsize);  
  80. }  
  81.   
  82.   
  83. return 0;  
  84.   
  85. }  



分开来写


  1. /*cpu information*/  
  2. float get_cpu()  
  3. {  
  4.     float cpu=0.0;  
  5.     char c[20];  
  6.     int i1,i2,i3,i4,i5,i6;  
  7.       
  8.     struct timeval tpstart,tpend;  
  9.     float timeuse;  
  10.     gettimeofday(&tpstart,NULL);  
  11.   
  12.     FILE* f1 = fopen("/proc/stat""r");  
  13.     fscanf(f1, "%s\t%d\t%d\t%d\n", c, &i1, &i2, &i3);  
  14.     fclose(f1);  
  15.       
  16.     usleep(1000000);  
  17.       
  18.     gettimeofday(&tpend,NULL);  
  19.     timeuse = 1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;  
  20.     timeuse /= 1000000;  
  21.   
  22.     f1 = fopen("/proc/stat""r");  
  23.     fscanf(f1, "%s\t%d\t%d\t%d\n", c, &i4, &i5, &i6);  
  24.     fclose(f1);  
  25.   
  26.     int t = (i4+i5+i6)-(i1+i2+i3);  
  27.     cpu = t /timeuse;  
  28.   
  29.     return cpu;  
  30. }  
  31.   
  32. /*memory information*/   
  33. unsigned int get_mem_total()  
  34. {  
  35.     unsigned int mem_total=0;  
  36.     char c[20],d[20];  
  37.   
  38.     FILE* f1 = fopen("/proc/meminfo","r");    
  39.     fscanf(f1, "%s\t%d\t%s",c,&mem_total,d);    
  40.     fclose(f1);   
  41.   
  42.     return mem_total/1024;//MB  
  43. }  
  44.   
  45. unsigned int get_mem_free()  
  46. {  
  47.     unsigned int mem_free=0;  
  48.     char c[20],d[20];  
  49.   
  50.     FILE* f1 = fopen("/proc/meminfo","r");    
  51.     fscanf(f1, "%s\t%d\t%s",c,&mem_free,d);    
  52.     fclose(f1);   
  53.   
  54.     return mem_free/1024;//MB  
  55. }  
  56.   
  57. float get_disk_total()  
  58. {  
  59.     float disk_total=0;  
  60.   
  61.     long long blocks;    
  62.     struct statfs fs;    
  63.     if(statfs("/",&fs)<0)    
  64.     {    
  65.         perror("statfs");    
  66.         exit(0);    
  67.     }  
  68.   
  69.     blocks=fs.f_blocks;    
  70.   
  71.     if(fs.f_type==EXT2_SUPER_MAGIC)    
  72.     {    
  73.         disk_total=blocks*fs.f_bsize/Gsize;  
  74.     }  
  75.       
  76.     return disk_total;  
  77. }  
  78.   
  79. float get_disk_free()  
  80. {  
  81.     float disk_free=0;  
  82.   
  83.     long long bfree;    
  84.     struct statfs fs;    
  85.     if(statfs("/",&fs)<0)    
  86.     {    
  87.         perror("statfs");    
  88.         exit(0);    
  89.     }    
  90.   
  91.     bfree=fs.f_bfree;    
  92.   
  93.     if(fs.f_type==EXT2_SUPER_MAGIC)    
  94.     {    
  95.         disk_free = bfree*fs.f_bsize/Gsize;  
  96.     }    
  97.   
  98.     return disk_free;  
  99. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值