简体   繁体   English

mmap 打开并从文件中读取

[英]mmap open and read from file

I am mapping a huge file to avoid my app thrashing to main virtual memory, and to be able to run the app with more than the RAM I have.我正在映射一个巨大的文件,以避免我的应用程序抖动到主虚拟 memory,并且能够使用超过我拥有的 RAM 运行该应用程序。 The code is c++ but partly follows old c APIs.代码为 c++,但部分遵循旧的 c API。 When I work with the allocated pointer, the memory does get backed to the file as desired.当我使用分配的指针时,memory 会根据需要返回到文件。 However, when I run the app next time, I want the memory to be read from this same file which already has the prepared data.但是,当我下次运行该应用程序时,我希望从已经具有准备好的数据的同一个文件中读取 memory。 For some reason, on the next run, I read back all zeros.出于某种原因,在下一次运行中,我读回了所有零。 What am I doing wrong?我究竟做错了什么? Is it the ftruncate call?是 ftruncate 调用吗? Is it the fopen call with wrong flag?是带有错误标志的 fopen 调用吗? Is it the mmap flags?是 mmap 标志吗?

int64_t mmbytes=1<<36;
FILE *file = fopen(filename, "w+");
int fd = fileno(file);
int r = ftruncate(fd, mmbytes );
if (file == NULL || r){
    perror("Failed: ");
    throw std::runtime_error(std::strerror(errno));
} // 

if ((mm = mmap(0, mmbytes,  
        PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0)) == MAP_FAILED)
  {
        fprintf(stderr,"mmap error for output, errno %d\n", errno);
        exit(-1);
  } 
 }
FILE *file = fopen(filename, "w+");

I refer you to fopen 's manual page, which describes "w+" as follows:我建议您参考fopen的手册页,其中对“w+”的描述如下:

       w+     Open  for  reading  and writing.  The file is created if it does
              not exist, otherwise it is truncated.  The stream is  positioned
              at the beginning of the file.

I specifically draw your attention to the "it is truncated" part.我特别提请您注意“它被截断”部分。 In other words, if there's anything in an existing file this ends up nuking it from high orbit.换句话说,如果现有文件中有任何内容,这最终会将其从高轨道上摧毁。

Depending on what else you're doing "a" will work better.根据您正在做的其他事情,“a”会更好。

Even better would be to forget fopen entirely, and simply use open :更好的办法是完全忘记fopen ,而只使用open

int fd=open(filename, O_RDWR|O_CREAT, 0666);

There's your file descriptor, without jumping through any hoops.这是您的文件描述符,无需跳过任何环节。 The file gets created, and left untouched if it already exists.文件被创建,如果它已经存在则保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM