doc_ipc_message.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include <boost/interprocess/detail/workaround.hpp>
  12. //[doc_ipc_message
  13. #include <boost/interprocess/managed_shared_memory.hpp>
  14. #include <cstdlib> //std::system
  15. #include <sstream>
  16. //<-
  17. #include "../test/get_process_id_name.hpp"
  18. //->
  19. int main (int argc, char *argv[])
  20. {
  21. using namespace boost::interprocess;
  22. if(argc == 1){ //Parent process
  23. //Remove shared memory on construction and destruction
  24. struct shm_remove
  25. {
  26. //<-
  27. #if 1
  28. shm_remove() { shared_memory_object::remove(test::get_process_id_name()); }
  29. ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); }
  30. #else
  31. //->
  32. shm_remove() { shared_memory_object::remove("MySharedMemory"); }
  33. ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
  34. //<-
  35. #endif
  36. //->
  37. } remover;
  38. //<-
  39. (void)remover;
  40. //->
  41. //Create a managed shared memory segment
  42. //<-
  43. #if 1
  44. managed_shared_memory segment(create_only, test::get_process_id_name(), 65536);
  45. #else
  46. //->
  47. managed_shared_memory segment(create_only, "MySharedMemory", 65536);
  48. //<-
  49. #endif
  50. //->
  51. //Allocate a portion of the segment (raw memory)
  52. managed_shared_memory::size_type free_memory = segment.get_free_memory();
  53. void * shptr = segment.allocate(1024/*bytes to allocate*/);
  54. //Check invariant
  55. if(free_memory <= segment.get_free_memory())
  56. return 1;
  57. //An handle from the base address can identify any byte of the shared
  58. //memory segment even if it is mapped in different base addresses
  59. managed_shared_memory::handle_t handle = segment.get_handle_from_address(shptr);
  60. std::stringstream s;
  61. s << argv[0] << " " << handle;
  62. //<-
  63. s << " " << test::get_process_id_name();
  64. //->
  65. s << std::ends;
  66. //Launch child process
  67. if(0 != std::system(s.str().c_str()))
  68. return 1;
  69. //Check memory has been freed
  70. if(free_memory != segment.get_free_memory())
  71. return 1;
  72. }
  73. else{
  74. //Open managed segment
  75. //<-
  76. #if 1
  77. managed_shared_memory segment(open_only, argv[2]);
  78. #else
  79. //->
  80. managed_shared_memory segment(open_only, "MySharedMemory");
  81. //<-
  82. #endif
  83. //->
  84. //An handle from the base address can identify any byte of the shared
  85. //memory segment even if it is mapped in different base addresses
  86. managed_shared_memory::handle_t handle = 0;
  87. //Obtain handle value
  88. std::stringstream s; s << argv[1]; s >> handle;
  89. //Get buffer local address from handle
  90. void *msg = segment.get_address_from_handle(handle);
  91. //Deallocate previously allocated memory
  92. segment.deallocate(msg);
  93. }
  94. return 0;
  95. }
  96. //]
  97. #include <boost/interprocess/detail/config_end.hpp>