如何在共享内存中获取共享对象 [英] How to get Shared Object in Shared Memory

查看:689
本文介绍了如何在共享内存中获取共享对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的应用程序取决于外部第三方提供的配置(包括自定义驱动/决策功能),可加载为.so文件。

Our app depends on an external, 3rd party-supplied configuration (including custom driving/decision making functions) loadable as .so file.

外部CGI模块使用一块共享内存,其中几乎所有的易失性状态都被保存,以便外部模块可以读取它并在适当的地方修改它。

Independently, it cooperates with external CGI modules using a chunk of shared memory, where almost all of its volatile state is kept, so that the external modules can read it and modify it where applicable.

问题是CGI模块需要来自.so的大量永久配置数据,并且主应用在两个存储器区域之间执行大量完全不必要的复制以使得数据可用。想法是使整个共享对象加载到共享内存,并使其直接可用于CGI。问题是:如何?

The problem is the CGI modules require a lot of the permanent config data from the .so as well, and the main app performs a whole lot of entirely unnecessary copying between the two memory areas to make the data available. The idea is to make the whole Shared Object to load into Shared Memory, and make it directly available to the CGI. The problem is: how?


  • dlopen和dlsym不提供用于指定加载SO文件的位置。

  • 我们尝试了shmat()。它似乎只工作,直到一些外部CGI实际上尝试访问共享内存。然后指向的区域显示为私有的,就好像它从未共享。也许我们做错了什么?

  • 在每个需要它的脚本中加载.so是没有问题的。结构的绝对大小,与呼叫的频率相关(一些脚本每秒被调用一次来生成实时更新),这是一个嵌入式应用程序,使它没有去。


  • dlopen and dlsym don't provide any facilities for assigning where to load the SO file.
  • we tried shmat(). It seems to work only until some external CGI actually tries to access the shared memory. Then the area pointed to appears just as private as if it was never shared. Maybe we're doing something wrong?
  • loading the .so in each script that needs it is out of question. The sheer size of the structure, connected with frequency of calls (some of the scripts are called once a second to generate live updates), and this being an embedded app make it no-go.
  • simply memcpy()'ing the .so into shm is not good either - some structures and all functions are interconnected through pointers.

推荐答案

我想最简单的选择是使用内存映射文件,Neil已经提出了。如果这个选项没有填充好,替代是可以定义专用分配器。这是一篇很好的文章:在共享内存中创建STL容器

I suppose the easiest option would be to use memory mapped file, what Neil has proposed already. If this option does not fill well, alternative is to could be to define dedicated allocator. Here is a good paper about it: Creating STL Containers in Shared Memory

还有优秀的IonGaztañaga的 Boost 。带有 shared_memory_object 和相关功能的.Interprocess 库。 Ion已经为C ++标准化委员会提出了未来TR的解决方案:内存映射文件和共享内存对于C ++
可能表明值得解决的问题。

There is also excellent Ion Gaztañaga's Boost.Interprocess library with shared_memory_object and related features. Ion has proposed the solution to the C++ standardization committee for future TR: Memory Mapped Files And Shared Memory For C++ what may indicate it's worth solution to consider.

这篇关于如何在共享内存中获取共享对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆