f_bfree和f_bavail的区别

8260阅读 0评论2013-12-25 linuxnerd
分类:C/C++


Linux系统开发中,在使用statfs统计分区空间时,要注意f_bfree和f_bavail两个值的区别。
实验一下:
以/boot分区为例,上面使用C代码查看分区信息,下面使用系统命令df查看分区信息

  1. #include <stdio.h>
  2. #include <sys/vfs.h>

  3. int main()
  4. {
  5.     struct statfs sfs;
  6.     int i = statfs("/boot", &sfs);
  7.     int percent = (sfs.f_blocks - sfs.f_bfree ) * 100 / (sfs.f_blocks -

  8. sfs.f_bfree + sfs.f_bavail) + 1;
  9.     printf("/dev/sda1 %ld %ld %ld %d%% /boot\n",
  10.                            4*sfs. f_blocks, 4*(sfs.f_blocks - sfs.f_bfree),

  11.     4*sfs.f_bavail, percent);
  12.     system("df /boot ");
  13.     return 0;

  14. }






  1. #include <stdio.h>
  2. #include <sys/vfs.h>

  3. int main()
  4. {
  5.     struct statfs sfs;
  6.     int i = statfs("/boot", &sfs);
  7.     int percent = (sfs.f_bfree - sfs.f_bavail ) * 100 / sfs.f_blocks;
  8.         printf("/dev/sda1 free=%ld avail=%ld block=%ld block-free=%ld percent=%d%% /boot\n", 4*sfs. f_bfree, 4*sfs. f_bavail, 4*sfs. f_blocks, 4*(sfs.f_bfree - sfs.f_bavail), percent);
  9.         return 0;

  10. }


可以看到f_bfree和f_bavail两个值的区别,前者是硬盘所有剩余空间,后者为非root用户剩余空间。一般ext3文件系统会给root留5%的独享空间。所以如果计算出来的剩余空间总比df显示的要大,那一定是你用了f_bfree。 5%的空间大小这个值是仅仅给root用的,普通用户用不了,目的是防止文件系统的碎片。  
上一篇:Linux C语言木马程序盗取root用户密码
下一篇:Linux环境下C语言模拟内存负载测试