df 和 statfs 的输出不同

编程入门 行业动态 更新时间:2024-10-22 13:35:52
本文介绍了df 和 statfs 的输出不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

调用statfs的程序:

#include #include #include #include #include #include int main(){结构 statfs vfs;如果(statfs(/",& vfs)!= 0){fprintf(stderr, "%s: statfs 失败: %s\n","/", strerror(errno));退出(0);}printf("挂载在 %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);返回0;}

输出:

<前>镶嵌在/:f_bsize: 4096f_blocks:119189762f_bfree:112718672f_bavail:106662506f_files:30285824f_ffree:29990111

df 命令的输出:

<前>~$ df/文件系统 1K-blocks Used 可用使用% Mounted on/dev/sda1 476759048 25882620 426651764 6%/

df 命令在内部调用 statfs 系统调用本身,但是为什么结构值和 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("总空间=%lldKb 可用空间=%lldKB\n",Total_Space,Avail_Space);

why is the output of df command and statfs() system call values are different:

program to call 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;
}

output:

    mounted on /:
        f_bsize: 4096
        f_blocks: 119189762
        f_bfree: 112718672
        f_bavail: 106662506
        f_files: 30285824
        f_ffree: 29990111

output of df command:

    ~$ df /
    Filesystem     1K-blocks     Used Available Use% Mounted on
    /dev/sda1      476759048 25882620 426651764   6% /

df command internally calls the statfs systemcall itself, but why is the output is different on structure values and the output of df command, can anyone explain me clearly.

解决方案

df's data may be based on f_bavail, not f_bfree So for calculation of total space and available space may be as follows

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);

这篇关于df 和 statfs 的输出不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 12:13:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409381.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:df   statfs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!