栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Linux创造固定的文件大小-预分配磁盘空间

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux创造固定的文件大小-预分配磁盘空间


一、课前预习

写本篇文章的目的很简单,防止采坑、防止采坑、防止采坑

我们在开发程序的过程中,往往需要预分配磁盘空间,防止因磁盘空间不够而引发程序异常问题(已踩过坑), 现网查阅资料,有些预分配磁盘空间的方法不正确,在这里特别记录一下, 除此之外,把正确的预分配的方法和大家分享一下,如果其他人有建议,欢迎拍砖狠砸

二、假:磁盘预分配---ftruncate

1. 使用lseek、ftruncate到一个固定位置生成的“空洞文件”是不会占据真正的磁盘空间的。

#include

#include

#include

#include

#include

#include

#include

uint64_t file_size = 10*1024*1024*1024ULL;

int main()

{

    int fd = -1;

    fd = open("test.data.fruncate.txt", O_RDWR | O_CREAT, 0666);

    if(fd < 0){

      printf("open failedn");

      return -1;

    }

    if(ftruncate(fd, file_size)){

        printf("ftruncate errorn");

        return -1;

    }

    lseek(fd, file_size - 1,SEEK_CUR);

    write(fd, "1", 1);

    close(fd);

    return 0;

}

测试结果

Linux创造固定的文件大小-预分配磁盘空间

2. fseek fputs填充文件的最后一个字节

#include

#include

int main()

{

    FILE *f = fopen("test.data", "w");

    fseek(f, 256*1024*1024 - 1, SEEK_CUR);

    fputc(0,f);

    return 0;

}

3. dd命令

         dd if=/dev/zero of=test bs=1M count=0 seek=1024

测试结果

Linux创造固定的文件大小-预分配磁盘空间

4. 额外补充:

1 .ls显示文件的“逻辑上”的size, 这不是文件真正占用磁盘空间大小,这部分空间也会被其他进程使用。

2 .du显示文件“物理上”的size,即du显示的size是文件在硬盘上占据了多少个block计算出来的

三、真:磁盘预分配---fallocate

1. 使用fallocate 或者posix_fallocate函数

请求tlinux内核大神后,原来还存在fallocate 和 posix_fallocate这样的函数, 还是自己太low, 其实fallocate 和 posix_fallocate两个最大的区别:

This is a nonportable, Linux-specific system call

废话也不多说, 还是直接上代码比较好一点, 一目了然

#define _GNU_SOURCE            

#include

#include

#include

#include

#include

#include

#include

uint64_t file_size = 10*1024*1024*1024ULL;

//int fallocate(int fd, int mode, off_t offset, off_t len);

//int posix_fallocate(int fd, off_t offset, off_t len);

int main()

{

   int fd = -1;

   int ret = -1;   

   fd = open("tmp.txt", O_CREAT|O_RDWR, 0666);

   if(fd < 0){

     printf("fd < 0");

     return -1;

   } 

   //ret = fallocate(fd, 0, 0, file_size);

   ret = posix_fallocate(fd, 0, file_size);

   if(ret < 0 ){

      printf("ret = %d, errno = %d,  %sn", ret, errno, strerror(errno));

      return -1;

   }

   printf("fallocate create %.2fG filen", file_size/1024/1024/1024.0);

   close(fd);

   return 0;

}

测试结果:

root@xxx.xxx.xxx.xxx:/data6#time ./posix_fallocate 

fallocate create 10.00G file

real    0m0.014s

user    0m0.000s

sys 0m0.014s

root@xxx.xxx.xxx.xxx:/data6#ll -h posix_fallocate.file.txt 

-rw-r--r-- 1 root root 10G Oct 25 15:45 posix_fallocate.file.txt

root@xxx.xxx.xxx.xxx:/data6#du -sh posix_fallocate.file.txt 

11G posix_fallocate.file.txt

Linux创造固定的文件大小-预分配磁盘空间

不仅可以占住空间,而且速度非常快, 之前有过一个愚蠢的想法, 用dd命令直接创建10G文件,但是会造成IO毛刺,肯定不能这么玩,要是创造100G文件岂不是凉凉了。dd命令还是安安静静压测下磁盘性能或者创造一些小文件比较好。

2. 额外补充

磁盘空间预分配,不是所有linux系统都支持,有时候就分配失败,这就没有办法了。如下

ret = -1, errno = 95,  Operation not supported

linux命令磁盘预分配---fallocate

root@xxx.xxx.xxx.xxx:/data6#fallocate -l 10G  2.txt

root@xxx.xxx.xxx.xxx:/data6#ll -h 2.txt   

-rw-r--r-- 1 root root 10G Oct 25 15:33 2.txt

root@xxx.xxx.xxx.xxx:/data6#du -sh 2.txt 

11G 2.txt

©著作权归作者所有:来自51CTO博客作者vaynedu的原创作品,如需转载,请注明出处,否则将追究法律责任


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/232402.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号