]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/example/doc_xsi_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 //////////////////////////////////////////////////////////////////////////////
11 #include <boost/interprocess/detail/workaround.hpp>
13 #if defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
15 //[doc_xsi_shared_memory
16 #include <boost/interprocess/xsi_shared_memory.hpp>
17 #include <boost/interprocess/mapped_region.hpp>
22 using namespace boost::interprocess
;
24 void remove_old_shared_memory(const xsi_key
&key
)
27 xsi_shared_memory
xsi(open_only
, key
);
28 xsi_shared_memory::remove(xsi
.get_shmid());
30 BOOST_CATCH(interprocess_exception
&e
){
31 if(e
.get_error_code() != not_found_error
)
36 int main(int argc
, char *argv
[])
38 if(argc
== 1){ //Parent process
39 //Build XSI key (ftok based)
40 xsi_key
key(argv
[0], 1);
42 remove_old_shared_memory(key
);
44 //Create a shared memory object.
45 xsi_shared_memory
shm (create_only
, key
, 1000);
47 //Remove shared memory on destruction
51 shm_remove(int shmid
) : shmid_(shmid
){}
52 ~shm_remove(){ xsi_shared_memory::remove(shmid_
); }
53 } remover(shm
.get_shmid());
55 //Map the whole shared memory in this process
56 mapped_region
region(shm
, read_write
);
58 //Write all the memory to 1
59 std::memset(region
.get_address(), 1, region
.get_size());
61 //Launch child process
62 std::string
s(argv
[0]); s
+= " child ";
63 if(0 != std::system(s
.c_str()))
67 //Build XSI key (ftok based)
68 xsi_key
key(argv
[0], 1);
70 //Create a shared memory object.
71 xsi_shared_memory
shm (open_only
, key
);
73 //Map the whole shared memory in this process
74 mapped_region
region(shm
, read_only
);
76 //Check that memory was initialized to 1
77 char *mem
= static_cast<char*>(region
.get_address());
78 for(std::size_t i
= 0; i
< region
.get_size(); ++i
)
80 return 1; //Error checking memory
86 #else //BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
93 #endif //BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS