• 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 
13 #if defined(BOOST_INTERPROCESS_WINDOWS) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
14 
15 //[doc_windows_shared_memory
16 #include <boost/interprocess/windows_shared_memory.hpp>
17 #include <boost/interprocess/mapped_region.hpp>
18 #include <cstring>
19 #include <cstdlib>
20 #include <string>
21 //<-
22 #include "../test/get_process_id_name.hpp"
23 //->
24 
main(int argc,char * argv[])25 int main(int argc, char *argv[])
26 {
27    using namespace boost::interprocess;
28 
29    if(argc == 1){  //Parent process
30       //Create a native windows shared memory object.
31       //<-
32       #if 1
33       windows_shared_memory shm (create_only, test::get_process_id_name(), read_write, 1000);
34       #else
35       //->
36       windows_shared_memory shm (create_only, "MySharedMemory", read_write, 1000);
37       //<-
38       #endif
39       //->
40 
41       //Map the whole shared memory in this process
42       mapped_region region(shm, read_write);
43 
44       //Write all the memory to 1
45       std::memset(region.get_address(), 1, region.get_size());
46 
47       //Launch child process
48       std::string s(argv[0]); s += " child ";
49       //<-
50       s += test::get_process_id_name();
51       //->
52       if(0 != std::system(s.c_str()))
53          return 1;
54       //windows_shared_memory is destroyed when the last attached process dies...
55    }
56    else{
57       //Open already created shared memory object.
58       //<-
59       #if 1
60       windows_shared_memory shm (open_only, argv[2], read_only);
61       #else
62       //->
63       windows_shared_memory shm (open_only, "MySharedMemory", read_only);
64       //<-
65       #endif
66       //->
67 
68       //Map the whole shared memory in this process
69       mapped_region region(shm, read_only);
70 
71       //Check that memory was initialized to 1
72       char *mem = static_cast<char*>(region.get_address());
73       for(std::size_t i = 0; i < region.get_size(); ++i)
74          if(*mem++ != 1)
75             return 1;   //Error checking memory
76       return 0;
77    }
78    return 0;
79 }
80 //]
81 
82 #else //BOOST_INTERPROCESS_WINDOWS
83 
main()84 int main()
85 {
86    return 0;
87 }
88 
89 #endif //BOOST_INTERPROCESS_WINDOWS
90 
91 #include <boost/interprocess/detail/config_end.hpp>
92