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:
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?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?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.datI 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.
How to delete the memory mapped files created after use? By using rm -rf cmd/linux apis?