一、功能介紹
通過讀取/proc/stat檔案擷取目前系統的CPU占用率。
Linux系統上的/proc目錄是一種檔案系統,即proc檔案系統,與其它常見的檔案系統不同的是,/proc檔案系統是一個僞檔案系統,它隻存在記憶體當中,而不占用外存空間。它以檔案系統的方式為核心與程序提供通信的接口。使用者和應用程式可以通過/proc得到系統的資訊,并可以改變核心的某些參數。由于系統的資訊,如程序,是動态改變的,是以使用者或應用程式讀取/proc目錄中的檔案時,proc檔案系統是動态從系統核心讀出所需資訊并送出的。
/proc/stat檔案包含了所有CPU活動的資訊,該檔案中的所有值都是從系統啟動開始累計到目前時刻。不同核心版本中該檔案的格式可能不大一緻。
wbyq@wbyq:/mnt/hgfs/linux-share-dir/linux_c$ cat /proc/stat
cpu 673896 219583 260680 4878312 1821 0 4364 0 0 0
cpu0 320077 75898 124098 2490890 1231 0 2451 0 0 0
cpu1 353819 143684 136582 2387421 589 0 1913 0 0 0
intr 16591367 3 19561 0 0 0 0 0 0 1 0 0 0 1033239 0 0 0 360062 318044 40 343791 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1154266 0 0 15434 5315 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 28277855
btime 1598774265
processes 117139
procs_running 1
procs_blocked 0
softirq 9953753 74 4098860 4860 347201 336159 0 26182 2177955 0 2962462
wbyq@wbyq:/mnt/hgfs/linux-share-dir/linux_c$
二、代碼實作
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
typedef struct cpu_occupy_ //定義一個cpu occupy的結構體
{
char name[20]; //定義一個char類型的數組名name有20個元素
unsigned int user; //定義一個無符号的int類型的user
unsigned int nice; //定義一個無符号的int類型的nice
unsigned int system; //定義一個無符号的int類型的system
unsigned int idle; //定義一個無符号的int類型的idle
unsigned int iowait;
unsigned int irq;
unsigned int softirq;
}cpu_occupy_t;
double cal_cpuoccupy (cpu_occupy_t *o, cpu_occupy_t *n)
{
double od, nd;
double id, sd;
double cpu_use ;
od = (double) (o->user + o->nice + o->system +o->idle+o->softirq+o->iowait+o->irq);//第一次(使用者+優先級+系統+空閑)的時間再賦給od
nd = (double) (n->user + n->nice + n->system +n->idle+n->softirq+n->iowait+n->irq);//第二次(使用者+優先級+系統+空閑)的時間再賦給od
id = (double) (n->idle); //使用者第一次和第二次的時間之差再賦給id
sd = (double) (o->idle) ; //系統第一次和第二次的時間之差再賦給sd
if((nd-od) != 0)
cpu_use =100.0 - ((id-sd))/(nd-od)*100.00; //((使用者+系統)乖100)除(第一次和第二次的時間差)再賦給g_cpu_used
else
cpu_use = 0;
return cpu_use;
}
void get_cpuoccupy (cpu_occupy_t *cpust)
{
FILE *fd;
int n;
char buff[256];
cpu_occupy_t *cpu_occupy;
cpu_occupy=cpust;
fd = fopen ("/proc/stat", "r");
if(fd == NULL)
{
perror("fopen:");
exit (0);
}
fgets (buff, sizeof(buff), fd);
sscanf (buff, "%s %u %u %u %u %u %u %u", cpu_occupy->name, &cpu_occupy->user, &cpu_occupy->nice,&cpu_occupy->system, &cpu_occupy->idle ,&cpu_occupy->iowait,&cpu_occupy->irq,&cpu_occupy->softirq);
fclose(fd);
}
double get_sysCpuUsage()
{
cpu_occupy_t cpu_stat1;
cpu_occupy_t cpu_stat2;
double cpu;
get_cpuoccupy((cpu_occupy_t *)&cpu_stat1);
sleep(1);
//第二次擷取cpu使用情況
get_cpuoccupy((cpu_occupy_t *)&cpu_stat2);
//計算cpu使用率
cpu = cal_cpuoccupy ((cpu_occupy_t *)&cpu_stat1, (cpu_occupy_t *)&cpu_stat2);
return cpu;
}
int main(int argc,char **argv)
{
while(1)
{
printf("CPU占用率:%f\n",get_sysCpuUsage());
}
return 0;
}
三、ubuntu18.04下運作效果

四、redhat6.3系統上運作效果