C/C++知识点之Linux创造固定的文件大小-预分配磁盘空间
小标 2019-03-01 来源 : 阅读 2173 评论 0

摘要:本文主要向大家介绍了 C/C++知识点之Linux创造固定的文件大小-预分配磁盘空间,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

本文主要向大家介绍了 C/C++知识点之Linux创造固定的文件大小-预分配磁盘空间,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

C/C++知识点之Linux创造固定的文件大小-预分配磁盘空间

一、课前预习


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


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


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


#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdint.h>

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 failed\n");
      return -1;
    }

    if(ftruncate(fd, file_size)){
        printf("ftruncate error\n");
        return -1;
    }

    lseek(fd, file_size - 1,SEEK_CUR);
    write(fd, "1", 1);

    close(fd);
    return 0;
}


测试结果

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


#include <stdio.h>
#include <unistd.h>

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


测试结果

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             /* See feature_test_macros(7) */
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>

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,  %s\n", ret, errno, strerror(errno));
      return -1;
   }

   printf("fallocate create %.2fG file\n", 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


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


2. 额外补充



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

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

  2. 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

   

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程