]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/example/doc_file_mapping.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>
12 #include <boost/interprocess/file_mapping.hpp>
13 #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
;
32 std::string
file_name(boost::interprocess::ipcdetail::get_temporary_path());
33 file_name
+= "/"; file_name
+= test::get_process_id_name();
34 const char *FileName
= file_name
.c_str();
37 const char *FileName
= "file.bin";
41 const std::size_t FileSize
= 10000;
43 if(argc
== 1){ //Parent process executes this
45 file_mapping::remove(FileName
);
47 fbuf
.open(FileName
, std::ios_base::in
| std::ios_base::out
48 | std::ios_base::trunc
| std::ios_base::binary
);
50 fbuf
.pubseekoff(FileSize
-1, std::ios_base::beg
);
57 file_remove(const char *FileName
)
58 : FileName_(FileName
) {}
59 ~file_remove(){ file_mapping::remove(FileName_
); }
60 const char *FileName_
;
63 //Create a file mapping
64 file_mapping
m_file(FileName
, read_write
);
66 //Map the whole file with read-write permissions in this process
67 mapped_region
region(m_file
, read_write
);
69 //Get the address of the mapped region
70 void * addr
= region
.get_address();
71 std::size_t size
= region
.get_size();
73 //Write all the memory to 1
74 std::memset(addr
, 1, size
);
76 //Launch child process
77 std::string
s(argv
[0]); s
+= " child ";
79 s
+= "\""; s
+= FileName
; s
+= "\"";
81 if(0 != std::system(s
.c_str()))
84 else{ //Child process executes this
85 { //Open the file mapping and map it as read-only
88 file_mapping
m_file(argv
[2], read_only
);
91 file_mapping
m_file(FileName
, read_only
);
96 mapped_region
region(m_file
, read_only
);
98 //Get the address of the mapped region
99 void * addr
= region
.get_address();
100 std::size_t size
= region
.get_size();
102 //Check that memory was initialized to 1
103 const char *mem
= static_cast<char*>(addr
);
104 for(std::size_t i
= 0; i
< size
; ++i
)
106 return 1; //Error checking memory
108 { //Now test it reading the file
112 fbuf
.open(argv
[2], std::ios_base::in
| std::ios_base::binary
);
115 fbuf
.open(FileName
, std::ios_base::in
| std::ios_base::binary
);
121 std::vector
<char> vect(FileSize
, 0);
122 fbuf
.sgetn(&vect
[0], std::streamsize(vect
.size()));
124 //Check that memory was initialized to 1
125 const char *mem
= static_cast<char*>(&vect
[0]);
126 for(std::size_t i
= 0; i
< FileSize
; ++i
)
128 return 1; //Error checking memory
135 #include <boost/interprocess/detail/config_end.hpp>