Using boost::iostreams::mapped_file

这一生的挚爱 提交于 2019-12-14 03:56:50

问题


I am very new to the memory mapping and trying to understand memory mapped files to use them in my project(linux based). My requirement is to write & then read from memory mapped files. I wrote a sample program which only writes and it works fine but i have a few very basic doubts as i do not understand this funda of memory mapping properly.

#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:

  1. When i do this : mf.open(params) , i see that a file is created on disk with size 100. Now when i write to it i.e *w = w1, the contents of the file on disk changes. Does this mean that i am not using the RAM at all and i am writing straight into the
    disk?

  2. When i do mf.size(), it always give me the size which i gave as the input for creating the actual file. How can i find out the size of the data that i actually wrote into the
    memory mapped file?

  3. Also if i give params.new_file_size = 10GB, the file of that size gets created on the
    disk but it does not take up any disk space.Confirmed by using df cmd. Why so? -rwx------. 1 root root 10000000000 Apr 29 14:26 map.dat

  4. I read that close file frees the mapping. Does this mean that after close i lose all the data that i wrote? But this is not true as i have the working code where i close and then open the file again and read it correctly.

  5. How to delete the memory mapped files created after use? By using rm -rf cmd/linux apis?


回答1:


  • When i do this : mf.open(params) , i see that a file is created on disk with size 100. Now when i write to it i.e *w = w1, the contents of the file on disk changes. Does this mean that i am not using the RAM at all and i am writing straight into the disk?

You're using memory mapped files. This means both: you are writing to 'virtual memory pages' that have been mapped into your process space, but actually refer to disk blocks. The growth indicates that the pages get committed on write.

  • When i do mf.size(), it always give me the size which i gave as the input for creating the actual file. How can i find out the size of the data that i actually wrote into the memory mapped file?

You can't. You can only find the number of blocks committed with a tool like stat

  • Also if i give params.new_file_size = 10GB, the file of that size gets created on the disk but it does not take up any disk space.Confirmed by using df cmd. Why so? -rwx------. 1 root root 10000000000 Apr 29 14:26 map.dat

It's sparsely allocated. E.g. using fallocate or similar on other platforms.

  • I read that close file frees the mapping. Does this mean that after close i lose all the data that i wrote? But this is not true as i have the working code where i close and then open the file again and read it correctly.

No. It means that the mapping is freed. That is, the /virtual memory/ area in your process space is now 'free' to use for other things.

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

Yes.



来源:https://stackoverflow.com/questions/23383681/using-boostiostreamsmapped-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!