zoukankan      html  css  js  c++  java
  • linux c读取CPU和内存的使用情况

    /**
    * get cpu and memory info
    * wangzeng 2009.05.04
    */
    #include
    <stdio.h>
    #include
    <unistd.h>
    #include
    <time.h>
    #include
    <sys/types.h>

    //cpu
    struct cpustatus
    {
            
    long  total;
            
    float user;
            
    float nice;
            
    float system;
            
    float idle;
    };

    //memory
    struct meminfo
    {
            
    char total[20];
            
    char free[20];
    };

    void errmsg(char *msg);
    void get_cpu_status(struct cpustatus *);
    void get_mem_info(struct meminfo *);
    void write_log(struct cpustatus *,struct meminfo *);

    int main(int ac,char *av[])
    {
    struct cpustatus cpu_stat;
    struct meminfo mem_info;
    get_cpu_status(
    &cpu_stat);
    printf(
    "user\t nice\t system\t idle\n");
    printf(
    "%4.2f\t %4.2f\t %4.2f\t %4.2f\n",cpu_stat.user,cpu_stat.nice,cpu_stat.system,cpu_stat.idle);
    get_mem_info(
    &mem_info);
    printf(
    "total:\t%s\n",mem_info.total);
    printf(
    "free:\t%s\n",mem_info.free);
    write_log(
    &cpu_stat,&mem_info);
    return 0;
    }

    //deal error
    void errmsg(char *msg)
    {
            perror(msg);
            exit(
    1);
    }

    // start write cpu and memory info by applend mode
    void write_log(struct cpustatus *cpu_stat,struct meminfo *mem_info)
    {
    FILE 
    *fp;
    long seconds;
    if((fp=fopen("cpustatuslog.txt","a+"))==NULL)
            errmsg(
    "open cpustatuslog.txt error\n");
    else
            {
            seconds
    =time((time_t*)NULL);
            fprintf(fp,
    "%s",ctime(&seconds));
            fprintf(fp,
    "user\t nice\t system\t idle\n");
            fprintf(fp,
    "%4.2f\t %4.2f\t %4.2f\t %4.2f\n",cpu_stat->user,cpu_stat->nice,cpu_stat->system,cpu_stat->idle);
            fprintf(fp,
    "total:\t%s\n",mem_info->total);
            fprintf(fp,
    "free:\t%s\n",mem_info->free);
            fprintf(fp,
    "-------------------------------\n\n");
            fclose(fp);
            }
    }

    // get cpu status
    void get_cpu_status(struct cpustatus *cpu_stat)
    {
            unsigned 
    int total;
            
    float   user;
            
    float   nice;
            
    float   system;
            
    float   idle;
            
    char   cpu[21];
            
    char   text[201];
            FILE   
    *fp;
            fp
    =fopen("/proc/stat","r");
            
    while(fgets(text,200,fp))
            {
                 
    if(strstr(text,"cpu"))
                  {
                   sscanf(text,
    "%s %f %f %f %f",cpu,&user,&nice,&system,&idle);          }
            }
            fclose(fp);
            total
    =(user+nice+system+idle);
            user
    =(user/total)*100;
            nice
    =(nice/total)*100;
            system
    =(system/total)*100;
            idle
    =(idle/total)*100;
            cpu_stat
    ->total=total;
            cpu_stat
    ->user=user;
            cpu_stat
    ->nice=nice;
            cpu_stat
    ->system=system;
            cpu_stat
    ->idle=idle;
            
    return;
    }

    //get memory info
    void get_mem_info(struct meminfo *minfo)
    {
            FILE 
    *fp;
            
    int i,j;
            
    char total[20];
            
    char free[20];
            
    char temp[80];
            
    if((fp=fopen("/proc/meminfo","r"))==NULL)
                    errmsg(
    "open meminfo error.\n");
            
    else
            {
                    
    for(i=0;i<5;i++)
                    {
                            fgets(temp,
    80,fp);
                            
    if(i==3//total info at 3 row
                                    strcpy(total,temp);
                            
    if(i==4//free info at 4 row
                                    strcpy(free,temp);
                    }
            }
            
    if(strlen(total)>0)
            {
                    
    for(i=0,j=0;i<strlen(total);i++)
                    {
                            
    if(isdigit(total[i]))
                                    minfo
    ->total[j++]=total[i];
                    }
                    minfo
    ->total[j]='\0';
            }
            
    if(strlen(free)>0)
            {
                    
    for(i=0,j=0;i<strlen(free);i++)
                    {
                            
    if(isdigit(free[i]))
                                    minfo
    ->free[j++]=free[i];
                    }
                    minfo
    ->free[j]='\0';
            }
            
    return;
    }

  • 相关阅读:
    linux查看用户组所有成员
    navicat for mysql 在Mac上安装后没有连接列表,就是左边的那一列连接项目怎么办?
    mysql启动问题access denied for user 'root'@'localhost'(using password:YES)
    phpcms多站点表单统一到主站点管理的解决方案
    thinkphp5.0 session驱动方式问题汇总
    Python__开启进程的两种方式
    Python并发编程之操作系统理论部分
    操作系统简介
    Python__基于udp的套接字
    网络编程1
  • 原文地址:https://www.cnblogs.com/ringwang/p/1453439.html
Copyright © 2011-2022 走看看