简体   繁体   English

如何创建可以随时间扩展的共享内存区域?

[英]How can I create a shared memory region which can expand over time?

I need to work with an old C++ project where each process loads the same data into the memory. 我需要使用旧的C ++项目,其中每个进程将相同的数据加载到内存中。 This is read-only and each process has the exact same copy of the data. 这是只读的,每个进程都具有完全相同的数据副本。 Of late the service has started thrashing the system that it runs on. 最近,该服务开始颠覆它运行的系统。

I was evaluating ways in which I could make changes to this and I think boost interprocess libraries are a good fit for my use case. 我正在评估我可以对此进行更改的方法,我认为boost interprocess库非常适合我的用例。 Another thing that I considered was the use of an external data store but due to the latencies I'm not too inclined to use that. 我考虑的另一件事是使用外部数据存储,但由于延迟,我不太倾向于使用它。

I had a look at the boost apis and they seem to require us to specify the size of the shared memory region. 我看了一下boost apis,他们似乎要求我们指定共享内存区域的大小。 In my case this won't be constant as the volume of data that needs to be loaded in grows over time. 在我的情况下,这将不会是恒定的,因为需要加载的数据量会随着时间的推移而增长。 Is this shared memory resizable? 这个共享内存可以调整大小吗?

Also feel free to suggest alternative suggestions to the problem or your experiences with boost interprocess in similar situations. 也可以随意提出问题的替代建议或您在类似情况下使用boost interprocess的经验。

To resize shared memory, you can use shmctl , where cmd=SHM_SIZE . 要调整共享内存的大小,可以使用shmctl ,其中cmd=SHM_SIZE

You must take care when doing this. 这样做时你必须小心。 If you reduce the size, and other processes access memory beyond it's size, you will get SIGBUS signal. 如果减小尺寸,并且其他进程访问超出其大小的内存,您将获得SIGBUS信号。

Another problem is how to inform other processes that a shared memory was resized. 另一个问题是如何通知其他进程已调整共享内存的大小。 I guess you can use a message queue, or other IPC mechanism. 我想你可以使用消息队列或其他IPC机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM