]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/example/doc_ipc_message.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 #include <boost/interprocess/managed_shared_memory.hpp>
14 #include <cstdlib> //std::system
17 #include "../test/get_process_id_name.hpp"
20 int main (int argc
, char *argv
[])
22 using namespace boost::interprocess
;
23 if(argc
== 1){ //Parent process
24 //Remove shared memory on construction and destruction
29 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
30 ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
33 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
34 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
43 //Create a managed shared memory segment
46 managed_shared_memory
segment(create_only
, test::get_process_id_name(), 65536);
49 managed_shared_memory
segment(create_only
, "MySharedMemory", 65536);
54 //Allocate a portion of the segment (raw memory)
55 managed_shared_memory::size_type free_memory
= segment
.get_free_memory();
56 void * shptr
= segment
.allocate(1024/*bytes to allocate*/);
59 if(free_memory
<= segment
.get_free_memory())
62 //An handle from the base address can identify any byte of the shared
63 //memory segment even if it is mapped in different base addresses
64 managed_shared_memory::handle_t handle
= segment
.get_handle_from_address(shptr
);
66 s
<< argv
[0] << " " << handle
;
68 s
<< " " << test::get_process_id_name();
71 //Launch child process
72 if(0 != std::system(s
.str().c_str()))
74 //Check memory has been freed
75 if(free_memory
!= segment
.get_free_memory())
79 //Open managed segment
82 managed_shared_memory
segment(open_only
, argv
[2]);
85 managed_shared_memory
segment(open_only
, "MySharedMemory");
90 //An handle from the base address can identify any byte of the shared
91 //memory segment even if it is mapped in different base addresses
92 managed_shared_memory::handle_t handle
= 0;
95 std::stringstream s
; s
<< argv
[1]; s
>> handle
;
97 //Get buffer local address from handle
98 void *msg
= segment
.get_address_from_handle(handle
);
100 //Deallocate previously allocated memory
101 segment
.deallocate(msg
);
106 #include <boost/interprocess/detail/config_end.hpp>