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>
12 #include <boost/interprocess/shared_memory_object.hpp>
13 #include <boost/interprocess/mapped_region.hpp>
18 #include "../test/get_process_id_name.hpp"
21 int main(int argc, char *argv[])
23 using namespace boost::interprocess;
25 if(argc == 1){ //Parent process
26 //Remove shared memory on construction and destruction
31 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
32 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
35 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
36 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
42 //Create a shared memory object.
45 shared_memory_object shm (create_only, test::get_process_id_name(), read_write);
48 shared_memory_object shm (create_only, "MySharedMemory", read_write);
56 //Map the whole shared memory in this process
57 mapped_region region(shm, read_write);
59 //Write all the memory to 1
60 std::memset(region.get_address(), 1, region.get_size());
62 //Launch child process
63 std::string s(argv[0]); s += " child ";
65 s += test::get_process_id_name();
67 if(0 != std::system(s.c_str()))
71 //Open already created shared memory object.
74 shared_memory_object shm (open_only, argv[2], read_only);
77 shared_memory_object shm (open_only, "MySharedMemory", read_only);
82 //Map the whole shared memory in this process
83 mapped_region region(shm, read_only);
85 //Check that memory was initialized to 1
86 char *mem = static_cast<char*>(region.get_address());
87 for(std::size_t i = 0; i < region.get_size(); ++i)
89 return 1; //Error checking memory
94 #include <boost/interprocess/detail/config_end.hpp>