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. Java 并发同步器之CountDownLatch、CyclicBarrier

    一.简介 1.CountDownLatch是一个同步计数器,构造时传入int参数,该参数就是计数器的初始值,每调用一次countDown()方法,计数器减1,计数器大于0 时,await()方法会阻塞 ...

  2. vc++ 获取当前用户名

    #include<afxwin.h> #include <stdio.h> int main(void) { char userName[MAX_PATH]; unsigned ...

  3. POJ 2080

    import java.util.*; public class Main { public static void main(String args[]){ Scanner cin=new Scan ...

  4. Android图片缩放方法

    安卓开发中应用到图片的处理时候,我们通常会怎么缩放操作呢,来看下面的两种做法: 方法1:按固定比例进行缩放 在开发一些软件,如新闻客户端,很多时候要显示图片的缩略图,由于手机屏幕限制,一般情况下,我们 ...

  5. java如何追加写入txt文件

    java中,对文件进行追加内容操作的三种方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import java.io.BufferedWriter; import  ...

  6. mysql变量使用总结

    set语句的学习: 使用select定义用户变量的实践将如下语句改成select的形式: set @VAR=(select sum(amount) from penalties);我的修改: sele ...

  7. Spring框架学习之第5节

    request session global-session 三个在web开发中才有意义 如果配置成prototype有点类似于request 如果配置成singleton有点类似于web开发中的gl ...

  8. [软件]XAMPP错误解决

    // 错误1  (在运行安装包时候出现) // 错误2 1. 找到这个文件 这个文件  要将 config.inc.php 中 $cfg['Servers'][$i]['host'] = ’local ...

  9. 文件夹属性中只有"常规"解决办法

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\shellex\PropertySheetHandlers] [HK ...

  10. java string,需要进行首字母大写改写

    java string,需要进行首字母大写改写,网上大家的思路基本一致,就是将首字母截取,转化成大写然后再串上后面的,类似如下代码 //首字母大写     public static String c ...