Chinaunix首页 | 论坛 | 博客
  • 博客访问: 146623
  • 博文数量: 13
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1192
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-11 15:41
个人简介

Cyber Security

文章分类

全部博文(13)

文章存档

2015年(1)

2014年(6)

2013年(6)

分类: C/C++

2013-12-25 17:33:45


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用的,普通用户用不了,目的是防止文件系统的碎片。  
阅读(7932) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~