Linux CPU usage in C # include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h> // header file
Struct occupy // declare an Occupy struct
{
Char name [20]; // defines a char-type array name with 20 Elements
Unsigned int user; // defines an unsigned int-type user.
Unsigned int nice; // defines an unsigned int type nice
Unsigned INT system; // defines an unsigned int type system.
Unsigned int idle; // defines an unsigned int-type idle.
};
Float g_cpu_used; // defines a global Float Type g_cpu_used
Int cpu_num; // defines a global int type cup_num
Void cal_occupy (struct occupy *, struct occupy *); // declare that the non-typed function cal_occupy contains two struct Parameters
Void get_occupy (struct occupy *); // declare that the untyped function get_occupy contains a struct Parameter
Int main () // start the main function
{
Struct occupy ocpu [10]; // defines occupy struct variable name as ocpu containing 10 elements
Struct occupy ncpu [10]; // defines the Occupy struct variable name as ncpu containing 10 elements
Int I; // defines a local int variable I
Cpu_num = sysconf (_ SC _nprocessors_onln); // The number of CPUs returned by system calls is assigned to cpu_num.
For (;) // Infinite Loop
{
Sleep (1); // wait 1 second
Get_occupy (ocpu); // call the get function to bring back the first time of the struct Array
Sleep (1); // wait 1 second
Get_occupy (ncpu); // call the get function to bring back the second time of the struct Array
For (I = 0; I <cpu_num; I ++) // cyclically cpu_num-1 times
{
Cal_occupy (& ocpu [I], & ncpu [I]); // call the cal function to bring back the struct Array
Printf ("% F \ n", g_cpu_used); // print the usage of g_cpu_used in 6 decimal places
}
}
}
Void
Cal_occupy (struct occupy * o, struct occupy * n) // For a non-type cal function, the pointer variables O and N of the Two Parameter struct types are included.
{
Double OD, nd; // defines the dual-precision real-type variables OD, nd
Double ID, SD; // defines the double-precision real-type variable ID, SD
Double scale; // defines the scale of Double Precision Real Variables
OD = (double) (o-> User + O-> nice + O-> system + O-> idle); // The First Time (User + priority + system + idle) time is then assigned to OD
ND = (double) (N-> User + N-> nice + N-> system + N-> idle); // second (User + priority + system + idle) time is then assigned to OD
Scale = 100.0/(float) (Nd-od); // 100 except for the forced conversion (Nd-od), the difference is the float type and then assigned to the scale variable.
Id = (double) (N-> User-o-> User); // the time difference between the first and second times of the user is assigned to the ID.
SD = (double) (N-> system-o-> system); // the difference between the first and second times of the system is then assigned to SD.
G_cpu_used = (SD + id) * 100.0)/(Nd-od); // (User + System) 100) Division (time difference between the first and second times) then assign g_cpu_used
}
Void
Get_occupy (struct occupy * o) // For a non-type get function, the pointer o is obtained by a struct class of the form parameter.
{
File * FD; // defines the file pointer FD
Int N; // defines the local variable N as the int type.
Char buff [1024]; // defines the local variable buff array as char type and the size is 1024
FD = fopen ("/proc/STAT", "R"); // open the stat file in R read mode and then assign it to the pointer FD
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.
For (n = 0; n <cpu_num; n ++) // cyclically cpu_num-1 times
{
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.
/* The following describes how to convert a buff string to a data string based on the format parameter and save it to the corresponding struct parameter */
Sscanf (buff, "% S % u", & O [N]. name, & O [N]. user, & O [N]. nice, & O [N]. system, & O [N]. idle );
/* The following is the output of the Error */
Fprintf (stderr, "% S % u \ n", O [N]. name, O [N]. user, O [N]. nice, O [N]. system, O [N]. idle );
}
Fclose (FD); // close the file FD
}