Linux C programming Get native CPU utilization memory usage hard disk usage __ block chain

Source: Internet
Author: User
Tags memory usage cpu usage disk usage

1. Get CPU Utilization

The typedef struct CPU_PACKED         //defines a CPU occupy structure body
{
char name[20];             Defines a char-type array name with 20 elements
unsigned int user;        Defines a user unsigned int of unsigned type int nice
;        Defines a nice unsigned int system of unsigned int types
;    Defines a system unsigned int idle with unsigned int type
;         Defines an unsigned int-type idle
unsigned int iowait;
unsigned int IRQ;
unsigned int softirq;
} Cpu_occupy;

Main program:

Double Cal_cpuoccupy (cpu_occupy *o, Cpu_occupy *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);//First time (user + priority + System + Idle time is then assigned to od nd = (double) (N->user + n->nice + n->system +N->IDLE+N->SOFTIRQ+N->IOWAIT+N->IRQ)    //second (user + priority + system + Idle) time is assigned to OD id = (double) (n->idle);    The difference between the first and second times of the user is then assigned to the ID SD = (double) (o->idle); The time difference between the first and second times of the system is then assigned to the SD if ((Nd-od)!= 0) Cpu_use =100.0-((ID-SD))/(Nd-od) *100.00;
    (user + system) good 100) in addition to (the first and second time difference) and then assigned to g_cpu_used else Cpu_use = 0;
return cpu_use;
    } void Get_cpuoccupy (Cpu_occupy *cpust) {FILE *fd;
    int n;
    Char buff[256];
    Cpu_occupy *cpu_occupy;

    Cpu_occupy=cpust;
    FD = fopen ("/proc/stat", "R");

    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 Getcpurate () {cpu_occupy cpu_stat1;
    Cpu_occupy Cpu_stat2;
    Double CPU;
    Get_cpuoccupy ((Cpu_occupy *) &CPU_STAT1);

    Sleep (1);

    Second get CPU usage get_cpuoccupy ((cpu_occupy *) &CPU_STAT2);

    Compute CPU Usage CPU = cal_cpuoccupy ((cpu_occupy *) &CPU_STAT1, (Cpu_occupy *) &CPU_STAT2);
return CPU; }

2. Get Memory usage

typedef struct MEM_PACKED         //Definition of a MEM occupy structure body
{
        char name[20];      Defines a char type array name with 20 elements
        unsigned long total;
        Char name2[20];
} Mem_occupy;


typedef struct MEM_PACK         //define a MEM occupy structure body
{
        double total,used_rate;
} Mem_pack;
Main program:

Mem_pack *get_memoccupy ()    //Get RAM message
{
    FILE *fd;
    int n;
    Double mem_total,mem_used_rate;;
    Char buff[256];
    Mem_occupy *m= (Mem_occupy *) malloc (sizeof (mem_occupy))
    Mem_pack *p= (Mem_pack *) malloc (sizeof (Mem_pack));
    FD = fopen ("/proc/meminfo", "R");

    Fgets (buff, sizeof (buff), FD);
    SSCANF (Buff, "%s%lu%s\n", M->name, &m->total, m->name2);
    mem_total=m->total;
    Fgets (buff, sizeof (buff), FD);
    SSCANF (Buff, "%s%lu%s\n", M->name, &m->total, m->name2);
    Mem_used_rate= (1-m->total/mem_total) *100;
    mem_total=mem_total/(1024*1024);
    p->total=mem_total;
    p->used_rate=mem_used_rate;
    Fclose (FD);     Close file fd return
    p;
}

3. Get the use of hard disk

typedef struct DEV_MEM         //define a MEM occupy structure body
{
        double total,used_rate;
} Dev_mem;

Main program:

Dev_mem *get_devmem ()        //Get Hard disk meeeage
{
        FILE * FP;
        int h=0;
        Char buffer[80],a[80],d[80],e[80],f[80],buf[256];
        Double c,b;
        Fp=popen ("DF", "R");
        Fgets (BUF,256,FP);
        Double dev_total=0,dev_used=0;
        Dev_mem  *dev= (DEV_MEM *) malloc (sizeof (DEV_MEM));
        while (6==FSCANF (FP, "%s%lf%lf%s%s", a,&b,&c,d,e,f))
        {
                dev_total+=b;
                dev_used+=c;
        }
        dev->total=dev_total/1024/1024;;
        dev->used_rate=dev_used/dev_total*100;
        Pclose (FP);
        return dev;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.