使用与char不同的类型来增加shared_memory_object问题

时间:2010-01-06 00:34:41

标签: c++ boost concurrency shared-memory interprocess

我遇到了boost shared_memory_object和mapped_region的问题。我想在内存对象中编写一组对象(结构)。如果结构只包含一个字符,一切都很好;如果我只是在结构中添加一个int,那么如果我放了太多的对象(让我们说70,比块的极限小得多)我写的时会出现分段错误。

到目前为止,我刚刚看到了将简单字符写入共享内存的示例,但我还没有读到任何可以使用的对象类型的内容。我想知道是否必须在我的对象和字节流之间进行转换,或者这样的函数是否已经存在。或者,如果我在代码中做错了什么。注释行是在退出时给我一个段错误的行...

#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <iostream>
#include <unistd.h>

struct Record {
    char c;
    int i;
//  float f;
//  double d;
//  char cs[32];
//  boost::interprocess::string is;
//  std::vector<int> v;

    Record() {}
    Record(int _k) { Init(_k); }

    void Init(int _k = 0) {
        c = _k + 65;
        i = _k;
//      f = _k + _k/100.0;
//      d = _k + _k/1000.0;
//      is = "interprocess string";
//      for(int j = 0; j < _k; ++j) v.push_back(j);
    }
};

int main(int argc, char *argv[])
{
   using namespace boost::interprocess;
   using std::cerr;
   using std::endl;

   int nObjects = 0;
   size_t blockSize = 1024;

   static std::string sharedObjName("MySharedMemory");      // why static?

   const int writer = 1, reader = 2, error = -1;
   int operation = error;

   if(argc >= 2) {
      if(argv[1][0] == 'w') operation = writer;
      if(argv[1][0] == 'r') operation = reader;
   }
   if(argc == 1) operation = writer;

   if(operation == writer)      // Writer process
   {
      cerr << "Number of objects to write = ";
      std::cin >> nObjects;

      // Remove shared memory on construction and destruction
      struct shm_remove {
         shm_remove() { shared_memory_object::remove(sharedObjName.c_str()); }
         ~shm_remove(){ shared_memory_object::remove(sharedObjName.c_str()); }
      } remover;

      shared_memory_object shm(create_only, sharedObjName.c_str(), read_write);

      shm.truncate(blockSize);

      mapped_region region(shm, read_write);

      offset_t shmSize;
      shm.get_size(shmSize);

      // Produce and write data
      Record *pData0 = static_cast<Record*>(region.get_address());
      Record *pData  = pData0;

      for(int i = 0; i < nObjects; ++i) {
         if(pData0 + blockSize - pData < signed(sizeof(Record))) {
            cerr << "Error: memory block full!" << endl;
            break;
         }
         pData->Init(i);
         pData += sizeof(Record);
      }

      //Launch child process
      pid_t pId = fork();

      if(pId == 0)
      {
         std::string s(argv[0]); s += " r";

         if(std::system(s.c_str()) != 0) {
            cerr << "Error launching reader process." << endl;
            exit(1);
         }
         exit(0);
      }
      else if(pId > 0)
      {
         sleep(2);
         cerr << "Writer has finished!" << endl;
      }
      else  // pId < 0
         exit(-1);
   }
   else if(operation == reader)         // Reader process
   {
      shared_memory_object shm (open_only, sharedObjName.c_str(), read_only);

      mapped_region region(shm, read_only);

      Record *pData = static_cast<Record*>(region.get_address());

      for(int i = 0; i < nObjects; ++i)  {
         // Print pData...
         pData += sizeof(Record);
      }
   }
   else
      exit(1);

   return 0;
}

感谢您的任何提示!

MacOS X 10.6.2 - gcc 4.2 - 提升1.41.0

1 个答案:

答案 0 :(得分:3)

pData += sizeof(Record);

那条线就是问题所在。指针算术意味着更改以底层指针类型的“单位”为单位,在本例中为Record。因此,如果要增加到下一条记录,则应该执行pData ++,而不是pData + = sizeof(Record),这将使指针增加 64 字节(假设sizeof(Record)为8 - 8 * 8 = 64)。

在尺寸检查中你有一个类似的指针算术错误:

pData0 + blockSize - pData < signed(sizeof(Record))

您可能需要以下内容:

blockSize/sizeof(Record)-(pData-pData0) <= 0