Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This is bit long, but I need to explain something before I can ask for actual reviews/advice.

I have to test generated C code from Matlab Simulink model. I can create a executable binary from those. I need to test this application using tests developed using Python scripting.

I came up with following approach: inter-process communication using shared memory.

I planned to write a DLL or a shared object, which enables the communication between the tests scripts and the application. This DLL will be used via both Python scripts and application to interact with each other

Here's my DLL code, which uses boost::interprocess shared memory.

int create_shmem(const char* shmem_name, long long length  )
{
    try{
       // remove_shmem(shmem_name);
        boost::interprocess::shared_memory_object shm 
                          (boost::interprocess::create_only,
                           shmem_name,
                           boost::interprocess::read_write);
      //Set size
      shm.truncate(length);
      return 1;
      }
      catch(...){
        std::cerr << "Error Creating Shared Memory : " << shmem_name << std::endl;
      }
      return 0;
}

int remove_shmem(const char* shmem_name )
{
    try {
        boost::interprocess::shared_memory_object::remove(shmem_name); 
        return 1;
    }
    catch(...){
        std::cerr << "Error Removing Shared Memory : "<< shmem_name << std::endl;
    }
    return 0;
}

int write_shmem(const char* shmem_name, char* shmem)
{
      try{
            boost::interprocess::shared_memory_object shm
            (boost::interprocess::open_only, //Open if already created
                 shmem_name, 
             boost::interprocess::read_write);

      //Map the whole shared memory in this process
            boost::interprocess::mapped_region region
                (shm, 
                boost::interprocess::read_write
                );

            std::memcpy(region.get_address(), shmem, region.get_size());
            return 1;
      }
      catch(...){
         std::cerr << "Error Writing to Shared Memory " << shmem_name << std::endl;
      }
      return 0;
}


int read_shmem( const char* shmem_name, char *shmem )
{
      //Open already created shared memory object.
     // Same process might not be calling it, so create new object
     try{
            boost::interprocess::shared_memory_object shm (
                boost::interprocess::open_only, 
                    shmem_name, 
                    boost::interprocess::read_only);

      //Map the whole shared memory in this process
            boost::interprocess::mapped_region region
                (shm, 
                     boost::interprocess::read_only
                );

      //CAUTION : No check is performed
    std::memcpy(shmem, reinterpret_cast<char*> (region.get_address()),
                       region.get_size()); 

      return 1;
      }

      catch(...){
      std::cerr << "Error Reading from Shared Memory " << shmem_name << std::endl;
      }

   return 0;
}

The application needs simulation of around 1.5K different signals of different data types all packed in a C struct.

My initial though is to created a shared memory and write the simulated signals on to it which the application can fetch and accordingly shows the output by writing back to same shared memory which is again read from test scripts.

Any help/advice regarding underlying issues/failure and improvement will be much appreciated.

I can post my ctypes Python interface if required.

share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.