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//[doc_file_mapping
12#include <boost/interprocess/file_mapping.hpp>
13#include <boost/interprocess/mapped_region.hpp>
14#include <iostream>
15#include <fstream>
16#include <string>
17#include <vector>
18#include <cstring>
19#include <cstddef>
20#include <cstdlib>
21//<-
22#include "../test/get_process_id_name.hpp"
23//->
24
25int main(int argc, char *argv[])
26{
27 using namespace boost::interprocess;
28
29 //Define file names
30 //<-
31 #if 1
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();
35 #else
36 //->
37 const char *FileName = "file.bin";
38 //<-
39 #endif
40 //->
41 const std::size_t FileSize = 10000;
42
43 if(argc == 1){ //Parent process executes this
44 { //Create a file
45 file_mapping::remove(filename: FileName);
46 std::filebuf fbuf;
47 fbuf.open(s: FileName, mode: std::ios_base::in | std::ios_base::out
48 | std::ios_base::trunc | std::ios_base::binary);
49 //Set the size
50 fbuf.pubseekoff(off: FileSize-1, way: std::ios_base::beg);
51 fbuf.sputc(c: 0);
52 }
53
54 //Remove on exit
55 struct file_remove
56 {
57 file_remove(const char *FileName)
58 : FileName_(FileName) {}
59 ~file_remove(){ file_mapping::remove(filename: FileName_); }
60 const char *FileName_;
61 } remover(FileName);
62
63 //Create a file mapping
64 file_mapping m_file(FileName, read_write);
65
66 //Map the whole file with read-write permissions in this process
67 mapped_region region(m_file, read_write);
68
69 //Get the address of the mapped region
70 void * addr = region.get_address();
71 std::size_t size = region.get_size();
72
73 //Write all the memory to 1
74 std::memset(s: addr, c: 1, n: size);
75
76 //Launch child process
77 std::string s(argv[0]); s += " child ";
78 //<-
79 s += "\""; s+= FileName; s += "\"";
80 //->
81 if(0 != std::system(command: s.c_str()))
82 return 1;
83 }
84 else{ //Child process executes this
85 { //Open the file mapping and map it as read-only
86 //<-
87 #if 1
88 file_mapping m_file(argv[2], read_only);
89 #else
90 //->
91 file_mapping m_file(FileName, read_only);
92 //<-
93 #endif
94 //->
95
96 mapped_region region(m_file, read_only);
97
98 //Get the address of the mapped region
99 void * addr = region.get_address();
100 std::size_t size = region.get_size();
101
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)
105 if(*mem++ != 1)
106 return 1; //Error checking memory
107 }
108 { //Now test it reading the file
109 std::filebuf fbuf;
110 //<-
111 #if 1
112 fbuf.open(s: argv[2], mode: std::ios_base::in | std::ios_base::binary);
113 #else
114 //->
115 fbuf.open(FileName, std::ios_base::in | std::ios_base::binary);
116 //<-
117 #endif
118 //->
119
120 //Read it to memory
121 std::vector<char> vect(FileSize, 0);
122 fbuf.sgetn(s: &vect[0], n: std::streamsize(vect.size()));
123
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)
127 if(*mem++ != 1)
128 return 1; //Error checking memory
129 }
130 }
131
132 return 0;
133}
134//]
135
136

source code of boost/libs/interprocess/example/doc_file_mapping.cpp