Using boost :: iostreams :: mapped_file

I am very new to memory mapping and trying to figure out memory mapping files to use in my project (based on Linux). My requirement is to write and then read from memory mapped files. I wrote an example program that only writes, and it works great, but I have a few very basic doubts, since I do not understand this memory card correctly.

#include <unordered_map> #include <boost/iostreams/device/mapped_file.hpp> using namespace boost::interprocess; using namespace std; typedef unordered_map<int, string> work; int main() { boost::iostreams::mapped_file_params params; params.path = "map.dat"; params.new_file_size = 100; params.mode = (std::ios_base::out | std::ios_base::in); boost::iostreams::mapped_file mf; mf.open(params); work w1; w1[0] = "abcd"; w1[1] = "bcde"; w1[2] = "cdef"; work* w = static_cast<work*>((void*)mf.data()); *w = w1; mf.close(); return 0; } 

I have a few questions here:

  • When I do this: mf.open (params), I see that the file is created on a disk of size 100. Now, when I write to it ie * w = w1, the contents of the file on the disk change. Does this mean that I do not use RAM at all, and I write directly to disk?

  • When I do mf.size (), it always gives me the size that I gave as input to create the actual file. How can I find out the size of the data that I actually wrote to the memory mapped file?

  • Also, if I give params.new_file_size = 10GB, a file of this size is created on the disk site, but it does not take up disk space. Confirmed with df cmd. Why is that? ------ using rwx. 1 root root 1,000,000,000 Apr 29 14:26 map.dat

  • I read that a closed file frees the display. Does this mean that after closing I will lose all the data that I wrote? But this is not so, because I have a working code where I close and then open the file again and read it correctly.

  • How to delete memory mapped files created after use? Using rm -rf cmd / linux apis?

+2
boost linux memory-mapped-files boost-iostreams
source share
1 answer
  • When I do this: mf.open (params), I see that the file is being created on a disk with a size of 100. Now, when I write to it ie * w = w1, the contents of the file on the disk change. Does this mean that I do not use RAM at all, and I write directly to disk?

You are using memory mapped files. This means: you write to “virtual memory pages” that have been mapped to process space but actually refer to disk blocks. Growth indicates that pages are locked during recording.

  • When I do mf.size (), it always gives me the size that I gave as input to create the actual file. How can I find out the size of the data that I actually wrote to the memory mapped file?

You can not. You can only find the number of blocks executed with a tool like stat

  • Also, if I give params.new_file_size = 10GB, a file of this size is created on disk, but it does not take up disk space. Confirmed with df cmd. Why is that? ------ using rwx. 1 root root 1,000,000,000 Apr 29 14:26 map.dat

It is rarely distributed. For example. using fallocate or similar on other platforms.

  • I read that a closed file frees the display. Does this mean that after closing I will lose all the data that I wrote? But this is not so, because I have working code where I close and then open the file again and read it correctly.

Not. This means that the display is freed. That is, / virtual memory / area in your process space is now "free" to use for other things.

  • How to delete memory mapped files created after use? Using rm -rf cmd / linux apis?

Yes.

+3
source share

All Articles