简体   繁体   中英

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. The code is c++ but partly follows old c APIs. When I work with the allocated pointer, the memory does get backed to the file as desired. 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. For some reason, on the next run, I read back all zeros. What am I doing wrong? Is it the ftruncate call? Is it the fopen call with wrong flag? Is it the mmap flags?

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:

       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.

Even better would be to forget fopen entirely, and simply use 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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