1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006-2009. 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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
10 #include <boost/interprocess/detail/config_begin.hpp>
11 #include <boost/interprocess/detail/workaround.hpp>
13 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <cstdlib> //std::system
17 #include "../test/get_process_id_name.hpp"
20 int main (int argc, char *argv[])
22 using namespace boost::interprocess;
23 if(argc == 1){ //Parent process
24 //Remove shared memory on construction and destruction
29 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
30 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
33 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
34 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
40 //Create a managed shared memory segment
43 managed_shared_memory segment(create_only, test::get_process_id_name(), 65536);
46 managed_shared_memory segment(create_only, "MySharedMemory", 65536);
51 //Allocate a portion of the segment (raw memory)
52 std::size_t free_memory = segment.get_free_memory();
53 void * shptr = segment.allocate(1024/*bytes to allocate*/);
56 if(free_memory <= segment.get_free_memory())
59 //An handle from the base address can identify any byte of the shared
60 //memory segment even if it is mapped in different base addresses
61 managed_shared_memory::handle_t handle = segment.get_handle_from_address(shptr);
63 s << argv[0] << " " << handle;
65 s << " " << test::get_process_id_name();
68 //Launch child process
69 if(0 != std::system(s.str().c_str()))
71 //Check memory has been freed
72 if(free_memory != segment.get_free_memory())
76 //Open managed segment
79 managed_shared_memory segment(open_only, argv[2]);
82 managed_shared_memory segment(open_only, "MySharedMemory");
87 //An handle from the base address can identify any byte of the shared
88 //memory segment even if it is mapped in different base addresses
89 managed_shared_memory::handle_t handle = 0;
92 std::stringstream s; s << argv[1]; s >> handle;
94 //Get buffer local address from handle
95 void *msg = segment.get_address_from_handle(handle);
97 //Deallocate previously allocated memory
98 segment.deallocate(msg);
103 #include <boost/interprocess/detail/config_end.hpp>