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. close和shutdown的区别

    转的,没验证 close(sock_fd)会把sock_fd的内部计数器减1当sock_fd的内部计数器为0时, 才调用shutodwn(), 并最终释放文件描述符调用shutdown()只是进行了T ...

  2. javascript和swf在网页中交互的一些总结

    Javascript和swf在网页中交互一般可有以下几种情况: 1.swf和这些调用的javascript在同域 2.swf和这些调用的javascript在不同域,比如加载远程的swf然后call别 ...

  3. HDU 4639 Hehe(字符串处理,斐波纳契数列,找规律)

    题目 //每次for循环的时候总是会忘记最后一段,真是白痴.... //连续的he的个数 种数 //0 1 //1 1 //2 2 //3 3 //4 5 //5 8 //…… …… //斐波纳契数列 ...

  4. db2日期和时间常用汇总

    1.db2可以通过SYSIBM.SYSDUMMY1.SYSIBM.DUAL获取寄存器中的值,也可以通过VALUES关键字获取寄存器中的值. SELECT 'HELLO DB2' FROM SYSIBM ...

  5. spring 主题使用详解[转]

    在common_include_v2.jsp文件中,spring主题的使用: <link href="${staticPath }/<spring:theme code='sty ...

  6. 机器学习之神经网络模型-上(Neural Networks: Representation)

    在这篇文章中,我们一起来讨论一种叫作"神经网络"(Neural Network)的机器学习算法,这也是我硕士阶段的研究方向.我们将首先讨论神经网络的表层结构,在之后再具体讨论神经网 ...

  7. 编程实现LINUX下目录的层层遍历

    /************************************************************************* > File Name: treedir.c ...

  8. DP+矩阵快速幂 HDOJ 5318 The Goddess Of The Moon

    题目传送门 /* DP::dp[i][k] 表示选择i个字符串,最后一次是k类型的字符串,它由sum (dp[i-1][j]) (a[j], a[k] is ok)累加而来 矩阵快速幂:将n个字符串看 ...

  9. Hadoop基础教程之HelloWord

    上一章中,我们把hadoop下载.安装.运行起来,最后还执行了一个Hello world程序,看到了结果.现在我们就来解读一下这个Hello Word. OK,我们先来看一下当时在命令行里输入的内容: ...

  10. 编写基于outlook显示的html邮件需要注意的问题

    outlook对于html的支持是有限制的,在编写这些html的时候注意遵循以下规则: Never use colspans or rowspans. Always set correct dimen ...