如何使用地图使用共享内存 [英] How to use shared memory using map

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

问题描述



我正在尝试实现一个程序,以使用共享内存在进程之间共享存储在映射中的信息.

当我将共享内存附加到映射时,它正在转储核心,但是我能够将结构成功附加到共享内存.

请问原因可能是什么?


问候
Centinnel

Hi ,

I am trying to implement a program to share information stored in a map between process using shared memory .

When I am attaching the shared memory to the map , it is dumping core but I am able to attach the structure to shared memory successfully .

Could you please tell what could be the reason for this ?


Regards
Centinnel

推荐答案

请看以下文章:
http: //www-10.lotus.com/ldd/dominowiki.nsf/dx/How-to-read-memory-dump-detects-leaks-Pool-sizes-Handle-counts [ http://www.cs.washington.edu/homes/luisceze/publications/asplos004- devietti.pdf [ ^ ]
http://www.eng.auburn.edu/files/acad_depts/csse/csse_technical_reports/CSSE91-04.pdf [ ^ ]

希望对您有用
Let''s see following articles:
http://www-10.lotus.com/ldd/dominowiki.nsf/dx/How-to-read-memory-dump-detects-leaks-Pool-sizes-Handle-counts[^]
http://www.cs.washington.edu/homes/luisceze/publications/asplos004-devietti.pdf[^]
http://www.eng.auburn.edu/files/acad_depts/csse/csse_technical_reports/CSSE91-04.pdf[^]

I hope it will be useful for you


如果将映射放在共享内存中,并不意味着映射将使用共享内存进行分配.因此,在进程之间,您将只共享一个指向非共享内存中某些内存的指针值. Map使用新功能/删除功能来构建自身.
If you put the map in the shared memory, it does not mean that map will use the shared memory for allocations. So, between processes you will have shared only value of a pointer which points to some memory inside the nonshared memory. Map uses new/delete functions to build itself.


完全可以做到,但解决方案很麻烦.方法如下:首先,您必须分配一块共享内存.然后,您应该创建一个std :: map并以"placement new"(新放置)来表示此共享内存的开头,并保留共享内存的另一(更大)部分,以用作其他分配的池.此std :: map应该具有自定义分配器(std :: map的第三个模板参数是分配器类).此自定义分配器应该是使用共享内存的池部分进行分配的分配器(我在某处有这样的分配器...).如果您的映射包含进行某种分配的非POD类型(作为键或值),那么您也应该为这些类编写新的运算符,以便从共享内存池中进行分配,但是在此共享中使用POD的麻烦要小得多地图.

如果您想在两个过程中都修改地图,则可能很难实现这一点(但可能的话).如果只有其中之一编写地图,而其他人只是阅读地图,则会更容易.无论哪种方式,您都将需要某种进程间同步.

忘记提及我会为此目的编写和使用一个自定义模板,该模板可以存储固定数量的键值对并且具有固定的大小.在内部,您可以使用hash + item表或较短的数组来加快索引编制的速度,但是如果您对模板的使用不多,则可以忽略这一点...
It is completely possible to do it but the solution is messy. Here is how to do it: First you have to allocate a piece of shared memory. Then you should create an std::map lets say the the beginning of this shared memory with "placement new" and reserve the other (bigger) part of the shared memory to be used as a pool for other allocations. This std::map should havea custom allocator (the 3rd template parameter of std::map is the allocator class). This custom allocator should be one that uses the pool part of the shared memory to allocate (I have such an allocator somewhere...). If your map contains non-POD types (either as key or value) that do some kind of allocations, then you should write new operators for those classes too to allocate from the shared memory pool but its much less bothersome to use PODs in this shared map.

it can be harder to implement this if you want to modify the map in both processes (but its possible). Its easier if only one of them writes the map, the other just reads it. Either way, you will need some kind of inter-process synchronization too.

forgot to mention that I would write and use a custom template for this purpose, one that can store a fix number of key value pairs and has a fixed size. Inside you could use a hash+item table or a shorted array to speed up indexing but forgot about this if you havent worked much with templates...


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

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