How to Read the stored memory map in C++
I am loading the map into memory with one program and retrieving the map
values by another program.
Process1.
#include
//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;
You tell Process1 to clean it up on exit. Chris -- echo mailto: NOSPAM !#$.'<*>'|sed 's. ..'|tr "<*> !#:2" org@fr33z3
#include
//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;
You tell Process1 to clean it up on exit.
Chris -- echo mailto: NOSPAM !#$.'<*>'|sed 's. ..'|tr "<*> !#:2" org@fr33z3 _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- View this message in context: http://www.nabble.com/How-to-Read-the-stored-memory-map-in-C%2B%2B-tp2550632... Sent from the Boost - Users mailing list archive at Nabble.com.
I did the needful for construction and destruction but it does not seem to be working. May i know what is the pblm. Thanks in advance. manish4gupta wrote:
#include
#include #include #include #include #include <iostream> using namespace boost::interprocess;
//Typedefs of allocators and containers typedef managed_shared_memory::segment_manager segment_manager_t; typedef allocator
void_allocator; typedef allocator int_allocator; typedef vector int_vector; typedef allocator int_vector_allocator; typedef vector int_vector_vector; typedef allocator char_allocator; typedef basic_string char_string; class complex_data { int id_; char_string char_string_; int_vector_vector int_vector_vector_;
public: //Since void_allocator is convertible to any other allocator<T>, we can simplify //the initialization taking just one allocator for all inner containers. complex_data(int id, const char *name, const void_allocator &void_alloc) : id_(id), char_string_(name, void_alloc), int_vector_vector_(void_alloc) {} //Other members... };
//Definition of the map holding a string as key and complex_data as mapped type typedef std::pair
map_value_type; typedef std::pair movable_to_map_value_type; typedef allocator map_value_type_allocator; typedef map< char_string, int , std::less , map_value_type_allocator> complex_map_type; int main () { //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 shared memory managed_shared_memory segment(create_only,"MySharedMemory", 65536);
//An allocator convertible to any allocator
type void_allocator alloc_inst (segment.get_segment_manager()); //Construct the shared memory map and fill it complex_map_type *mymap = segment.construct
//(object name), (first ctor parameter, second ctor parameter) ("MyMap")(std::less (), alloc_inst); std::string s = "test"; char_string cs(s.c_str(), alloc_inst); //string y (cs.c_str()); //std::cout << y <<"\n"; // = (char_string)s;
for(int i = 0; i < 100; ++i) { mymap->insert(std::pair
(cs , i)); //char_string key_object(alloc_inst); //map_value_type value(key_object, i); // mymap->insert(value); } shared_memory_object::remove("MySharedMemory"); return 0; } Now i have added the line shared_memory_object::remove("MySharedMemory") in process1 to remove the shared memory but still error is same.
Christoph Gysin-3 wrote:
//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;
You tell Process1 to clean it up on exit.
Chris -- echo mailto: NOSPAM !#$.'<*>'|sed 's. ..'|tr "<*> !#:2" org@fr33z3 _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- View this message in context: http://www.nabble.com/How-to-Read-the-stored-memory-map-in-C%2B%2B-tp2550632... Sent from the Boost - Users mailing list archive at Nabble.com.
Hi Ion, How can i solve my pblm? I tried many things but nothing seems to be working. Regards Manish manish4gupta wrote:
I am loading the map into memory with one program and retrieving the map values by another program.
Process1. #include
#include #include #include #include #include <iostream> using namespace boost::interprocess;
//Typedefs of allocators and containers typedef managed_shared_memory::segment_manager segment_manager_t; typedef allocator
void_allocator; typedef allocator int_allocator; typedef vector int_vector; typedef allocator int_vector_allocator; typedef vector int_vector_vector; typedef allocator char_allocator; typedef basic_string char_string; class complex_data { int id_; char_string char_string_; int_vector_vector int_vector_vector_;
public: //Since void_allocator is convertible to any other allocator<T>, we can simplify //the initialization taking just one allocator for all inner containers. complex_data(int id, const char *name, const void_allocator &void_alloc) : id_(id), char_string_(name, void_alloc), int_vector_vector_(void_alloc) {} //Other members... };
//Definition of the map holding a string as key and complex_data as mapped type typedef std::pair
map_value_type; typedef std::pair movable_to_map_value_type; typedef allocator map_value_type_allocator; typedef map< char_string, int , std::less , map_value_type_allocator> complex_map_type; int main () { //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 shared memory managed_shared_memory segment(create_only,"MySharedMemory", 65536);
//An allocator convertible to any allocator
type void_allocator alloc_inst (segment.get_segment_manager()); //Construct the shared memory map and fill it complex_map_type *mymap = segment.construct
//(object name), (first ctor parameter, second ctor parameter) ("MyMap")(std::less (), alloc_inst); std::string s = "test"; char_string cs(s.c_str(), alloc_inst); //string y (cs.c_str()); //std::cout << y <<"\n"; // = (char_string)s;
for(int i = 0; i < 100; ++i) { mymap->insert(std::pair
(cs , i)); //char_string key_object(alloc_inst); //map_value_type value(key_object, i); // mymap->insert(value); } return 0; } Process2. #include
#include #include #include #include #include <iostream> #include #include <algorithm> using namespace boost::interprocess; typedef managed_shared_memory::segment_manager segment_manager_t; typedef allocator void_allocator; typedef allocator int_allocator; typedef vector int_vector; typedef allocator int_vector_allocator; typedef vector int_vector_vector; typedef allocator char_allocator; typedef basic_string char_string; class complex_data { int id_; char_string char_string_; int_vector_vector int_vector_vector_; public: complex_data(int id, const char *name, const void_allocator &void_alloc): id_(id), char_string_(name, void_alloc), int_vector_vector_(void_alloc) {} };
typedef std::pair
map_value_type; typedef std::pair movable_to_map_value_type; typedef allocator map_value_type_allocator; typedef map< char_string, int,std::less , map_value_type_allocator> complex_map_type; int main () { using namespace boost::interprocess; try { managed_shared_memory segment(open_only ,"MySharedMemory"); typedef map< char_string, int, std::less
, map_value_type_allocator> complex_map_type; complex_map_type *MyMap = segment.find ("MyMap").first; for (complex_map_type::iterator it = MyMap->begin(); it != MyMap->end(); it++) { std::cout << it->first << "\n"; } segment.destroy ("MyMap"); } catch(...) { shared_memory_object::remove("MySharedMemory"); throw; } shared_memory_object::remove("MySharedMemory"); return 0; } But i am getting the following error message
terminate called after throwing an instance of 'boost::interprocess::interprocess_exception' what(): No such file or directory Aborted
What is the reason for this? Thanks in advance.
-- View this message in context: http://www.nabble.com/How-to-Read-the-stored-memory-map-in-C%2B%2B-tp2550632... Sent from the Boost - Users mailing list archive at Nabble.com.
2009/9/25 manish4gupta
How can i solve my pblm? I tried many things but nothing seems to be working.
Calling shared_memory_object::remove("MySharedMemory") removes the whole shared memory where you saved your data. If it gets called before Process2 tries to read it, it will fail with mentioned interprocess_exception. Just don't remove it in Process1, remove it at the end of Process2. Chris -- echo mailto: NOSPAM !#$.'<*>'|sed 's. ..'|tr "<*> !#:2" org@fr33z3
Previously i was doing in the same way but still getting the same error. I
was removing he shared region in process2 only but pblm is still there.Pls
can i get any working example doing the same.
Process1. storing map
2009/9/25 manish4gupta
: How can i solve my pblm? I tried many things but nothing seems to be working.
Calling shared_memory_object::remove("MySharedMemory") removes the whole shared memory where you saved your data. If it gets called before Process2 tries to read it, it will fail with mentioned interprocess_exception.
Just don't remove it in Process1, remove it at the end of Process2.
Chris -- echo mailto: NOSPAM !#$.'<*>'|sed 's. ..'|tr "<*> !#:2" org@fr33z3 _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- View this message in context: http://www.nabble.com/How-to-Read-the-stored-memory-map-in-C%2B%2B-tp2550632... Sent from the Boost - Users mailing list archive at Nabble.com.
manish4gupta wrote:
Previously i was doing in the same way but still getting the same error. I was removing he shared region in process2 only but pblm is still there.Pls can i get any working example doing the same. Process1. storing map
nad process2 reading it.I will really be thankful to you. Christoph Gysin-3 wrote:
2009/9/25 manish4gupta
: How can i solve my pblm? I tried many things but nothing seems to be working. Calling shared_memory_object::remove("MySharedMemory") removes the whole shared memory where you saved your data. If it gets called before Process2 tries to read it, it will fail with mentioned interprocess_exception.
Just don't remove it in Process1, remove it at the end of Process2.
Chris --
What Christoph wrote was correct: In the code you posted on th 18th, process1 removes the shared memory on exit (when the object called "remover" goes out of scope). Thus, when you run process1 it brings the shared memory into being, writes the map into it and then removes the whole stuff again. Thus, when you start process2 afterwards, the shared memory is long gone. Hence the exception. So in the code posted on the 18th, you just have to replace the remover by the following line: managed_shared_memory segment(create_only,"MySharedMemory", 65536); That works. BTW: In a "real world" application the remover would be a very useful helper class. If you do not know why, take a good book on C++, for instance "Effective C++" by Scott Meyers, and read until you do know :-) Regards, Roland PS: And please(!) remove the unnecessary stuff, before posting. The complex_data class for instance is totally irrelevant. Same goes for most of the typedefs. All this nonsense just adds to the noise which makes it harder to see the actual problem. PPS: Attached, please find the reduced code of process1.
Thanks it works as suggested. I was in trouble from past 2 weeks but u helped me. Once again thank you very much. Roland Bock-2 wrote:
manish4gupta wrote:
Previously i was doing in the same way but still getting the same error. I was removing he shared region in process2 only but pblm is still there.Pls can i get any working example doing the same. Process1. storing map
nad process2 reading it.I will really be thankful to you. Christoph Gysin-3 wrote:
2009/9/25 manish4gupta
: How can i solve my pblm? I tried many things but nothing seems to be working. Calling shared_memory_object::remove("MySharedMemory") removes the whole shared memory where you saved your data. If it gets called before Process2 tries to read it, it will fail with mentioned interprocess_exception.
Just don't remove it in Process1, remove it at the end of Process2.
Chris --
What Christoph wrote was correct:
In the code you posted on th 18th, process1 removes the shared memory on exit (when the object called "remover" goes out of scope). Thus, when you run process1 it brings the shared memory into being, writes the map into it and then removes the whole stuff again.
Thus, when you start process2 afterwards, the shared memory is long gone. Hence the exception.
So in the code posted on the 18th, you just have to replace the remover by the following line:
managed_shared_memory segment(create_only,"MySharedMemory", 65536);
That works.
BTW: In a "real world" application the remover would be a very useful helper class. If you do not know why, take a good book on C++, for instance "Effective C++" by Scott Meyers, and read until you do know :-)
Regards,
Roland
PS: And please(!) remove the unnecessary stuff, before posting. The complex_data class for instance is totally irrelevant. Same goes for most of the typedefs. All this nonsense just adds to the noise which makes it harder to see the actual problem.
PPS: Attached, please find the reduced code of process1.
#include
#include #include #include #include <iostream> using namespace boost::interprocess;
//Typedefs of allocators and containers typedef managed_shared_memory::segment_manager segment_manager_t; typedef allocator
void_allocator; typedef allocator char_allocator; typedef basic_string char_string; //Definition of the map holding a string as key and complex_data as mapped type typedef std::pair
map_value_type; typedef allocator map_value_type_allocator; typedef map< char_string, int, std::less , map_value_type_allocator> complex_map_type; int main () { shared_memory_object::remove("MySharedMemory");
//Create shared memory managed_shared_memory segment(create_only,"MySharedMemory", 65536);
//An allocator convertible to any allocator
type void_allocator alloc_inst (segment.get_segment_manager()); //Construct the shared memory map and fill it complex_map_type *mymap = segment.construct
("MyMap")(std::less (), alloc_inst); char_string cs("test", alloc_inst); for(int i = 0; i < 100; ++i) { mymap->insert(std::pair
(cs , i)); } return 0; } _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- View this message in context: http://www.nabble.com/How-to-Read-the-stored-memory-map-in-C%2B%2B-tp2550632... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (3)
-
Christoph Gysin
-
manish4gupta
-
Roland Bock