doc_file_mapping.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include <boost/interprocess/detail/config_begin.hpp>
  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. int main(int argc, char *argv[])
  25. {
  26. using namespace boost::interprocess;
  27. //Define file names
  28. //<-
  29. #if 1
  30. std::string file_name(boost::interprocess::ipcdetail::get_temporary_path());
  31. file_name += "/"; file_name += test::get_process_id_name();
  32. const char *FileName = file_name.c_str();
  33. #else
  34. //->
  35. const char *FileName = "file.bin";
  36. //<-
  37. #endif
  38. //->
  39. const std::size_t FileSize = 10000;
  40. if(argc == 1){ //Parent process executes this
  41. { //Create a file
  42. file_mapping::remove(FileName);
  43. std::filebuf fbuf;
  44. fbuf.open(FileName, std::ios_base::in | std::ios_base::out
  45. | std::ios_base::trunc | std::ios_base::binary);
  46. //Set the size
  47. fbuf.pubseekoff(FileSize-1, std::ios_base::beg);
  48. fbuf.sputc(0);
  49. }
  50. //Remove on exit
  51. struct file_remove
  52. {
  53. file_remove(const char *FileName)
  54. : FileName_(FileName) {}
  55. ~file_remove(){ file_mapping::remove(FileName_); }
  56. const char *FileName_;
  57. } remover(FileName);
  58. //Create a file mapping
  59. file_mapping m_file(FileName, read_write);
  60. //Map the whole file with read-write permissions in this process
  61. mapped_region region(m_file, read_write);
  62. //Get the address of the mapped region
  63. void * addr = region.get_address();
  64. std::size_t size = region.get_size();
  65. //Write all the memory to 1
  66. std::memset(addr, 1, size);
  67. //Launch child process
  68. std::string s(argv[0]); s += " child ";
  69. //<-
  70. s += "\""; s+= FileName; s += "\"";
  71. //->
  72. if(0 != std::system(s.c_str()))
  73. return 1;
  74. }
  75. else{ //Child process executes this
  76. { //Open the file mapping and map it as read-only
  77. //<-
  78. #if 1
  79. file_mapping m_file(argv[2], read_only);
  80. #else
  81. //->
  82. file_mapping m_file(FileName, read_only);
  83. //<-
  84. #endif
  85. //->
  86. mapped_region region(m_file, read_only);
  87. //Get the address of the mapped region
  88. void * addr = region.get_address();
  89. std::size_t size = region.get_size();
  90. //Check that memory was initialized to 1
  91. const char *mem = static_cast<char*>(addr);
  92. for(std::size_t i = 0; i < size; ++i)
  93. if(*mem++ != 1)
  94. return 1; //Error checking memory
  95. }
  96. { //Now test it reading the file
  97. std::filebuf fbuf;
  98. //<-
  99. #if 1
  100. fbuf.open(argv[2], std::ios_base::in | std::ios_base::binary);
  101. #else
  102. //->
  103. fbuf.open(FileName, std::ios_base::in | std::ios_base::binary);
  104. //<-
  105. #endif
  106. //->
  107. //Read it to memory
  108. std::vector<char> vect(FileSize, 0);
  109. fbuf.sgetn(&vect[0], std::streamsize(vect.size()));
  110. //Check that memory was initialized to 1
  111. const char *mem = static_cast<char*>(&vect[0]);
  112. for(std::size_t i = 0; i < FileSize; ++i)
  113. if(*mem++ != 1)
  114. return 1; //Error checking memory
  115. }
  116. }
  117. return 0;
  118. }
  119. //]
  120. #include <boost/interprocess/detail/config_end.hpp>