Reputation: 1239
I'm having using using Boost's interprocess for shared memory access.
What Im trying to do is store a map in shared memory and access if from across processes. Below is the part of the code where I try to do the same --
SharedMemoryManager.h
#ifndef SHARED_MEMORY_MANAGER_H
#define SHARED_MEMORY_MANAGER_H
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/sync/named_semaphore.hpp>
#include <boost/interprocess/containers/map.hpp>
enum RangeBound { START, END, SINGLE };
typedef struct {
std::string code;
RangeBound flag;
} ZipRangeInfo;
typedef std::string RangeKeyType;
typedef std::pair<const RangeKeyType, ZipRangeInfo> RangeValueType;
typedef boost::interprocess::allocator<RangeValueType, boost::interprocess::managed_shared_memory::segment_manager> RangeBasedShmemAllocator;
typedef boost::interprocess::map<RangeKeyType, ZipRangeInfo, std::less<RangeKeyType>, RangeBasedShmemAllocator> SharedRangeBasedMap;
class SharedMemoryManager {
private:
static boost::interprocess::managed_shared_memory *segment;
static RangeBasedShmemAllocator *alloc_range_map;
public:
static char* get_range_based_routing_code(char *dataItem, char *fileName);
static SharedRangeBasedMap* get_range_based_routing_table(char *fileName );
static void load_range_based_routing_table( const char *fileName );
};
#endif //SHARED_MEMORY_MANAGER_H
and SharedmemoryManager.cpp
#include "SharedMemoryManager.h"
const std::string shm_code_util("SharedMemoryUtil");
//Initializing shared memory of size 1 GB.
boost::interprocess::managed_shared_memory *SharedMemoryManager::segment =
new boost::interprocess::managed_shared_memory(
boost::interprocess::open_or_create, "CRS", 1024 * 1024 * 1024);
RangeBasedShmemAllocator *SharedMemoryManager::alloc_range_map =
new RangeBasedShmemAllocator(segment->get_segment_manager());
// Method definitions
char* SharedMemoryManager::get_range_based_routing_code(char *dataItem,
char *fileName) {
char* result = NULL;
// Postal Code Scrubbing Logic
if (dataItem == NULL)
return NULL;
try {
char *dataIt = (char *) calloc(strlen(dataItem) + 1, sizeof(char));
strcpy(dataIt, dataItem);
SharedRangeBasedMap *routing_table = get_range_based_routing_table(
fileName);
std::cout << "Hash Table Size :" << routing_table->size();
if (routing_table != NULL && routing_table->size() > 0) {
RangeKeyType key(dataItem);
SharedRangeBasedMap::const_iterator routing_entry =
routing_table->lower_bound(key);
std::cout << "Got iterator";
if (routing_entry == routing_table->end()) {
return NULL;
}
if (routing_entry->first == key
|| routing_entry->second.flag == END) {
result = (char *) routing_entry->second.code.c_str();
}
}
free(dataIt);
return result;
} catch (std::exception &e) {
throw;
}
}
SharedRangeBasedMap* SharedMemoryManager::get_range_based_routing_table(
char *fileName) {
boost::interprocess::named_semaphore sync_semaphore(
boost::interprocess::open_or_create, "LoadFilesSemaphore", 1);
sync_semaphore.wait();
try {
SharedRangeBasedMap *routing_table = segment->find_or_construct<
SharedRangeBasedMap>(fileName)(std::less<RangeKeyType>(),
*alloc_range_map);
if (routing_table->size() == 0) {
load_range_based_routing_table(fileName);
}
sync_semaphore.post();
return routing_table;
} catch (...) {
std::cout << "An exception was thrown; couldn't load post codes.";
sync_semaphore.post();
return NULL;
}
return NULL;
}
void SharedMemoryManager::load_range_based_routing_table(const char *fileName) {
SharedRangeBasedMap *range_based_map = NULL;
range_based_map = segment->find_or_construct<SharedRangeBasedMap>(
fileName)(std::less<RangeKeyType>(), *alloc_range_map);
// Build the hash if empty
if (range_based_map != NULL && range_based_map->size() == 0) {
//logic to populate the map - tested to work correctly
}
}
The problem that I'm facing is that the first process that calls get_range_based_routing_code
, thereby initializing the shared memory and populating the map, is able to retrieve the value from the map whereas any subsequent call to get_range_based_routing_code
encounters a segmentation fault.
By adding cout
statements, I have been able to infer that the below line is the one that causes the error(or is the lsat line of execution) --
SharedRangeBasedMap::const_iterator routing_entry =
routing_table->lower_bound(key);
Im having trouble figuring this out and my inexperience in C++ is not helping. Any pointers ?
Upvotes: 0
Views: 1406
Reputation: 393049
You have to make the keytype allocate from the shared memory area too.
There are a number of Boost IPC samples that define a shared_string
for this purpose. You can re-use this.
The same goes for the std::string
inside the ZipRangeInfo
type. Yes, this is a lot of work and troublesome. In practice, you might want to replace ZipRangeInfo::code
by some statically sized boost::array<char, ...>
or char[N]
Upvotes: 2