http://hi.baidu.com/lionpanther/item/e908c37abdaf1c3f71442380

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>  
struct occupy       
{
   char name[20];      
   unsigned int user;  
   unsigned int nice;  
   unsigned int system;
   unsigned int idle; 
};
float g_cpu_used;          
int cpu_num;               
void cal_occupy(struct occupy *, struct occupy *); 
void get_occupy(struct occupy *);

int main()                  
{
   struct occupy ocpu[10];   
   struct occupy ncpu[10];   
   int i;

cpu_num = sysconf(_SC_NPROCESSORS_ONLN);
   for(;;)                            
   {  
      sleep(1);                       
      get_occupy(ocpu);                
      sleep(1);                       
      get_occupy(ncpu);                
      for (i=0; i<cpu_num; i++)         
      {
         cal_occupy(&ocpu[i], &ncpu[i]); 
         printf("%f \n", g_cpu_used);    
      }
   }
}
void cal_occupy (struct occupy *o, struct occupy *n)
{
   double od, nd;   
   double id, sd;   
   double scale;    
   od = (double) (o->user + o->nice + o->system +o->idle);//第一次(用户+优先级+系统+空闲)的时间
   nd = (double) (n->user + n->nice + n->system +n->idle);//第二次(用户+优先级+系统+空闲)的时间
   scale = 100.0 / (float)(nd-od);      
   id = (double) (n->user - o->user);   
   sd = (double) (n->system - o->system);
   g_cpu_used = ((sd+id)*100.0)/(nd-od);
}
void get_occupy (struct occupy *o)
{
   FILE *fd;        
   int n;           
   char buff[1024];  
   fd = fopen ("/proc/stat", "r"); 
   fgets (buff, sizeof(buff), fd); 
   for(n=0;n<cpu_num;n++)          
   {
      fgets (buff, sizeof(buff),fd);
      sscanf (buff, "%s %u %u %u %u", &o[n].name, &o[n].user, &o[n].nice,&o[n].system, &o[n].idle);
      fprintf (stderr, "%s %u %u %u %u\n", o[n].name, o[n].user, o[n].nice,o[n].system, o[n].idle);
   }
   fclose(fd);    
}

linux CPU loading calculate的更多相关文章

  1. Linux CPU亲缘性详解

    前言 在淘宝开源自己基于nginx打造的tegine服务器的时候,有这么一项特性引起了笔者的兴趣.“自动根据CPU数目设置进程个数和绑定CPU亲缘性”.当时笔者对CPU亲缘性没有任何概念,当时作者只是 ...

  2. 查看线程linux cpu使用率

    Linux下如何查看高CPU占用率线程 LINUX CPU利用率计算 转 http://www.cnblogs.com/lidabo/p/4738113.html目录(?)[-] proc文件系统 p ...

  3. Linux CPU数量判断,通过/proc/cpuinfo.

    Linux CPU数量判断,通过/proc/cpuinfo. 相同 physical id :决定一个物理处理器 如果“siblings”和“cpu cores”一致,则说明不支持超线程,或者超线程未 ...

  4. How do I Find Out Linux CPU Utilization?

    From:http://www.cyberciti.biz/tips/how-do-i-find-out-linux-cpu-utilization.html Whenever a Linux sys ...

  5. Linux CPU监控指标

    Linux CPU监控指标 Linux提供了非常丰富的命令可以进行CPU相关数据进行监控,例如:top.vmstat等命令.top是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执 ...

  6. 转载: 一、linux cpu、内存、IO、网络的测试工具

    来源地址: http://blog.csdn.net/wenwenxiong/article/details/77197997 记录一下 以后好找.. 一.linux cpu.内存.IO.网络的测试工 ...

  7. Linux CPU使用率含义及原理

    相关概念 在Linux/Unix下,CPU利用率分为用户态.系统态和空闲态,分别表示CPU处于用户态执的时间,系统内核执行的时间,和空闲系统进程执行的时间. 下面是几个与CPU占用率相关的概念. CP ...

  8. Understanding Linux CPU stats

    Your Linux server is running slow, so you follow standard procedure and run top. You see the CPU met ...

  9. Linux CPU Load Average

    理解Linux系统负荷 LINUX下CPU Load Average的一点研究 Linux load average负载量分析与解决思路 Understanding Linux CPU Load - ...

随机推荐

  1. HDU 1671 Phone List(字符处理)

    题目 用字典树可以过,可是我写的字典树一直各种错误,,, 所以,我用了别的更简便的方法.. //去你妹的一直有问题的字典树!!! ////字典树,树的根是空的 // ////#include<i ...

  2. (1)opengl-nehe 4种框架

    http://www.yakergong.net/nehe/ 这个网站还是opengl方面比较权威的,作者叫nehe 这东西估计是要先装个ndk,然后才能运行代码 先睡觉! 以下内容参考自http:/ ...

  3. 关于linux下rar文件的解压缩操作

    在linux系统下.本身没有对rar文件操作的命令,如果需要对rar格式的文件操作,我们需要安装第三方的软件rar以及unrar. 1.linux下rar管理软件下载的官方地址为:http://www ...

  4. hadoop jobhistory解析工具汇总

    1. White Elephant是LinkedIn开源的一套Hadoop 作业日志收集器和展示器,使用mapreduce作业解析jobhistory日志,得到每个用户使用的资源情况,并通过网页展示. ...

  5. Hadoop处理大量小文件的问题和解决方法

    小文件指的是那些size比HDFS的block size(默认64M)小的多的文件.如果在HDFS中存储小文件,那么在HDFS中肯定会含有许许多多这样的小文件(不然就不会用hadoop了).而HDFS ...

  6. AA投资

    AA投资创建于2015年,总部位于北京,创始人成妙绮和王浩泽,专注于天使轮的技术创新驱动的TMT项目投资. 投资方向 AA投资是一家2015年才成立的风险投资机构,专注于种子轮.天使轮.Pre-A轮的 ...

  7. Spring框架学习之第6节

    bean的生命周期 为什么总是一个生命当做一个重点? Servlet –> servlet生命周期 Java对象生命周期 往往笔试,面试总喜欢问生命周期的问题? ①   实例化(当我们的程序加载 ...

  8. java 基本数据类型 回顾

  9. MDK4.6和J-LINK调试出现问题,软件自动关闭,在网上收集整理的解决办法

    MDK4.6配J-LINK调试时提示升级,升级完成后,弹出下图提示框后,软件自动退出. 提示原因:由于MDK4.6能识别山寨JLINK导致.网络牛人分析如下: 今天将Keil MDK升级到了V4.54 ...

  10. ubuntu下搭建cocos2dx编程环境-中

        上篇文章里讲了在ubuntu下部署cocos2d-x开发环境,这篇文章主要示范在ubuntu下部署cocos2d-x android开发环境.分开写就是因为我看很多文章里都将这两件事情混杂着写 ...