Click here to Skip to main content
16,017,502 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I am trying to implement a program to share information stored in a map between process using shared memory .

When I am attaching the shared memory to the map , it is dumping core but I am able to attach the structure to shared memory successfully .

Could you please tell what could be the reason for this ?


Regards
Centinnel
Posted
Comments
Philip Stuyck 3-Aug-12 2:26am    
You should give us a piece of the code, especially where it is crashing.
Sebastian T Xavier 3-Aug-12 2:40am    
Please show us your code
Malli_S 3-Aug-12 4:38am    
Post the code snip where it crashes.

If you put the map in the shared memory, it does not mean that map will use the shared memory for allocations. So, between processes you will have shared only value of a pointer which points to some memory inside the nonshared memory. Map uses new/delete functions to build itself.
 
Share this answer
 
Comments
enhzflep 3-Aug-12 7:03am    
Bingo!
Furthermore, one must assume that STL containers are NOT thread-safe. Even if copying the thing would work, There are bound to be tears(crying) later on.
My +5
It is completely possible to do it but the solution is messy. Here is how to do it: First you have to allocate a piece of shared memory. Then you should create an std::map lets say the the beginning of this shared memory with "placement new" and reserve the other (bigger) part of the shared memory to be used as a pool for other allocations. This std::map should havea custom allocator (the 3rd template parameter of std::map is the allocator class). This custom allocator should be one that uses the pool part of the shared memory to allocate (I have such an allocator somewhere...). If your map contains non-POD types (either as key or value) that do some kind of allocations, then you should write new operators for those classes too to allocate from the shared memory pool but its much less bothersome to use PODs in this shared map.

EDIT: it can be harder to implement this if you want to modify the map in both processes (but its possible). Its easier if only one of them writes the map, the other just reads it. Either way, you will need some kind of inter-process synchronization too.

EDIT2: forgot to mention that I would write and use a custom template for this purpose, one that can store a fix number of key value pairs and has a fixed size. Inside you could use a hash+item table or a shorted array to speed up indexing but forgot about this if you havent worked much with templates...
 
Share this answer
 
v3
Comments
nv3 3-Aug-12 9:19am    
You should have mentioned that this scenario requires the shared memory section to reside on the same virtual address in both processes, otherwise all pointers will be doomed.

A generally better way to use shared memory is to only store offsets, indices and other relative references, but never raw pointers. In this way the memory sections remains independent of the load address in virtual address space.

That however makes it difficult to work with data types like std::map, although it's possible to use relative references in iterators and allocators; just a lot of hassle.
pasztorpisti 3-Aug-12 12:54pm    
Good point! :-)
This answer is just a big masturbation over std::map, generally I don't like the C++ std. However a good self contained, fixed-capacity map template can be put together in 2-3 hundred lines of code. It would work offset independently with PODs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900