]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/example/doc_windows_shared_memory.cpp
1 //////////////////////////////////////////////////////////////////////////////
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)
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 #if defined(BOOST_INTERPROCESS_WINDOWS) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
15 //[doc_windows_shared_memory
16 #include <boost/interprocess/windows_shared_memory.hpp>
17 #include <boost/interprocess/mapped_region.hpp>
22 #include "../test/get_process_id_name.hpp"
25 int main(int argc
, char *argv
[])
27 using namespace boost::interprocess
;
29 if(argc
== 1){ //Parent process
30 //Create a native windows shared memory object.
33 windows_shared_memory
shm (create_only
, test::get_process_id_name(), read_write
, 1000);
36 windows_shared_memory
shm (create_only
, "MySharedMemory", read_write
, 1000);
41 //Map the whole shared memory in this process
42 mapped_region
region(shm
, read_write
);
44 //Write all the memory to 1
45 std::memset(region
.get_address(), 1, region
.get_size());
47 //Launch child process
48 std::string
s(argv
[0]); s
+= " child ";
50 s
+= test::get_process_id_name();
52 if(0 != std::system(s
.c_str()))
54 //windows_shared_memory is destroyed when the last attached process dies...
57 //Open already created shared memory object.
60 windows_shared_memory
shm (open_only
, argv
[2], read_only
);
63 windows_shared_memory
shm (open_only
, "MySharedMemory", read_only
);
68 //Map the whole shared memory in this process
69 mapped_region
region(shm
, read_only
);
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
)
75 return 1; //Error checking memory
82 #else //BOOST_INTERPROCESS_WINDOWS
89 #endif //BOOST_INTERPROCESS_WINDOWS
91 #include <boost/interprocess/detail/config_end.hpp>