doc_xsi_shared_memory.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #if defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  13. //[doc_xsi_shared_memory
  14. #include <boost/interprocess/xsi_shared_memory.hpp>
  15. #include <boost/interprocess/mapped_region.hpp>
  16. #include <cstring>
  17. #include <cstdlib>
  18. #include <string>
  19. using namespace boost::interprocess;
  20. void remove_old_shared_memory(const xsi_key &key)
  21. {
  22. try{
  23. xsi_shared_memory xsi(open_only, key);
  24. xsi_shared_memory::remove(xsi.get_shmid());
  25. }
  26. catch(interprocess_exception &e){
  27. if(e.get_error_code() != not_found_error)
  28. throw;
  29. }
  30. }
  31. int main(int argc, char *argv[])
  32. {
  33. if(argc == 1){ //Parent process
  34. //Build XSI key (ftok based)
  35. xsi_key key(argv[0], 1);
  36. remove_old_shared_memory(key);
  37. //Create a shared memory object.
  38. xsi_shared_memory shm (create_only, key, 1000);
  39. //Remove shared memory on destruction
  40. struct shm_remove
  41. {
  42. int shmid_;
  43. shm_remove(int shmid) : shmid_(shmid){}
  44. ~shm_remove(){ xsi_shared_memory::remove(shmid_); }
  45. } remover(shm.get_shmid());
  46. //Map the whole shared memory in this process
  47. mapped_region region(shm, read_write);
  48. //Write all the memory to 1
  49. std::memset(region.get_address(), 1, region.get_size());
  50. //Launch child process
  51. std::string s(argv[0]); s += " child ";
  52. if(0 != std::system(s.c_str()))
  53. return 1;
  54. }
  55. else{
  56. //Build XSI key (ftok based)
  57. xsi_key key(argv[0], 1);
  58. //Create a shared memory object.
  59. xsi_shared_memory shm (open_only, key);
  60. //Map the whole shared memory in this process
  61. mapped_region region(shm, read_only);
  62. //Check that memory was initialized to 1
  63. char *mem = static_cast<char*>(region.get_address());
  64. for(std::size_t i = 0; i < region.get_size(); ++i)
  65. if(*mem++ != 1)
  66. return 1; //Error checking memory
  67. }
  68. return 0;
  69. }
  70. //]
  71. #else //BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
  72. int main()
  73. {
  74. return 0;
  75. }
  76. #endif //BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
  77. #include <boost/interprocess/detail/config_end.hpp>