Using the Boost IPC library, it is possible to associate a file content with shared memory, that gives a number of advantages, like delegating to the OS all the trouble connected with data sync and caching.

By the way, sometimes could be useful to use file_mapping even if we are not interested in sharing memory, but just to simplify the data management on a file.

Here is an example:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstring>
#include <cstddef>
#include <cstdlib>
#include "boost/interprocess/file_mapping.hpp"
#include "boost/interprocess/mapped_region.hpp"

using namespace boost::interprocess;

const std::size_t FILE_SIZE = 10000;
const char* FILE_NAME = "file.bin";

class FileManager // 1.
enum Mode { create_remove, create, remove, access };
FileManager(const char* fname, Mode mode) : fname_(fname), mode_(mode)
if(mode == create_remove || mode == create)
std::filebuf fbuf;, std::ios_base::in | std::ios_base::out
| std::ios_base::trunc | std::ios_base::binary);
//Set the size
fbuf.pubseekoff(FILE_SIZE-1, std::ios_base::beg);

mapped_region getMappedRegion(mode_t mode)
//Create a file mapping
file_mapping m_file(fname_.c_str(), mode);

//Map the whole file with read-write permissions in this process
return mapped_region(m_file, mode);

if(mode_ == remove || mode_ == create_remove)
std::cout << "File " << fname_ << " removed" << std::endl;
std::string fname_;
Mode mode_;

// ensure the executable file name is quoted in case it has internal blanks
bool launchChildProcess(const char* progName)
bool quote = strchr(progName, ' ') == 0 ? false : true;
std::string s((quote ? "\"" : ""));
s += progName;
s += "\"";
s += " child";

if(std::system(s.c_str()) != 0)
std::cout << "error in the child process" << std::endl;
return false;
return true;

void checkMemoryOne(void* address, std::size_t size)
const char* mem = static_cast<const char*>(address);
for(std::size_t i = 0; i < size; ++i)
if(*mem++ != 1)
std::cout << "Memory check failed" << std::endl;
std::cout << "Memory check succeeded" << std::endl;

void sharedMemoryAccess() // 2.
FileManager fm(FILE_NAME, FileManager::access);
mapped_region region = fm.getMappedRegion(read_only);

//Get the address of the mapped region
void* addr = region.get_address();
std::size_t size = region.get_size();

checkMemoryOne(addr, size);

void fileAccess() // 3.
std::filebuf fbuf;, std::ios_base::in | std::ios_base::binary);

//Read it to memory
std::vector<char> vect(FILE_SIZE, 0);
fbuf.sgetn(&vect[0], std::streamsize(vect.size()));

checkMemoryOne(&vect[0], FILE_SIZE);

// parent process
void ip06a(const char* progName)
FileManager fm(FILE_NAME, FileManager::create_remove);
mapped_region region = fm.getMappedRegion(read_write);

//Get the address of the mapped region
void* addr = region.get_address();
std::size_t size = region.get_size();

//Write all the memory to 1
std::memset(addr, 1, size);


// child process
void ip06b()
std::cout << "Accessing the shared memory" << std::endl;

std::cout << "Accessing the file on disk" << std::endl;

1. the class FileManager is just a little wrapper to the basic functionality for the file access. Its constructor keeps track of the associate file name and the way we want to access it. Only if we want to actually create it, the filebuf functionality are called to access the file, otherwise, if we just want to access the data (or remove the file) we rely on the fact that someone else should have already crete the file. The getMappedRegion() method performs the mapping between shared memory and file, and return the mapped region, in the mode we require. The destructor remove the file, only when required.
2. the function sharedMemoryAccess() shows how to work with the mapped_region.
3. as comparison, the function fileAccess() performs the same action of sharedMemoryAccess(), but accessing directly the file.

The main for this application, basically just calls the parent or the child function accordingly to the passed parameter:
if(argc == 1)

The code is based on an example provided by the Boost Library Documentation.

No comments:

Post a Comment