]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/example/doc_xsi_shared_memory.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / interprocess / example / doc_xsi_shared_memory.cpp
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
11 #include <boost/interprocess/detail/workaround.hpp>
12
13 #if defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
14
15 //[doc_xsi_shared_memory
16 #include <boost/interprocess/xsi_shared_memory.hpp>
17 #include <boost/interprocess/mapped_region.hpp>
18 #include <cstring>
19 #include <cstdlib>
20 #include <string>
21
22 using namespace boost::interprocess;
23
24 void remove_old_shared_memory(const xsi_key &key)
25 {
26 BOOST_TRY{
27 xsi_shared_memory xsi(open_only, key);
28 xsi_shared_memory::remove(xsi.get_shmid());
29 }
30 BOOST_CATCH(interprocess_exception &e){
31 if(e.get_error_code() != not_found_error)
32 BOOST_RETHROW
33 } BOOST_CATCH_END
34 }
35
36 int main(int argc, char *argv[])
37 {
38 if(argc == 1){ //Parent process
39 //Build XSI key (ftok based)
40 xsi_key key(argv[0], 1);
41
42 remove_old_shared_memory(key);
43
44 //Create a shared memory object.
45 xsi_shared_memory shm (create_only, key, 1000);
46
47 //Remove shared memory on destruction
48 struct shm_remove
49 {
50 int shmid_;
51 shm_remove(int shmid) : shmid_(shmid){}
52 ~shm_remove(){ xsi_shared_memory::remove(shmid_); }
53 } remover(shm.get_shmid());
54
55 //Map the whole shared memory in this process
56 mapped_region region(shm, read_write);
57
58 //Write all the memory to 1
59 std::memset(region.get_address(), 1, region.get_size());
60
61 //Launch child process
62 std::string s(argv[0]); s += " child ";
63 if(0 != std::system(s.c_str()))
64 return 1;
65 }
66 else{
67 //Build XSI key (ftok based)
68 xsi_key key(argv[0], 1);
69
70 //Create a shared memory object.
71 xsi_shared_memory shm (open_only, key);
72
73 //Map the whole shared memory in this process
74 mapped_region region(shm, read_only);
75
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)
79 if(*mem++ != 1)
80 return 1; //Error checking memory
81 }
82 return 0;
83 }
84 //]
85
86 #else //BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
87
88 int main()
89 {
90 return 0;
91 }
92
93 #endif //BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
94
95