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基础类:Object类和Math类

    1.2.3.4.5.6.7.7.

  2. Xmarks丢失书签

    想体验下Xmarks,不同浏览器同步书签,听说很好用,就安装Chrome插件,没想到竟然把我的所有书签都丢了. 不过在网上找到了回复的办法,也很简单: 原始地址:http://irising.me/2 ...

  3. 【动态规划】 之最长公共子序列LCS

    int lcs_len(char *a, char *b, int c[][N]){ int aLen=strlen(a), bLen=strlen(b), i,j; ; i<=aLen; i+ ...

  4. 安装wps for linux无法启动

    我下载的是deb包,双击安装完之后,打开wps没有反应,重启了一下 计算机也不行. 改从命令行出现如下信息: /opt/kingsoft/wps-office/office6/wps: error w ...

  5. OleDbType.Decimal在插入DB2后会默认赋值0.00,改为OleDbType.Double则正常

    private void InsertShopClaimsTarget(ContinueTargetData.RT_SHOPCLAIMSTARGETRow aRow) { StringBuilder ...

  6. 有用的一些web网站

    1.http://www.aseoe.com/api-download/download.html 爱思资源网

  7. 【android原生应用】之闹钟应用搭起篇

    由于工作原因接触android开发一段时间了,对于开发有了一些了解,于是萌生了搭起android原生应用进行分析和学习的想法.先从闹钟应用开始吧. 1.首先要下载原生应用,原生应用在原生系统里面(当然 ...

  8. ArcGIS Engine -- 常用方法

    空间关系 计算两点间距离 feature平移 计算范围 得到点集合的n倍Envelope范围 查询 查询要素,返回多个要素union后的Geometry 查找图层 得到地图上图层列表 根据名称在地图上 ...

  9. MAC终端配色Solarized

    MAC终端配色Solarized $ git clone git://github.com/altercation/solarized.git 在 solarized/osx-terminal.app ...

  10. linux系统主要常见目录结构

    linux系统的一切从“/”开始,并按照文件系统目录标准FHS采用树桩结构来存放文件 并定义了每个区域的用途.