/* here are no errors and it can run normally */
#include
#include
#include
#include
#include
#include
#define cpu_info_size 1024
#define sleep 3
/*#define states_line2x4 "%s/03" /
" %#5.1f%% /02user,/03 %#5.1f%% /02system,/03 %#5.1f%% ,nice : %#5.1f%% /02idle/03/n"
*/#define states_line2x4 "%s ," /
" user: %#5.1f%% , system: %#5.1f%% , idle: %#5.1f%% /n"
static const char *states_fmts = states_line2x4;
//total number of cpu
static int cpu_tot;
// these typedefs attempt to ensure consistent 'ticks' handling
typedef unsigned long long tic_t;
// this structure stores a frame's cpu tics used in history
// calculations. it exists primarily for smp support but serves
// all environments.
typedef struct cpu_t cpu_t;
// this routine simply formats whatever the caller wants and
// returns a pointer to the resulting 'const char' string...
static const char *fmtmk (const char *fmts, ...)
/* refresh cpu information */
static cpu_t *cpus_refresh (cpu_t *cpus)
rewind(fp);
fflush(fp);
// first value the last slot with the cpu summary line
if (!fgets(buf, sizeof(buf), fp))
printf("failed /proc/stat read/n");
cpus[cpu_tot].x = 0; // fixme: can't tell by kernel version number
cpus[cpu_tot].y = 0; // fixme: can't tell by kernel version number
num = sscanf(buf, "cpu %lu %lu %lu %lu %lu %lu %lu",
&cpus[cpu_tot].u,
&cpus[cpu_tot].n,
&cpus[cpu_tot].s,
&cpus[cpu_tot].i,
&cpus[cpu_tot].w,
&cpus[cpu_tot].x,
&cpus[cpu_tot].y
);if (num < 4)
printf("failed /proc/stat read/n");
// and just in case we're 2.2.xx compiled without smp support...
if (cpu_tot == 1)
// now value each separate cpu's tics
for (i = 0; 1 < cpu_tot && i < cpu_tot; i++)
return cpus;
}return information;
}/* get all of cpu's information */
char * get_cpu_info()
/*當使用時只需掉用get_cpu_info即可得到cpu使用率,返回的為陣列首位址*/
獲得cpu的使用率 c )
include include include define systembasicinformation 0 define systemperformanceinformation 2 define systemtimeinformation 3 define li2double x double...
linux下持續顯示CPU使用率
環境 rhel6.0 cpu使用率 proc stat 這裡要從 proc stat中提取四個資料 使用者模式 user 低優先順序的使用者模式 nice 核心模式 system 以及空閒的處理器時間 idle 它們均位於 proc stat檔案的第一行。cpu的利用率使用如下公式來計算。cpu利用...
Linux計算cpu使用率
計算總的 cpu 使用率 totalcpuuse 1 取樣兩個足夠短的時間間隔的 cpu 快照,即讀取 proc stat 檔案,獲取兩個時間點的下列資料 cput1 user1,nice1,system1,idle1,iowait1,irq1,softirq1,stealstolen1,guest...