如何在linux中查找CPU负载并使用grep命令?

时间:2017-08-03 04:26:36

标签: c linux grep

**Without grep**

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    long double a[4], b[4], loadavg;
    FILE *fp;
    char dump[50];

    for(;;)
    {
        fp = fopen("/proc/stat","r");
        fscanf(fp,"%*s %Lf %Lf %Lf %Lf",&a[0],&a[1],&a[2],&a[3]);
        fclose(fp);
        sleep(1);

        fp = fopen("/proc/stat","r");
        fscanf(fp,"%*s %Lf %Lf %Lf %Lf",&b[0],&b[1],&b[2],&b[3]);
        fclose(fp);

        loadavg = ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
        printf("The current CPU utilization is : %Lf\n",loadavg);
    }
    return(0);
 }

我试过这个,但它没有显示正确的结果,也没有grep命令。我也找到了其他解决方案,但都显示错误答案。我也找不到Direct命令。如果有人有任何想法,那么建议我一样。

0 个答案:

没有答案