fork download
  1. #include <boost/interprocess/shared_memory_object.hpp>
  2. #include <boost/interprocess/mapped_region.hpp>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <string>
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9. using namespace boost::interprocess;
  10.  
  11. if(argc == 1){ //Parent process
  12. //Remove shared memory on construction and destruction
  13. struct shm_remove
  14. {
  15. shm_remove() { shared_memory_object::remove("MySharedMemory"); }
  16. ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
  17. } remover;
  18.  
  19. //Create a shared memory object.
  20. shared_memory_object shm (create_only, "MySharedMemory", read_write);
  21.  
  22. //Set size
  23. shm.truncate(1000);
  24.  
  25. //Map the whole shared memory in this process
  26. mapped_region region(shm, read_write);
  27.  
  28. //Write all the memory to 1
  29. std::memset(region.get_address(), 1, region.get_size());
  30.  
  31. //Launch child process
  32. std::string s(argv[0]); s += " child ";
  33. if(0 != std::system(s.c_str()))
  34. return 1;
  35. }
  36. else{
  37. //Open already created shared memory object.
  38. shared_memory_object shm (open_only, "MySharedMemory", read_only);
  39.  
  40. //Map the whole shared memory in this process
  41. mapped_region region(shm, read_only);
  42.  
  43. //Check that memory was initialized to 1
  44. char *mem = static_cast<char*>(region.get_address());
  45. for(std::size_t i = 0; i < region.get_size(); ++i)
  46. if(*mem++ != 1)
  47. return 1; //Error checking memory
  48. }
  49. return 0;
  50. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/home/vIgWnH/ccDNZo7G.o: In function `boost::interprocess::shared_memory_object::remove(char const*)':
prog.cpp:(.text._ZN5boost12interprocess20shared_memory_object6removeEPKc[_ZN5boost12interprocess20shared_memory_object6removeEPKc]+0x70): undefined reference to `shm_unlink'
/home/vIgWnH/ccDNZo7G.o: In function `main':
prog.cpp:(.text.startup+0x86): undefined reference to `shm_open'
prog.cpp:(.text.startup+0x1c7): undefined reference to `shm_open'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty