温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

linux中怎么获取flash分区大小

发布时间:2021-07-29 11:17:56 来源:亿速云 阅读:566 作者:Leah 栏目:互联网科技

linux中怎么获取flash分区大小,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

在嵌入式系统中,由于flash存储空间有限,或者是存储数据,实现数据的循环删除,需要获取到分区的使用情况,可以通过系统下的函数statfs来获取使用情况;实现代码如下:

flashInfo.cpp


#include <stdio.h>                                                                                                                  #include <stdlib.h>#include <unistd.h>#include <sys/vfs.h> typedef unsigned long long u64; //unit: 0-MBytes, 1-KBytes, default MBytesint getPartitionUse(const char *dir, size_t &totleSize, size_t &freeSize, int unit){    struct statfs diskInfo;      int ret = statfs(dir, &diskInfo);    if ( ret != 0 )  {    perror("getPartitionUse statfs error ");    return ret;  }      u64 allBlocks = diskInfo.f_bsize;      u64 tmpTotalSize = allBlocks * diskInfo.f_blocks;      u64 tmpFreeDisk = diskInfo.f_bfree*allBlocks;      totleSize = tmpTotalSize>>20;    freeSize  = tmpFreeDisk>>20;    if(unit == 0)  {      totleSize  = tmpTotalSize>>20;      freeSize  = tmpFreeDisk>>20;    }  else if( unit == 1 )  {    totleSize  = tmpTotalSize>>10;      freeSize  = tmpFreeDisk>>10;  }    return 0;} int main(){  size_t totleSize = 0;  size_t freeSize = 0;      getPartitionUse("/", totleSize,freeSize, 0);    printf ("system  total=%dMB, free=%dMB\n", totleSize, freeSize);      getPartitionUse("/work/data", totleSize,freeSize, 0);    printf ("data  total=%dMB, free=%dMB\n", totleSize, freeSize);        getPartitionUse("/work", totleSize,freeSize, 0);    printf ("config  total=%dMB, free=%dMB\n", totleSize, freeSize);  
}

编译:

mipsel-linux-g++ flashInfo.c -o flashInfo

运行结果如下:

linux中怎么获取flash分区大小

关于linux中怎么获取flash分区大小问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI