2

我对 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

4

1 回答 1

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
于 2010-01-08T00:38:46.717 回答