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. 开源搜索引擎Solr的快速搭建及集成到企业门户最佳实施方案--转载

    笔者经过研究查阅solr官方相关资料经过两周的研究实现了毫秒级百万数据的搜索引擎的搭建并引入到企业门户.现将实施心得和步骤分享一下. 1.      jdk1.6 安装jdk1.6到系统默认目录下X: ...

  2. HDU 2048 神、上帝以及老天爷(递归,错排,dp,概率)

    中文题,错排,求概率,不解释,核心思路同 HDU 1465 错排简单思路可看:http://www.cnblogs.com/laiba2004/p/3235934.html //错排,但是我之前叫了几 ...

  3. POJ 1939

    #include<iostream> #include<iomanip> #define MAXN 10000 using namespace std; ]; int main ...

  4. (转)两分钟彻底让你明白Android Activity生命周期(图文)!

    转自: http://blog.csdn.net/android_tutor/article/details/5772285 大家好,今天给大家详解一下Android中Activity的生命周期,我在 ...

  5. 编程实现Linux下的ls -l

    头文件 #ifndef __FUNC_H__ #define __FUNC_H__ #include <stdio.h> #include <stdlib.h> #includ ...

  6. linux下对普通用户设置文件访问控命令之setfacl

    命令名 setfacl  -设置文件访问控制列表 常用用法:setfacl [-bkRd] [{-m|-x} acl参数] 目标文件名 命令的常用参数 -m 设置后续的acl参数给文件使用(常用). ...

  7. BeanUtils.copyProperties与PropertyUtils.copyProperties用法及区别

    一.简介: BeanUtils提供对Java反射和自省API的包装.其主要目的是利用反射机制对JavaBean的属性进行处理.我们知道,一个JavaBean 通常包含了大量的属性,很多情况下,对Jav ...

  8. 执行脚本出现bin/bash: bad interpreter: No such file or directory

    -bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory VI打开文件,没发现任何问题, 把/bin/bash ...

  9. MFC的GUI窗口使用Console输出函数printf(AllocConsole后,使用GetStdHandle取得句柄,然后就可以操作了)

    在GUI程序中使用printf函数: #include <io.h> #include <fcntl.h> void InitConsole() { int nRet= 0; ...

  10. Mysql笔记——DDL

    数据库模式定义语言DDL(Data Definition Language),是用于描述数据库中要存储的现实世界实体的语言.一个数据库模式包含该数据库中所有实体的描述定义.   =========== ...