Boost logo

Boost :

From: Daren Lee (dropshot405_at_[hidden])
Date: 2006-12-06 17:19:39


Hi -

I am having problems porting some boost::interprocess code from Win32 to Linux
(rhel4). I am using boost-1.55.

Here's the setup: I need to find if there is an existing instance of the
process already running. When the process starts up, it checks the shared
memory for a bIsProcessingRunning flag. If it is true, the process dumps some
data into the shared memory and then exits. If not true, then the process
starts up normally and then checks the shared memory for any updates. The
class I use to wrap the boost::interprocess is below.

Note: I needed the shared memory location to only be initialized the first
time it was created so the only way I saw how to do it was through the src
code was to use the initialization_func_t().

This approach works fine on Win32 but on Linux, it doesn't work.

Thanks in advance for any suggestions or hints.

-------------------------------------------------------------

#pragma once

#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>

//----------------------------------------------------------------
// SSharedData
// Structure of shared memory data contents
//----------------------------------------------------------------
struct SSharedData
{
   SSharedData() :
       bIsProcessRunning( false ),
       bIsData( false ),
       argc( 0 )
   {
   }

   //Mutex to protect access to the queue
   boost::interprocess::interprocess_mutex mutex;

   bool bIsProcessRunning;
   bool bIsData;
   int argc;
   char argv[100][100];
};

//----------------------------------------------------------------
// CSharedMemory
// Wrapper class to boost::interprocess for shared memory
//----------------------------------------------------------------
class CSharedMemory
{
public:
   //----------------------------------------------------------------
   // initialization_func_t
   // helper class to initialize shared memory to SSharedData
   //----------------------------------------------------------------
   class initialization_func_t
   {
   public:
       initialization_func_t() {}

       bool operator()( void *pAddress, std::size_t nSize, bool bCreated )
       {
           char *mptr;

           if( bCreated )
           {
               mptr = reinterpret_cast<char*>(pAddress);
               // construct shared data structure
               BOOST_TRY
               {
                   new (mptr) SSharedData();
               }
               BOOST_CATCH( ... )
               {
                   return false;
               }
               BOOST_CATCH_END
           }
           return true;
       }
   };

public:
   CSharedMemory( std::string sName ) :
       m_pData( NULL )
   {
       try
       {
           // erase previous shared memory
           boost::interprocess::shared_memory_object::remove( sName.c_str() );

           // open a tempory shared memory object
           m_pShmem = new
boost::interprocess::detail::managed_open_or_create_impl<boost::interprocess::s
hared_memory_object>( boost::interprocess::open_or_create, sName.c_str() ,
sizeof( SSharedData ), boost::interprocess::read_write, (void*)0,
               //Prepare initialization functor
               initialization_func_t() );

           // get the address of the mapped region
           void *pAddr = m_pShmem->get_address();

           // construct the shared structure in memory
           m_pData = static_cast<SSharedData*>(pAddr);
       }
       catch( boost::interprocess::interprocess_exception &ex )
       {
           std::cout << ex.what() << std::endl;
       }

   }

   ~CSharedMemory()
   {
       delete m_pShmem;
   }

   SSharedData *GetData() { return m_pData; }

protected:
   SSharedData *m_pData;
   boost::interprocess::detail::managed_open_or_create_impl<
boost::interprocess::shared_memory_object > *m_pShmem;
};


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk