Use C to obtain CPU usage

Source: Internet
Author: User

# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <unistd. h> // header file <br/> struct occupy // declare an Occupy struct <br/> {<br/> char name [20]; // define a char array name with 20 elements <br/> unsigned int user; // define an unsigned int-type user <br/> unsigned int nice; // define an unsigned int-type nice <br/> unsigned INT system; // define an unsigned int-type system <br/> unsigned int idle; // define an unsigned int-type idle <br/> }; <br/> float g_cpu_used; // define a global Float Type g_cpu_used. <br/> int cpu_num; // define a global int type cup_num <br/> void cal_occupy (struct occupy *, struct occupy *); // declare that the non-typed function cal_occupy contains two struct parameters <br/> void get_occupy (struct occupy *); // declare that the untyped function get_occupy contains a struct parameter <br/> int main () // start the main function <br/>{< br/> struct occupy ocpu [10]; // define the Occupy struct variable name as ocpu containing 10 elements <br/> struct occupy ncpu [10]; // define the Occupy struct variable name as ncpu containing 10 elements <br/> int I; // define a local int variable I </P> <p> cpu_num = sysconf (_ SC _nprocessors_onln ); // The number of CPUs returned by the system call is assigned to cpu_num <br/> for (;) // endless loop <br/>{< br/> sleep (1 ); // wait 1 second <br/> get_occupy (ocpu); // call the get function to bring back the struct array for the first time <br/> sleep (1 ); // wait 1 second <br/> get_occupy (ncpu); // call the get function to bring back the struct array for the second time <br/> for (I = 0; I <cpu_num; I ++) // cyclically cpu_num-1 Times <br/>{< br/> cal_occupy (& ocpu [I], & ncpu [I]); // call the cal function to bring back the struct array <br/> printf ("CPU [% d] = % F/N", I, g_cpu_used ); // print the usage of g_cpu_used in 6 decimal places <br/>}< br/> void <br/> cal_occupy (struct occupy * o, struct occupy * n) // For a non-type cal function, the pointer variable O and N of the two struct types of the two parameters <br/>{< br/> double OD, nd; // define the Double Precision Real Variable OD, nd <br/> double ID, SD; // define the Double Precision Real Variable ID, SD <br/> double scale; // define the scale variable with Double Precision <br/> OD = (double) (o-> User + O-> nice + O-> system + O-> idle ); // The First Time (User + priority + system + idle) is assigned to OD <br/> Nd = (double) (N-> User + N-> nice + N-> system + N-> idle); // second (User + priority + system + idle) the time is then assigned to OD <br/> scale = 100.0/(float) (Nd-od); // 100 division by force conversion (Nd-od) the difference is that the float type is assigned to the scale variable <br/> id = (double) (N-> User-o-> User ); // The difference between the first time and the second time is assigned to the ID <br/> SD = (double) (N-> system-o-> system ); // The difference between the first and second time of the system is then assigned to SD <br/> g_cpu_used = (SD + id) * 100.0)/(Nd-od ); // (User + System) 100) Division (time difference between the first time and the second time) then assigned to g_cpu_used <br/>}< br/> void <br/> get_occupy (struct occupy * O) // For the non-type get function, the pointer o obtained by the class containing a form parameter struct <br/>{< br/> file * FD; // define the file pointer FD <br/> int N; // define the local variable N as the int type <br/> char buff [1024]; // define the local variable buff array as char type with a size of 1024 </P> <p> FD = fopen ("/proc/STAT", "R "); // open the stat file as R read and then assign it to the pointer FD <br/> fgets (buff, sizeof (buff), FD ); // read the string with the buff length from the FD file and save it to the buff space at the starting address <br/> for (n = 0; n <cpu_num; n ++) // cyclically cpu_num-1 Times <br/>{< br/> fgets (buff, sizeof (buff), FD ); // read the string whose length is buff from the FD file and save it to the buff space where the starting address is buff. <br/>/* The following describes how to convert the buff string to data based on the format parameter. the result is stored in the corresponding struct parameter */<br/> sscanf (buff, "% S % u", & O [N]. name, & O [N]. user, & O [N]. nice, & O [N]. system, & O [N]. idle); <br/>/* the output is incorrect */<br/> // fprintf (stderr, "% S % u/N", O [N]. name, O [N]. user, O [N]. nice, O [N]. system, O [N]. idle); <br/>}< br/> fclose (FD); // close the file FD <br/>}< br/>

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.