本文介绍了提高去除managed_shared_memory当过程连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个进程,进程1创建一个升压managed_shared_memory段和过程2打开该段。然后过程1被重新启动和过程1的开始具有以下,

 结构vshm_remove
{
    vshm_remove()
    {
        提高::进程间:: shared_memory_object ::删除(VMySharedMemory);
    }
    〜vshm_remove()
    {
        提高::进程间:: shared_memory_object ::删除(VMySharedMemory);
    }
} vremover;

据我所知,工艺1开始或结束remove方法时,将调用我的共享内存,但不应该只删除它,如果处理2没有重视它?我使用下面的连接到共享内存中的进程2,

 的boost ::进程间:: managed_shared_memory * vfsegment;
vfsegment =新的boost ::进程间:: managed_shared_memory(升压::进程间:: open_only,VMySharedMemory);

我注意到,该共享存储​​器是无论过程2的除去被连接。


解决方案

我不相信有这 shared_memory_object ::删除将无法在文档中提及的任何如果一个进程连接。

请参阅本节,以供参考:共享的卸载内存。特别是:

This means that a call to shared_memory_object::remove("foo") will attempt to remove shared memory named "foo" no matter what.

The implementation of that function (source here) reflects that behavior:

inline bool shared_memory_object::remove(const char *filename)
{
   try{
      //Make sure a temporary path is created for shared memory
      std::string shmfile;
      ipcdetail::tmp_filename(filename, shmfile);
      return ipcdetail::delete_file(shmfile.c_str());
   }
   catch(...){
      return false;
   }
}

In my experience with released production code, I've had success not calling shared_memory_object::remove until I no longer need access to the shared memory.

I wrote a very simple example main program that you might find helpful. It will attach to, create, or remove shared memory depending on how you run it. After compiling, try the following steps:

  1. Run with c to create the shared memory (1.0K by default) and insert dummy data
  2. Run with o to open ("attach to") the shared memory and read dummy data (reading will happen in a loop every 10 seconds by default)
  3. In a separate session, run with r to remove the shared memory
  4. Run again with o to try to open. Notice that this will (almost certainly) fail because the shared memory was (again, almost certainly) removed during the previous step
  5. Feel free to kill the process from the second step

As to why step 2 above continues to be able to access the data after a call to shared_memory_object::remove, please see Constructing Managed Shared Memory. Specifically:

Mostly likely, because the shared memory object is mapped into the process' address space, the shared memory file itself is no longer directly needed.

I realize that this is a rather contrived example, but I thought something more concrete might be helpful.

#include <cctype>   // tolower()
#include <iostream>
#include <string>
#include <unistd.h> // sleep()
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>

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

  if (argc == 1) {
    cout << "usage: " << argv[0] << " <command>\n  'c'   create\n  'r'   remove\n  'a'   attach" << endl;
    return 0;
  }

  const char * shm_name = "shared_memory_segment";
  const char * data_name = "the_answer_to_everything";

  switch (tolower(argv[1][0])) {
    case 'c':
        if (shared_memory_object::remove(shm_name)) { cout << "removed: " << shm_name << endl; }
        managed_shared_memory(create_only, shm_name, 1024).construct<int>(data_name)(42);
        cout << "created: " << shm_name << "\nadded int \"" << data_name << "\": " << 42 << endl;
        break;
    case 'r':
      cout << (shared_memory_object::remove(shm_name) ? "removed: " : "failed to remove: " ) << shm_name << endl;
      break;
    case 'a':
      {
        managed_shared_memory segment(open_only, shm_name);
        while (true) { 
          std::pair<int *, std::size_t> data = segment.find<int>( data_name );
          if (!data.first || data.second == 0) {
            cerr << "Allocation " << data_name << " either not found or empty" << endl;
            break;
          }
          cout << "opened: " << shm_name << " (" << segment.get_segment_manager()->get_size()
               << " bytes)\nretrieved int \"" << data_name << "\": " << *data.first << endl;
          sleep(10);
        }
      }
      break;
    default:
      cerr << "unknown command" << endl;
      break;
  }
  return 0;
}

这篇关于提高去除managed_shared_memory当过程连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 19:28