• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_spawn_vector
13 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <boost/interprocess/containers/vector.hpp>
15 #include <boost/interprocess/allocators/allocator.hpp>
16 #include <string>
17 #include <cstdlib> //std::system
18 //<-
19 #include "../test/get_process_id_name.hpp"
20 //->
21 
22 using namespace boost::interprocess;
23 
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;
27 
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;
31 
32 //Main function. For parent process argc == 1, for child process argc == 2
main(int argc,char * argv[])33 int main(int argc, char *argv[])
34 {
35    if(argc == 1){ //Parent process
36       //Remove shared memory on construction and destruction
37       struct shm_remove
38       {
39       //<-
40       #if 1
41          shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
42          ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
43       #else
44       //->
45          shm_remove() { shared_memory_object::remove("MySharedMemory"); }
46          ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
47       //<-
48       #endif
49       //->
50       } remover;
51       //<-
52       (void)remover;
53       //->
54 
55       //Create a new segment with given name and size
56       //<-
57       #if 1
58       managed_shared_memory segment(create_only, test::get_process_id_name(), 65536);
59       #else
60       //->
61       managed_shared_memory segment(create_only, "MySharedMemory", 65536);
62       //<-
63       #endif
64       //->
65 
66       //Initialize shared memory STL-compatible allocator
67       const ShmemAllocator alloc_inst (segment.get_segment_manager());
68 
69       //Construct a vector named "MyVector" in shared memory with argument alloc_inst
70       MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst);
71 
72       for(int i = 0; i < 100; ++i)  //Insert data in the vector
73          myvector->push_back(i);
74 
75       //Launch child process
76       std::string s(argv[0]); s += " child ";
77       //<-
78       s += test::get_process_id_name();
79       //->
80       if(0 != std::system(s.c_str()))
81          return 1;
82 
83       //Check child has destroyed the vector
84       if(segment.find<MyVector>("MyVector").first)
85          return 1;
86    }
87    else{ //Child process
88       //Open the managed segment
89       //<-
90       #if 1
91       managed_shared_memory segment(open_only, argv[2]);
92       #else
93       //->
94       managed_shared_memory segment(open_only, "MySharedMemory");
95       //<-
96       #endif
97       //->
98 
99       //Find the vector using the c-string name
100       MyVector *myvector = segment.find<MyVector>("MyVector").first;
101 
102       //Use vector in reverse order
103       std::sort(myvector->rbegin(), myvector->rend());
104 
105       //When done, destroy the vector from the segment
106       segment.destroy<MyVector>("MyVector");
107    }
108 
109    return 0;
110 }
111 
112 //]
113 #include <boost/interprocess/detail/config_end.hpp>
114