当前位置: 技术问答>linux和unix
求linux下查看内存及硬盘使用情况的程序
来源: 互联网 发布时间:2016-05-19
本文导语: 我刚刚接触linux下的C编程,现在想写两个函数,一个得到当前内存使用情况,包括总共和未用,另一个获取硬盘的使用情况,包括总共和未用。我刚刚接触这块,还请各位说的详细些,要加上哪些头文件也请说明,谢...
我刚刚接触linux下的C编程,现在想写两个函数,一个得到当前内存使用情况,包括总共和未用,另一个获取硬盘的使用情况,包括总共和未用。我刚刚接触这块,还请各位说的详细些,要加上哪些头文件也请说明,谢谢!!
|
硬盘空间用statfs()
|
#include
#include
#include
#include
//获取内存信息
int getmeminfo(double * total,double * used ,double * free)
{
struct sysinfo info;
sysinfo(&info);
*total = (double)info.totalram/(1024.0*1024.0);
*used = (double)(info.totalram-info.freeram)/(1024.0*1024.0);
*free = (double)info.freeram/(1024.0*1024.0);
return 0;
}
//获取分区使用情况
int gethd(char *path)
{
struct statfs stat;
//statfs(path,&stat);
//printf("total=%dM,free=%dM,%.1f%%n",stat.f_bsize*stat.f_blocks/1024/1024,stat.f_bsize*stat.f_bfree/1024/1024,(float)stat.f_bfree/(float)stat.f_blocks*100);
struct statvfs stat1;
statvfs(path,&stat1);
if(stat1.f_flag)
printf("%s total=%dK free=%dK %0.1f%%n",path,stat1.f_bsize*stat1.f_blocks/1024,stat1.f_bsize*stat1.f_bfree/1024,((float)stat1.f_blocks-(float)stat1.f_bfree)/(float)stat1.f_blocks*100);
}
#include
#include
#include
//获取内存信息
int getmeminfo(double * total,double * used ,double * free)
{
struct sysinfo info;
sysinfo(&info);
*total = (double)info.totalram/(1024.0*1024.0);
*used = (double)(info.totalram-info.freeram)/(1024.0*1024.0);
*free = (double)info.freeram/(1024.0*1024.0);
return 0;
}
//获取分区使用情况
int gethd(char *path)
{
struct statfs stat;
//statfs(path,&stat);
//printf("total=%dM,free=%dM,%.1f%%n",stat.f_bsize*stat.f_blocks/1024/1024,stat.f_bsize*stat.f_bfree/1024/1024,(float)stat.f_bfree/(float)stat.f_blocks*100);
struct statvfs stat1;
statvfs(path,&stat1);
if(stat1.f_flag)
printf("%s total=%dK free=%dK %0.1f%%n",path,stat1.f_bsize*stat1.f_blocks/1024,stat1.f_bsize*stat1.f_bfree/1024,((float)stat1.f_blocks-(float)stat1.f_bfree)/(float)stat1.f_blocks*100);
}