获取linux的CPU,内存,磁盘的C源代码
#include#include#include#include#include#include#define Gsize (1024.00*1024.00*1024.00)#define Msize (1024.00*1024.
·
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/vfs.h>
#include <error.h>
#define Gsize (1024.00*1024.00*1024.00)
#define Msize (1024.00*1024.00)
#ifndef EXT2_SUPER_MAGIC
#define EXT2_SUPER_MAGIC 0xef53
#endif
double time_so_far()
{
struct timeval tp;
if (gettimeofday(&tp, (struct timezone *) NULL) == -1)
perror("gettimeofday");
return ((double) (tp.tv_sec)) +(((double) tp.tv_usec) * 0.000001 );
}
int main()
{
FILE *f1;
double ti, tf;
char c[10],d[10];
int i1,i2,i3,i4,i5,i6;
/*cpu information*/
ti = time_so_far();
f1 = fopen("/proc/stat", "r");
fscanf(f1, "%s\t%d\t%d\t%d\n", c, &i1, &i2, &i3);
fclose(f1);
usleep(1000000);
tf = time_so_far();
f1 = fopen("/proc/stat", "r");
fscanf(f1, "%s\t%d\t%d\t%d\n", c, &i4, &i5, &i6);
fclose(f1);
int t = (i4+i5+i6)-(i1+i2+i3);
printf("cpu usage: %.1f%%\n", (t / ((tf-ti) * 100)) * 100);
/*memory information*/
f1 = fopen("/proc/meminfo","r");
fscanf(f1, "%s\t%d\t%s",c,&i1,d);
printf("mem total: %d\n", i1);
fscanf(f1, "%s\t%d\t%s",c,&i1,d);
printf("mem free: %d\n", i1);
fclose(f1);
/*disk information*/
long long blocks,bfree;
struct statfs fs;
if(statfs("/",&fs)<0)
{
perror("statfs");
exit(0);
}
blocks=fs.f_blocks;
bfree=fs.f_bfree;
if(fs.f_type==EXT2_SUPER_MAGIC)
{
printf("Total size of / is %f G\n",blocks*fs.f_bsize/Gsize);
printf("Free size of / is %f G\n",bfree*fs.f_bsize/Gsize);
}
return 0;
}
分开来写
/*cpu information*/
float get_cpu()
{
float cpu=0.0;
char c[20];
int i1,i2,i3,i4,i5,i6;
struct timeval tpstart,tpend;
float timeuse;
gettimeofday(&tpstart,NULL);
FILE* f1 = fopen("/proc/stat", "r");
fscanf(f1, "%s\t%d\t%d\t%d\n", c, &i1, &i2, &i3);
fclose(f1);
usleep(1000000);
gettimeofday(&tpend,NULL);
timeuse = 1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
timeuse /= 1000000;
f1 = fopen("/proc/stat", "r");
fscanf(f1, "%s\t%d\t%d\t%d\n", c, &i4, &i5, &i6);
fclose(f1);
int t = (i4+i5+i6)-(i1+i2+i3);
cpu = t /timeuse;
return cpu;
}
/*memory information*/
unsigned int get_mem_total()
{
unsigned int mem_total=0;
char c[20],d[20];
FILE* f1 = fopen("/proc/meminfo","r");
fscanf(f1, "%s\t%d\t%s",c,&mem_total,d);
fclose(f1);
return mem_total/1024;//MB
}
unsigned int get_mem_free()
{
unsigned int mem_free=0;
char c[20],d[20];
FILE* f1 = fopen("/proc/meminfo","r");
fscanf(f1, "%s\t%d\t%s",c,&mem_free,d);
fclose(f1);
return mem_free/1024;//MB
}
float get_disk_total()
{
float disk_total=0;
long long blocks;
struct statfs fs;
if(statfs("/",&fs)<0)
{
perror("statfs");
exit(0);
}
blocks=fs.f_blocks;
if(fs.f_type==EXT2_SUPER_MAGIC)
{
disk_total=blocks*fs.f_bsize/Gsize;
}
return disk_total;
}
float get_disk_free()
{
float disk_free=0;
long long bfree;
struct statfs fs;
if(statfs("/",&fs)<0)
{
perror("statfs");
exit(0);
}
bfree=fs.f_bfree;
if(fs.f_type==EXT2_SUPER_MAGIC)
{
disk_free = bfree*fs.f_bsize/Gsize;
}
return disk_free;
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献3条内容
所有评论(0)