Linux c 语言怎么方便的读取 cpu,磁盘信息

Linux c 语言怎么方便的读取 cpu,磁盘信息,第1张

我附上我的代码给你参考。

CPU占用 需要查看/proc/stat 的信息

磁盘需要 使用statfs这个函数来确认文件所包含的信息。

我附上我的代码给你参考。

我的代码支持CPU使用率(占用率),内存占用率,及磁盘占用率。

#include <stdio.h>

#include <stdlib.h>

#include <sys/time.h>

#include <unistd.h>

#include <sys/vfs.h>

#include <error.h>

#define Gsize (1024.00*1024.00*1024.00)

#define Msize (1024.00*1024.00)

#ifndef EXT2_SUPER_MAGIC

#define EXT2_SUPER_MAGIC 0xef53

#endif

double time_so_far()

float get_cpu_rate()

float get_memory_rate()

float get_disk_rate()

int main(int argc,char *argv[])

{

    get_cpu_rate()

    get_memory_rate()

    get_disk_rate()

    return 0

}

double time_so_far(){

    struct timeval tp

    if(gettimeofday(&tp,(struct timezone *)NULL) == -1)

        perror("gettimeofday")

    return ((double)(tp.tv_sec))+(((double)tp.tv_usec)*0.000001)

}

float get_cpu_rate(){

    FILE *f1

    double ti,tf

    char c[10],d[10]

    int t,i1,i2,i3,i4,i5,i6

    

    ti=time_so_far()

    f1=fopen("/proc/stat","r")

    fscanf(f1,"%s\t%d\t%d\t%d\n",c,&i1,&i2,&i3)

    fclose(f1)

    printf("%s\t%d\t%d\t%d\n",c,i1,i2,i3)

    usleep(1000000)

    tf=time_so_far()

    f1=fopen("/proc/stat","r")

    fscanf(f1,"%s\t%d\t%d\t%d\n",c,&i4,&i5,&i6)

    fclose(f1)

     printf("%s\t%d\t%d\t%d\n",c,i4,i5,i6)

    t=(i4+i5+i6)-(i1+i2+i3)

    printf("%d\n",t)

    printf("cpu usage: %.2f%%\n",( t/((tf-ti)*100) )*100 )

}

float get_memory_rate(){

    FILE *f1

    int itemp1,itemp2

    char c[10],d[10]

    f1=fopen("/proc/meminfo","r")

    fscanf(f1,"%s\t%d\t%s",c,&itemp1,d)

    printf("memory total is %d Kb\n",itemp1)

    printf("memory total is %.2f Mb\n",itemp1/1024.0)

    fscanf(f1,"%s\t%d\t%s",c,&itemp2,d)

    printf("memory free is %d Kb\n",itemp2)

    printf("memory free is %.2f Mb\n",itemp2/1024.0)

    fclose(f1)

    printf("men usage : %.2f%%\n",((itemp1-itemp2)*100.0)/itemp1)

}

float get_disk_rate(){

    struct statfs *fs

    long long blocks,bfree

    if(statfs("/",fs) != 0)

        {

            perror("stafts")

            printf("exit\n")

            exit(1)

        }

    blocks=fs->f_blocks

    bfree=fs->f_bfree

    //if(fs.f_type == EXT2_SUPER_MAGIC)

    //{

        printf("Disk size of / is %.2f G\n",blocks*fs->f_bsize/Gsize)

        printf("Free Disk size of / is %.2f G\n",bfree*fs->f_bsize/Gsize)

        printf("Disk usage of / is %.2f%% \n",bfree*100.0/blocks)

    //}

}

linux下面的概念是一切皆文件。所以没有像c盘d盘这样的东西,有的只是各种各样的文件夹和文件。要读一个linux下面的文件很简单,命令pwd可以得到当前路径,然后路径接上你打开的文件名就可以知道这个文件的详细路径了。直接open就可以了。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7432109.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-05
下一篇 2023-04-05

发表评论

登录后才能评论

评论列表(0条)

保存