Guest User

Untitled

a guest
Jun 13th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #include <boost/interprocess/managed_shared_memory.hpp>
  2. #include <cstring>
  3. #include <string>
  4. #include <cstdlib> //std::system
  5. #include <iostream>
  6. #include <stdio.h>
  7.  
  8. using namespace boost::interprocess;
  9.  
  10. class MyClass {
  11. public:
  12. MyClass();
  13. ~MyClass();
  14. std::string name;
  15. };
  16. MyClass::MyClass() { this->name = "init"; }
  17. MyClass::~MyClass() { }
  18.  
  19. int main( int argc, char *argv[] ) {
  20. if ( argc == 1 ) { // Parent process
  21. std::cout << "parent: " << std::endl;
  22. struct shm_remove { // Remove shared memory on construction and destruction
  23. shm_remove() { shared_memory_object::remove("MySharedMemory"); }
  24. ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
  25. } remover;
  26. managed_shared_memory shm(create_only, "MySharedMemory", 1000); // Create a managed shared memory
  27. MyClass *myclass = shm.construct<MyClass>("MyClass")(); // Construct a named object
  28. std::cout << "parent see: " << myclass << std::endl;
  29. std::cout << "parent see: " << myclass->name << std::endl;
  30. std::string s( argv[0] );
  31. s += " child ";
  32. std::system( s.c_str() );
  33. } else {
  34. std::cout << "child: " << std::endl;
  35. managed_shared_memory shm(open_only, "MySharedMemory");
  36. MyClass *myclass = shm.find<MyClass>("MyClass").first;
  37. std::cout << "child see: " << myclass << std::endl;
  38. std::cout << "child see: " << myclass->name << std::endl;
  39. }
  40. return 0;
  41. }
  42.  
  43. parent:
  44. parent see: 0x7f54974fc118
  45. parent see: init
  46. child:
  47. child see: 0x7f8431583118
  48. Segmentation fault
Add Comment
Please, Sign In to add comment