#include <stdio.h> #include <stdlib.h> #include <unistd.h> //header file struct occupy //declare an occupy Structure { char name [20]; //define a char type array name name has 20 elements unsigned int user; //define an unsigned int type of user unsigned int nice; //define an unsigned int Type of rare unsigned int system; //define an unsigned int type system unsigned int idle; //define an unsigned int type of idle }; float g_cpu_used; //define a global float type g_cpu_used int cpu_num; //Define a global int type cup_num void cal_occupy (struct occupy *, struct occupy *); //declare a typeless function cal_occupy contains two structure parameters void get_occupy (struct occupy *); //declare a typeless function Get_occupy contains a structure parameter int main () //main function start { struct occupy ocpu [10]; //define the occupy structure variable name is ocpu with 10 elements struct occupy ncpu [10]; //define the occupy structure variable The name is ncpu with 10 elements int i; //Define a local int variable i cpu_num = sysconf(_SC_NPROCESSORS_ONLN); //The system call returns the number of cpus assigned to cpu_num for(;;) //infinite loop { sleep(1 Waiting for 1 second get_occupy(ocpu); //call the get function to bring back the structure array for the first time sleep(1); //wait for 1 second get_occupy(ncpu); //call the get function to bring back the array of structures The second for (i=0; i<cpu_num; i++) //loop cpu_num-1 times { cal_occupy(&ocpu[i], &ncpu[i]); //call the cal function to bring back the array of structures Printf("%f \ ", g_cpu_used); //Print g_cpu_used usage is displayed in 6 decimal places} } } void cal_occupy (struct occupy *o, struct occupy *n)//contains two for the untyped cal function Pointer variables of the formal parameter structure type O and N { double od, nd; //define the double precision real variable od, nd double id, sd; //define the double precision real variable id, sd double scale; Define a double-precision real variable scale od = (double) (o->user + o->nice + o->system +o->idle);//first time (user+priority+system + idle) time is assigned to od nd = (doub Le) (n->user + n->nice + n->system +n->idle);//Second time (user + priority + system + idle) time is assigned to od scale = 100.0 /(float)(nd-od); //100 except for the cast (nd-od) difference is the float type and then assigned to the scale variable id = (double) (n->user - o-> User); //The difference between the first time and the second time of the user is then assigned to id sd = (double) (n->system - o->system);//system first and second time The difference in time is then assigned to sd g_cpu_used = ((sd+id)*100.0)/(nd-od); //((user + system)乖100) except (the first and second time difference) Assigned to g_cpu_used } void get_occupy (struct occupy *o) //The pointer to the untyped get function contains a formal parameter structure class O { FILE *fd; //Define the file pointer fd int n; //Define the local variable n Int type char buff[1024]; //Define the local variable buff array to char type size is 1024 fd = fopen ("/proc/stat", "r"); //Open the stat file in R read mode Then assign the pointer fd fgets (buff, sizeof(buff), fd); //read the string of length buff from the fd file and save it to the starting place. The address is buff in this space for (n = 0; n < cpu_num; n + +) //loop cpu_num -1 times { fgets (buff, size of (buff), fd); //read the length of buff from the fd file The string is saved to the starting address in the buff space. /* The following is the result of converting the buff string into the corresponding structure parameter according to the parameter format. */sscanf (buff, "%s %u %u %u %u", &o[n].name, &o[n].user, &o[n].nice,&o[n].system, &o[n] .idle); /*The following is the wrong output */fprintf (stderr, "%s %u %u %u %u\ ", o[n].name, o[n].user, o[n ].nice,o[n].system, o[n].idle); } fclose(fd); //close the file fd }
Requirement scenario Using the proxy server HAProxy to load balance Mysql is a common solution. To
Engaged in two years WindowsClient development, what multi-threading, multi-process development, ha
you want under Linux How to install the jdk you want under Linux? Last time we talked about how to u
Vinux experience operating system for the blind
Linode VPS Modify Time Zone Settings Tutorial
The correct way to restore EXT3 Superblock under Linux
How to use ReiserFS file system in Linux environment
How beginners learn Linux system command operations
Linux Find Command Mastery Guide
In the process of learning the Linux driver, I encountered a macro called container
How to cancel the Windows XP folder auto-expand function
How to make the Windows system automatically release resources
How to replace the win7 theme?
Win8 Forgot your password How to unlock BitLocker
Win7: Make good use of "library" files to operate faster
Driver Life 6 solves the common faults of the sound card driver.