linux 计算CPU使用率(C代码)

int getcpuutilization(char *cpuutilization)
{
    int sum1[2]={0};
    int sum2[2]={0};
    int i=0;
    for(i=0;i<2;i++){
        if(i==1)
        {
                sleep(3);
        }
        FILE *stream1=NULL;
        FILE *stream2=NULL;
        FILE *stream3=NULL;
        FILE *stream4=NULL;
        FILE *stream5=NULL;
        FILE *stream6=NULL;
        FILE *stream7=NULL;
        FILE *stream8=NULL;
        FILE *stream9=NULL;
        char COMMAND1[OSNAMESZ]="cat /proc/stat | sed -n 1p | awk '{print $2}'";
        char COMMAND2[OSNAMESZ]="cat /proc/stat | sed -n 1p | awk '{print $3}'";
        char COMMAND3[OSNAMESZ]="cat /proc/stat | sed -n 1p | awk '{print $4}'";
        char COMMAND4[OSNAMESZ]="cat /proc/stat | sed -n 1p | awk '{print $5}'";
        char COMMAND5[OSNAMESZ]="cat /proc/stat | sed -n 1p | awk '{print $6}'";
        char COMMAND6[OSNAMESZ]="cat /proc/stat | sed -n 1p | awk '{print $7}'";
        char COMMAND7[OSNAMESZ]="cat /proc/stat | sed -n 1p | awk '{print $8}'";
        char COMMAND8[OSNAMESZ]="cat /proc/stat | sed -n 1p | awk '{print $9}'";
        char COMMAND9[OSNAMESZ]="cat /proc/stat | sed -n 1p | awk '{print $10}'";
        char buff1[OSNAMESZ]={0};
        char buff2[OSNAMESZ]={0};
        char buff3[OSNAMESZ]={0};
        char buff4[OSNAMESZ]={0};
        char buff5[OSNAMESZ]={0};
        char buff6[OSNAMESZ]={0};
        char buff7[OSNAMESZ]={0};
        char buff8[OSNAMESZ]={0};
        char buff9[OSNAMESZ]={0};
        int cpudata1;
        int cpudata2;
        int cpudata3;
        int cpudata4;
        int cpudata5;
        int cpudata6;
        int cpudata7;
        int cpudata8;
        int cpudata9;

        stream1=popen(COMMAND1,"r");
        signal(SIGCHLD,SIG_IGN);
        stream2=popen(COMMAND2,"r");
        signal(SIGCHLD,SIG_IGN);
        stream3=popen(COMMAND3,"r");
        signal(SIGCHLD,SIG_IGN);
        stream4=popen(COMMAND4,"r");
        signal(SIGCHLD,SIG_IGN);
        stream5=popen(COMMAND5,"r");
        signal(SIGCHLD,SIG_IGN);
        stream6=popen(COMMAND6,"r");
        signal(SIGCHLD,SIG_IGN);
        stream7=popen(COMMAND7,"r");
        signal(SIGCHLD,SIG_IGN);
        stream8=popen(COMMAND8,"r");
        signal(SIGCHLD,SIG_IGN);
        stream9=popen(COMMAND9,"r");
        signal(SIGCHLD,SIG_IGN);
        if(!stream1 || !stream2 || !stream3 || !stream4 || !stream5 || !stream6 || !stream7 || !stream8 || !stream9)
        {
                return -1;
        }
        fread(buff1,sizeof(char),OSNAMESZ,stream1);
        pclose(stream1);
        fread(buff2,sizeof(char),OSNAMESZ,stream2);
        pclose(stream2);
        fread(buff3,sizeof(char),OSNAMESZ,stream3);
        pclose(stream3);
        fread(buff4,sizeof(char),OSNAMESZ,stream4);
        pclose(stream4);
        fread(buff5,sizeof(char),OSNAMESZ,stream5);
        pclose(stream5);
        fread(buff6,sizeof(char),OSNAMESZ,stream6);
        pclose(stream6);
        fread(buff7,sizeof(char),OSNAMESZ,stream7);
        pclose(stream7);
        fread(buff8,sizeof(char),OSNAMESZ,stream8);
        pclose(stream8);
        fread(buff9,sizeof(char),OSNAMESZ,stream9);
        pclose(stream9);
        if(strlen(buff1)==0 || strlen(buff2)==0 || strlen(buff3)==0 || strlen(buff4)==0 || strlen(buff5)==0 || strlen(buff6)==0 || strlen(buff7)==0 || strlen(buff8)==0 || strlen(buff9)==0)
        {
                return -1;
        }
        cpudata1=atoi(buff1);
        cpudata2=atoi(buff2);
        cpudata3=atoi(buff3);
        cpudata4=atoi(buff4);
        cpudata5=atoi(buff5);
        cpudata6=atoi(buff6);
        cpudata7=atoi(buff7);
        cpudata8=atoi(buff8);
        cpudata9=atoi(buff9);
        sum1[i]=cpudata1+cpudata2+cpudata3;
        sum2[i]=cpudata1+cpudata2+cpudata3+cpudata4+cpudata5+cpudata6+cpudata7+cpudata8+cpudata9;
    }
    sprintf(cpuutilization,"%0.1f",(float)(sum1[1]-sum1[0])/(sum2[1]-sum2[0])*100);
    return 0;
}


完!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值