zoukankan      html  css  js  c++  java
  • Linux 使用statvfs读取文件系统信息

    本文转载自:https://blog.csdn.net/papiping/article/details/6980573

    在测试过程中,f_bfree的值比f_frsize的值大于10%的尺寸大小,意味着16G的磁盘

    f_bfree测试出来的结果是17G,f_frsize测试的结果是15G

    struct statvfs {

       unsigned long  f_bsize;    /* file system block size */文件系统块大小

       unsigned long  f_frsize;   /* fragment size */碎片大小

       fsblkcnt_t     f_blocks;   /* size of fs in f_frsize units */

       fsblkcnt_t     f_bfree;    /* # free blocks */空闲的块

       fsblkcnt_t     f_bavail;   /* # free blocks for unprivileged users */用户级的空闲节点

       fsfilcnt_t     f_files;    /* # inodes */节点

       fsfilcnt_t     f_ffree;    /* # free inodes */空闲的节点

       fsfilcnt_t     f_favail;   /* # free inodes for unprivileged users */用户级的空闲节点

       unsigned long  f_fsid;     /* file system ID */

       unsigned long  f_flag;     /* mount flags */

       unsigned long  f_namemax;  /* maximum filename length */

    };

    测试环境:ext4

    1)分析f_bsize和f_frsize的异同

    表示每块包含字节的多少

    打印结果都是4096字节,显然都是一样的

    2)分析f_bfree和f_bavail

    表示磁盘剩余空闲的容量

    打印结果是

    磁盘为16G的情况下,f_bfree比f_bavail大809MB

    磁盘为1.5T的情况下,f_bfree比f_bavail大2585MB

    并没有我们想象的成线性增长

    实际情况下采用df -Th

    f_bavail更加接近显示的值

    3)f_blocks表示磁盘的整体空间

    4)计算磁盘各个方面的数值如下:

    磁盘的整体空间:(buf.f_blocks *  buf.f_frsize)/1024/1024)MB

    磁盘的空闲空间:(buf.f_bfree *  buf.f_frsize)/1024/1024)MB

    磁盘的用户级空闲空间:(buf.f_bavail *  buf.f_frsize)/1024/1024)MB

    #include<stdio.h>

    #include<stdlib.h>

    #include<string.h>

    #include<mntent.h>

    #include<sys/statvfs.h>

    //pszDevicePath是设备的文件描述符,pszDeviceMountPoint是设备的挂载点

    intGetDeviceMountPoint(constchar*pszDevicePath,char*pszDeviceMountPoint)

    {

    structmntent*pent;

    FILE*pFile;

    pFile=setmntent("/proc/mounts","r");

    if(pFile==NULL){

    perror("setmntent");

    return-1;

    }

    while(NULL!=(pent=getmntent(pFile)))

    {

    if(strcmp(pent->mnt_fsname,pszDevicePath)==0)

    {

    sprintf(pszDeviceMountPoint,"%s",pent->mnt_dir);

    printf("Device:%smountpointis:%s ",pszDevicePath,pszDeviceMountPoint);

    }

    }

    endmntent(pFile);

    return0;

    }

    //通过statvfs查询挂载点上文件系统的空间大小

    intGetDeviceRoomSize(constchar*pszDeviceMountPoint)

    {

    structstatvfsbuf;

    intrv=statvfs(pszDeviceMountPoint,&buf);

    if(!rv)

    {

    printf("wholespace:%uMbytes ",(buf.f_blocks*buf.f_frsize)/1024/1024);

    printf("availspace:%uMbytes ",(buf.f_bavail*buf.f_frsize)/1024/1024);

    }

    else

    {

    perror("statvfs");

    return-1;

    }

    return(buf.f_blocks*buf.f_frsize)/1024/1024;

    }

    intmain(intargc,char*argv[])

    {

    intnDiskRoomSize=0;

    charszMountPoint[32]={0};

    GetDeviceMountPoint(argv[1],szMountPoint);

    nDiskRoomSize=GetDeviceRoomSize(szMountPoint);

    printf("DiskRoomSize=%dMB ",nDiskRoomSize);

    return 0;

    }

  • 相关阅读:
    SQLServer学习笔记系列1
    结束回忆的2014,带着精彩奔向2015!
    sql重置自增长
    回首一年的骚动岁月
    IIS7.5上的REST服务的Put,Delete操作发生HTTP Error 405.0
    Javascript学习笔记1
    C#基础回顾(三)—索引器、委托、反射
    C#基础回顾(二)—页面值传递、重载与重写、类与结构体、装箱与拆箱
    C#基础回顾(一)—C#访问修饰符
    SQL连接
  • 原文地址:https://www.cnblogs.com/zzb-Dream-90Time/p/10114615.html
Copyright © 2011-2022 走看看