Boost logo

Boost Users :

Subject: [Boost-users] error in getting shared vector
From: manish4gupta (manish_at_[hidden])
Date: 2009-09-17 02:24:57


I am woking on IPC shared memory where i am loading one vector in memory by
process1 and using the same with another program. but in process2 i am
getting one error whie this code is same as given
http://www.boost.org/doc/libs/1_38_0/doc/html/interprocess/quick_guide.html#interprocess.quick_guide.qg_offset_ptr.
I am just doing benchmarking. May i know what is the reason behind this?

Process1.

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>

int main ()
{
   using namespace boost::interprocess;
   try{
      //Shared memory front-end that is able to construct objects
      //associated with a c-string. Erase previous shared memory with the
name
      //to be used and create the memory segment at the specified address
and initialize resources
      shared_memory_object::remove("MySharedMemory");
      managed_shared_memory segment
         (create_only
         ,"MySharedMemory" //segment name
         ,65536); //segment size in bytes

      //Alias an STL compatible allocator of ints that allocates ints from
the managed
      //shared memory segment. This allocator will allow to place
containers
      //in managed shared memory segments
      typedef allocator<int, managed_shared_memory::segment_manager>
         ShmemAllocator;

      //Alias a vector that uses the previous STL-like allocator
      typedef vector<int, ShmemAllocator> MyVector;

      //Initialize shared memory STL-compatible allocator
      const ShmemAllocator alloc_inst (segment.get_segment_manager());

      //Construct a shared memory
      MyVector *myvector =
         segment.construct<MyVector>("MyVector") //object name
                                    (alloc_inst);//first ctor parameter

      //Insert data in the vector
      for(int i = 0; i < 100; ++i){
         myvector->push_back(i);
      }
   }
   catch(...){
      shared_memory_object::remove("MySharedMemory");
      throw;
   }
   shared_memory_object::remove("MySharedMemory");
   return 0;
}

Process2

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <algorithm>
#include <iostream>
int main ()
{
   using namespace boost::interprocess;
   try{
      //A special shared memory where we can
      //construct objects associated with a name.
      //Connect to the already created shared memory segment
      //and initialize needed resources
      managed_shared_memory segment
         (open_only
         ,"MySharedMemory"); //segment name

      //Alias an STL compatible allocator of ints that allocates ints from
the managed
      //shared memory segment. This allocator will allow to place
containers
      //in managed shared memory segments
      typedef allocator<int, managed_shared_memory::segment_manager>
         ShmemAllocator;

      //Alias a vector that uses the previous STL-like allocator
      typedef vector<int, ShmemAllocator> MyVector;

      //Find the vector using the c-string name
      MyVector *myvector = segment.find<MyVector>("MyVector").first;
       
     for (MyVector::iterator it = myvector->begin(); it!=myvector->end();
it++) {
          std::cout << *it << "\n";
     }
      //Use vector in reverse order
      std::sort(myvector->rbegin(), myvector->rend());
      // . . .

      //When done, destroy the vector from the segment
      segment.destroy<MyVector>("MyVector");
   }
   catch(...){
      shared_memory_object::remove("MySharedMemory");
      throw;
   }
   shared_memory_object::remove("MySharedMemory");
   return 0;
}

Error Message:

terminate called after throwing an instance of
'boost::interprocess::interprocess_exception'
  what(): No such file or directory
Aborted

-- 
View this message in context: http://www.nabble.com/error-in-getting-shared-vector-tp25485820p25485820.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net