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 <boost/interprocess/containers/vector.hpp>
15 #include <boost/interprocess/allocators/allocator.hpp>
17 #include <cstdlib> //std::system
19 #include "../test/get_process_id_name.hpp"
22 using namespace boost::interprocess;
24 //Define an STL compatible allocator of ints that allocates from the managed_shared_memory.
25 //This allocator will allow placing containers in the segment
26 typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator;
28 //Alias a vector that uses the previous STL-like allocator so that allocates
29 //its values from the segment
30 typedef vector<int, ShmemAllocator> MyVector;
32 //Main function. For parent process argc == 1, for child process argc == 2
33 int main(int argc, char *argv[])
35 if(argc == 1){ //Parent process
36 //Remove shared memory on construction and destruction
41 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
42 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
45 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
46 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
52 //Create a new segment with given name and size
55 managed_shared_memory segment(create_only, test::get_process_id_name(), 65536);
58 managed_shared_memory segment(create_only, "MySharedMemory", 65536);
63 //Initialize shared memory STL-compatible allocator
64 const ShmemAllocator alloc_inst (segment.get_segment_manager());
66 //Construct a vector named "MyVector" in shared memory with argument alloc_inst
67 MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst);
69 for(int i = 0; i < 100; ++i) //Insert data in the vector
70 myvector->push_back(i);
72 //Launch child process
73 std::string s(argv[0]); s += " child ";
75 s += test::get_process_id_name();
77 if(0 != std::system(s.c_str()))
80 //Check child has destroyed the vector
81 if(segment.find<MyVector>("MyVector").first)
85 //Open the managed segment
88 managed_shared_memory segment(open_only, argv[2]);
91 managed_shared_memory segment(open_only, "MySharedMemory");
96 //Find the vector using the c-string name
97 MyVector *myvector = segment.find<MyVector>("MyVector").first;
99 //Use vector in reverse order
100 std::sort(myvector->rbegin(), myvector->rend());
102 //When done, destroy the vector from the segment
103 segment.destroy<MyVector>("MyVector");
110 #include <boost/interprocess/detail/config_end.hpp>