C df和statfs的输出不同

C df和statfs的输出不同,c,system-calls,C,System Calls,为什么df命令和statfs()系统调用值的输出不同: 调用statfs的程序: #include <stdio.h> #include <errno.h> #include <sys/types.h> #include <sys/vfs.h> #include <string.h> #include <stdlib.h> int main() { struct statfs vfs; if (stat

为什么df命令和statfs()系统调用值的输出不同:

调用statfs的程序:

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/vfs.h>
#include <string.h>
#include <stdlib.h>


int main()
{
    struct statfs vfs;

    if (statfs("/", & vfs) != 0) {
        fprintf(stderr, "%s: statfs failed: %s\n",
            "/", strerror(errno));
        exit(0);
    }

    printf("mounted on %s:\n","/");

    printf("\tf_bsize: %ld\n", vfs.f_bsize);
    printf("\tf_blocks: %ld\n", vfs.f_blocks);
    printf("\tf_bfree: %ld\n", vfs.f_bfree);
    printf("\tf_bavail: %ld\n", vfs.f_bavail);
    printf("\tf_files: %ld\n", vfs.f_files);
    printf("\tf_ffree: %ld\n", vfs.f_ffree);

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
结构statfs vfs;
如果(statfs(“/”,&vfs)!=0){
fprintf(stderr,“%s:statfs失败:%s\n”,
“/”,strerror(errno));
出口(0);
}
printf(“安装在%s:\n”和“/”)上;
printf(“\tf\u bsize:%ld\n”,vfs.f\u bsize);
printf(“\tf\u块:%ld\n”,vfs.f\u块);
printf(“\tf\u bfree:%ld\n”,vfs.f\u bfree);
printf(“\tf\u bavail:%ld\n”,vfs.f\u bavail);
printf(“\tf\u文件:%ld\n”,vfs.f\u文件);
printf(“\tf\u ffree:%ld\n”,vfs.f\u ffree);
返回0;
}
输出:

mounted on /: f_bsize: 4096 f_blocks: 119189762 f_bfree: 112718672 f_bavail: 106662506 f_files: 30285824 f_ffree: 29990111 安装在/: f_b尺寸:4096 f_区块:119189762 自由:112718672 法乌·巴维尔:106662506 f_档案:30285824 福弗里:29990111 df命令的输出:

~$ df / Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda1 476759048 25882620 426651764 6% / ~$df/ 已使用的文件系统1K块可用使用%已安装在 /dev/sda1 476759048 25882620 426651764 6%/
df命令在内部调用statfs systemcall本身,但是为什么输出在结构值和df命令的输出上不同,有人能解释清楚吗。

df的数据可能基于
f_bavail
,而不是
f_bfree
,因此总空间和可用空间的计算如下所示

long long Total_Space = vfs.f_blocks;
Total_Space *= vfs.f_frsize;
Total_Space /= 1024;
long long Avail_Space = vfs.f_bfree;
Avail_Space *= vfs.f_frsize;
Avail_Space /= 1024;

printf("Total Space=%lldKb Available Space=%lldKB\n",Total_Space,Avail_Space);
请看