关于C++ 使用boost库,创建进程间的共享内存,是一种常见的方式,需要注意的就是读写之间的的时间同步
1. boost::interprocess::shared_memory_object 类
// In header: <boost/interprocess/shared_memory_object.hpp>
class shared_memory_object {
public:
// construct/copy/destruct
shared_memory_object();
shared_memory_object(create_only_t, const char *, mode_t,
const permissions & = permissions());
shared_memory_object(open_or_create_t, const char *, mode_t,
const permissions & = permissions());
shared_memory_object(open_only_t, const char *, mode_t);
shared_memory_object(shared_memory_object &&);
shared_memory_object & operator=(shared_memory_object &&);
~shared_memory_object();
// public member functions
void swap(shared_memory_object &);
void truncate(offset_t);
const char * get_name() const;
bool get_size(offset_t &) const;
mode_t get_mode() const;
mapping_handle_t get_mapping_handle() const;
// public static functions
static bool remove(const char *);
};
https://www.boost.org/doc/libs/1_70_0/doc/html/boost/interprocess/shared_memory_object.html
2. share momory创建模板
https://www.boost.org/doc/libs/1_70_0/doc/html/interprocess/sharedmemorybetweenprocesses.html
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <cstring>
#include <cstdlib>
#include <string>
int main(int argc, char *argv[])
{
using namespace boost::interprocess;
if(argc == 1){ //Parent process
//Remove shared memory on construction and destruction
struct shm_remove
{
shm_remove() { shared_memory_object::remove("MySharedMemory"); }
~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
} remover;
//Create a shared memory object.
shared_memory_object shm (create_only, "MySharedMemory", read_write);
//Set size
shm.truncate(1000);
//Map the whole shared memory in this process
mapped_region region(shm, read_write);
//Write all the memory to 1
std::memset(region.get_address(), 1, region.get_size());
//Launch child process
std::string s(argv[0]); s += " child ";
if(0 != std::system(s.c_str()))
return 1;
}
else{
//Open already created shared memory object.
shared_memory_object shm (open_only, "MySharedMemory", read_only);
//Map the whole shared memory in this process
mapped_region region(shm, read_only);
//Check that memory was initialized to 1
char *mem = static_cast<char*>(region.get_address());
for(std::size_t i = 0; i < region.get_size(); ++i)
if(*mem++ != 1)
return 1; //Error checking memory
}
return 0;
}
3. Mapping File's Contents In Memory
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstring>
#include <cstddef>
#include <cstdlib>
int main(int argc, char *argv[])
{
using namespace boost::interprocess;
//Define file names
const char *FileName = "file.bin";
const std::size_t FileSize = 10000;
if(argc == 1){ //Parent process executes this
{ //Create a file
file_mapping::remove(FileName);
std::filebuf fbuf;
fbuf.open(FileName, std::ios_base::in | std::ios_base::out
| std::ios_base::trunc | std::ios_base::binary);
//Set the size
fbuf.pubseekoff(FileSize-1, std::ios_base::beg);
fbuf.sputc(0);
}
//Remove on exit
struct file_remove
{
file_remove(const char *FileName)
: FileName_(FileName) {}
~file_remove(){ file_mapping::remove(FileName_); }
const char *FileName_;
} remover(FileName);
//Create a file mapping
file_mapping m_file(FileName, read_write);
//Map the whole file with read-write permissions in this process
mapped_region region(m_file, 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);
//Launch child process
std::string s(argv[0]); s += " child ";
if(0 != std::system(s.c_str()))
return 1;
}
else{ //Child process executes this
{ //Open the file mapping and map it as read-only
file_mapping m_file(FileName, read_only);
mapped_region region(m_file, read_only);
//Get the address of the mapped region
void * addr = region.get_address();
std::size_t size = region.get_size();
//Check that memory was initialized to 1
const char *mem = static_cast<char*>(addr);
for(std::size_t i = 0; i < size; ++i)
if(*mem++ != 1)
return 1; //Error checking memory
}
{ //Now test it reading the file
std::filebuf fbuf;
fbuf.open(FileName, std::ios_base::in | std::ios_base::binary);
//Read it to memory
std::vector<char> vect(FileSize, 0);
fbuf.sgetn(&vect[0], std::streamsize(vect.size()));
//Check that memory was initialized to 1
const char *mem = static_cast<char*>(&vect[0]);
for(std::size_t i = 0; i < FileSize; ++i)
if(*mem++ != 1)
return 1; //Error checking memory
}
}
return 0;
}