Linux系统开发中,在使用statfs统计分区空间时,要注意f_bfree和f_bavail两个值的区别。
实验一下:
以/boot分区为例,上面使用C代码查看分区信息,下面使用系统命令df查看分区信息
#include <stdio.h>
#include <sys/vfs.h>
int main()
{
struct statfs sfs;
int i = statfs("/boot", &sfs);
int percent = (sfs.f_blocks - sfs.f_bfree ) * 100 / (sfs.f_blocks -
sfs.f_bfree + sfs.f_bavail) + 1;
printf("/dev/sda1 %ld %ld %ld %d%% /boot\n",
4*sfs. f_blocks, 4*(sfs.f_blocks - sfs.f_bfree),
4*sfs.f_bavail, percent);
system("df /boot ");
return 0;
}