doc_shared_memory.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_shared_memory
  12. #include <boost/interprocess/shared_memory_object.hpp>
  13. #include <boost/interprocess/mapped_region.hpp>
  14. #include <cstring>
  15. #include <cstdlib>
  16. #include <string>
  17. //<-
  18. #include "../test/get_process_id_name.hpp"
  19. //->
  20. int main(int argc, char *argv[])
  21. {
  22. using namespace boost::interprocess;
  23. if(argc == 1){ //Parent process
  24. //Remove shared memory on construction and destruction
  25. struct shm_remove
  26. {
  27. //<-
  28. #if 1
  29. shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
  30. ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
  31. #else
  32. //->
  33. shm_remove() { shared_memory_object::remove("MySharedMemory"); }
  34. ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
  35. //<-
  36. #endif
  37. //->
  38. } remover;
  39. //<-
  40. (void)remover;
  41. //->
  42. //Create a shared memory object.
  43. //<-
  44. #if 1
  45. shared_memory_object shm (create_only, test::get_process_id_name(), read_write);
  46. #else
  47. //->
  48. shared_memory_object shm (create_only, "MySharedMemory", read_write);
  49. //<-
  50. #endif
  51. //->
  52. //Set size
  53. shm.truncate(1000);
  54. //Map the whole shared memory in this process
  55. mapped_region region(shm, read_write);
  56. //Write all the memory to 1
  57. std::memset(region.get_address(), 1, region.get_size());
  58. //Launch child process
  59. std::string s(argv[0]); s += " child ";
  60. //<-
  61. s += test::get_process_id_name();
  62. //->
  63. if(0 != std::system(s.c_str()))
  64. return 1;
  65. }
  66. else{
  67. //Open already created shared memory object.
  68. //<-
  69. #if 1
  70. shared_memory_object shm (open_only, argv[2], read_only);
  71. #else
  72. //->
  73. shared_memory_object shm (open_only, "MySharedMemory", read_only);
  74. //<-
  75. #endif
  76. //->
  77. //Map the whole shared memory in this process
  78. mapped_region region(shm, read_only);
  79. //Check that memory was initialized to 1
  80. char *mem = static_cast<char*>(region.get_address());
  81. for(std::size_t i = 0; i < region.get_size(); ++i)
  82. if(*mem++ != 1)
  83. return 1; //Error checking memory
  84. }
  85. return 0;
  86. }
  87. //]
  88. #include <boost/interprocess/detail/config_end.hpp>