获取linux的CPU,内存,磁盘的C源代码

#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;
}


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值