Linux系统下获取系统的CPU占用率(C语言代码)
【摘要】
一、功能介绍
通过读取/proc/stat文件获取当前系统的CPU占用率。
Linux系统上的/proc目录是一种文件系统,即proc文件系统,与其它常见的文件系统不同的是,/proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间。它以文件系统的方式为内核与进程提供通信的接口。用户和应用程序可以通过/proc得到系统的...
一、功能介绍
通过读取/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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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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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系统上运行效果
文章来源: xiaolong.blog.csdn.net,作者:DS小龙哥,版权归原作者所有,如需转载,请联系作者。
原文链接:xiaolong.blog.csdn.net/article/details/108313827
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)