1 ////////////////////////////////////////////////////////////////////////////// 2 // 3 // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost 4 // Software License, Version 1.0. (See accompanying file 5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 // 7 // See http://www.boost.org/libs/interprocess for documentation. 8 // 9 ////////////////////////////////////////////////////////////////////////////// 10 #include <boost/interprocess/detail/config_begin.hpp> 11 #include <boost/interprocess/detail/workaround.hpp> 12 //[doc_ipc_message 13 #include <boost/interprocess/managed_shared_memory.hpp> 14 #include <cstdlib> //std::system 15 #include <sstream> 16 //<- 17 #include "../test/get_process_id_name.hpp" 18 //-> 19 main(int argc,char * argv[])20int main (int argc, char *argv[]) 21 { 22 using namespace boost::interprocess; 23 if(argc == 1){ //Parent process 24 //Remove shared memory on construction and destruction 25 struct shm_remove 26 { 27 //<- 28 #if 1 29 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); } 30 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); } 31 #else 32 //-> 33 shm_remove() { shared_memory_object::remove("MySharedMemory"); } 34 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); } 35 //<- 36 #endif 37 //-> 38 } remover; 39 //<- 40 (void)remover; 41 //-> 42 43 //Create a managed shared memory segment 44 //<- 45 #if 1 46 managed_shared_memory segment(create_only, test::get_process_id_name(), 65536); 47 #else 48 //-> 49 managed_shared_memory segment(create_only, "MySharedMemory", 65536); 50 //<- 51 #endif 52 //-> 53 54 //Allocate a portion of the segment (raw memory) 55 managed_shared_memory::size_type free_memory = segment.get_free_memory(); 56 void * shptr = segment.allocate(1024/*bytes to allocate*/); 57 58 //Check invariant 59 if(free_memory <= segment.get_free_memory()) 60 return 1; 61 62 //An handle from the base address can identify any byte of the shared 63 //memory segment even if it is mapped in different base addresses 64 managed_shared_memory::handle_t handle = segment.get_handle_from_address(shptr); 65 std::stringstream s; 66 s << argv[0] << " " << handle; 67 //<- 68 s << " " << test::get_process_id_name(); 69 //-> 70 s << std::ends; 71 //Launch child process 72 if(0 != std::system(s.str().c_str())) 73 return 1; 74 //Check memory has been freed 75 if(free_memory != segment.get_free_memory()) 76 return 1; 77 } 78 else{ 79 //Open managed segment 80 //<- 81 #if 1 82 managed_shared_memory segment(open_only, argv[2]); 83 #else 84 //-> 85 managed_shared_memory segment(open_only, "MySharedMemory"); 86 //<- 87 #endif 88 //-> 89 90 //An handle from the base address can identify any byte of the shared 91 //memory segment even if it is mapped in different base addresses 92 managed_shared_memory::handle_t handle = 0; 93 94 //Obtain handle value 95 std::stringstream s; s << argv[1]; s >> handle; 96 97 //Get buffer local address from handle 98 void *msg = segment.get_address_from_handle(handle); 99 100 //Deallocate previously allocated memory 101 segment.deallocate(msg); 102 } 103 return 0; 104 } 105 //] 106 #include <boost/interprocess/detail/config_end.hpp> 107