Get the target computer is running Linux system hardware occupation, wrote a few small program directly after.
Proc file is read to obtain.
cpu usage: / proc / stat, memory usage: / proc / meminfo
Look at the program:
/ ************************************************* **************
* @ File: statusinfo.c
*
* @ Brief: obtained from a Linux system CPU and memory usage
*
* @ Version 1.0
*
************************************************** ************* /
typedef struct PACKED / / define a cpu occupy the structure
{
char name [20]; / / define an array of type char name name 20 elements
unsigned int the user; / / define the user of an unsigned int type
unsigned int nice; / / define an unsigned int type nice
unsigned int system ;/ / define an unsigned int type system
unsigned int idle; / / Define an unsigned int type idle
} CPU_OCCUPY;
typedef struct PACKED / / define a mem occupy the structure
{
char name [20]; / / define an array of type char name name 20 elements
unsigned long total;
char name2 [20];
unsigned long free;
} MEM_OCCUPY;
get_memoccupy (MEM_OCCUPY * mem) / / untyped get function contains a parameter structure class get pointer O
{
FILE * fd;
int n;
char BUFF [256];
MEM_OCCUPY * m;
m = mem;
fd = fopen ("/ proc / meminfo", "r");
fgets (buff, sizeof (buff), fd);
fgets (buff, sizeof (buff), fd);
fgets (buff, sizeof (buff), fd);
fgets (buff, sizeof (buff), fd);
sscanf (buff, "% s% u% s", m-> name, & m-> total, m-> name2);
fgets (buff, sizeof (buff), fd); / / read from fd file length buff string and then saved to the starting address of this space buff
sscanf (buff, "% s% u", m-> name2, & m-> free, m-> name2);
fclose (fd); / / close the file fd
}
int cal_cpuoccupy (CPU_OCCUPY * o, CPU_OCCUPY * n)
{
unsigned long od, nd;
unsigned long id, sd;
int cpu_use = 0;
OD = (unsigned long) (o-> user + O-> nice + o-> system + o-> idle) ;/ / first (user + priority + system + idle) time and then assigned to the od
nd = (unsigned long) (n-> user + nice + n-> system + n-> idle) ;/ / second (user + priority + system + idle) and then assigned to the od
id = (unsigned long) (n-> user - o-> user); / / user the first time and the second time the difference is assigned to id
sd = (unsigned long) (n-> system - o-> system) ;/ / system first and the second time the difference is assigned to sd
if ((nd-od)! = 0)
cpu_use = (int) ((sd + id) * 10000) / (nd-od); / / ((user + system) well-behaved 100) in addition to the (first and second time) is assigned to g_cpu_used
else cpu_use = 0;
/ / Printf ("cpu:% u / n", cpu_use);
return cpu_use;
}
get_cpuoccupy (CPU_OCCUPY * cpust) / / untyped get function contains a parameter structure class get pointer O
{
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", cpu_occupy-> name, & cpu_occupy-> user, & cpu_occupy-> nice, & cpu_occupy-> system, & cpu_occupy-> idle);
fclose (fd);
}
int main ()
{
CPU_OCCUPY cpu_stat1;
CPU_OCCUPY cpu_stat2;
MEM_OCCUPY mem_stat;
int cpu;
/ / Get the memory
get_memoccupy ((MEM_OCCUPY *) & mem_stat);
/ / First get cpu usage
get_cpuoccupy ((CPU_OCCUPY *) & cpu_stat1);
sleep (10);
/ / Get CPU usage
get_cpuoccupy ((CPU_OCCUPY *) & cpu_stat2);
/ / Calculate the cpu utilization
cpu = cal_cpuoccupy ((CPU_OCCUPY *) & cpu_stat1, (CPU_OCCUPY *) & cpu_stat2);
return 0;
}