两个 C++在 Linux 上共享只读内存区域的应用程序

发布于 2024-08-17 19:54:31 字数 270 浏览 5 评论 0原文

我有两个进程 P1 和 P2。

我有一个名为“R”的大型只读资源,我希望 P1 和 P2 都可以访问它。

R 不仅仅是一个“平面”字节组;它是一堆互相指向的 C++ 对象。

我希望 P1 和 P2 只共享 R 的一份副本——以某种方式让 P1 将 R 加载到内存中的某个区域(在 P1 和 P2 中映射到同一地址),然后 P1 和 P2 都可以访问 R 中的对象,如下所示C++ 对象(没有竞争条件,因为所有对象都是只读的)。

有人熟悉如何做到这一点/陷阱吗?

I have two processes P1 and P2.

I have this large read-only resource, called "R" that I want both P1 and P2 to have access to.

R is not just a "flat" group of bytes; it's a bunch of C++ objects that point to each other.

I would prefer that P1 and P2 only share one copy of R -- somehow have P1 load R into a region in memory (that's mmaped in P1 and P2 at the same address), then P1 and P2 can both access the objects in R as C++ objects (no race conditions since all is read only).

Anyone familiar how to do this / gotchas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

記憶穿過時間隧道 2024-08-24 19:54:31

实际上,已经问过类似的问题并之前解决了

最佳答案可能对您有用:
使用 Boost 进程间库。虽然您仍然无法使用具有虚拟函数的对象(共享内存外部令人讨厌的 vtable 指针问题),但它们确实有工具可以让您使用指向共享内存内其他对象的智能指针,以及在共享内存内分配的自定义分配器来创建std::vector 和 std::map 对象。

Actually something similar has been asked and solved before:

And the best answer will probably work for you:
Use boost interprocess library. While you still can't use objects with virtual functions (nasty vtable pointer outside shared memory issue), they do have tools to let you use smart pointers to other objects inside the shared memory, and custom allocators that allocate inside the shared memory for creating std::vector and std::map objects.

魔法唧唧 2024-08-24 19:54:31

R 中的对象如何相互指向?如果它相对于当前对象的位置,您可以使用共享内存。无法保证此共享内存会加载到进程 P1 和 P2 内的同一地址位置。这就是为什么相对只能起作用。
既然你说过,他们都不会尝试修改它并只是从中读取,我想你不需要使用信号量/互斥体来保护它。

How are the objects inside R pointing to each other? If its' relative to the current objects' position, You could use shared memory. There is no gurantee that this shared memory is loaded inside both processes P1 and P2 at the same address location. Thats why relative only works.
And since you said, none of them will try to modify it and just read from it, I guess you need not protect it using either a semaphore/mutex.

梦在夏天 2024-08-24 19:54:31

如果我理解手册页,似乎 Linux mmap 函数 允许您映射如果您提供听起来有点可怕的 MAP_FIXED 选项,则可以将文件、共享内存或其他可映射的内容放入进程中的特定虚拟地址处。搜索 MAP_FIXED 的手册页,您会发现许多警告(可能不受支持,可能使 malloc 不再工作,等等)。但如果你能让它工作,共享对象就可以有彼此的指针。

If I understand the man page, it seems that the Linux mmap function allows you to map a file, shared memory, or other mappable thing into your process at a specific virtual address if you provide the somewhat scary sounding MAP_FIXED option. Search the man page for MAP_FIXED and you'll find many warnings (might not be supported, might make malloc not work anymore, etc.). But if you could get it to work, the shared objects could have pointers to each other.

我还不会笑 2024-08-24 19:54:31

就这么简单:

#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>

// ...

int fd = open("memfile.txt",O_RDWR);
struct stat info;
fstat(fd, &info);
void * page = mmap(0, info.st_size, PROT_READ , MAP_SHARED, fd, 0);

现在您可以使用存储在 memfile.txt 中的任何内容作为结构,并且它将在进程之间共享。

注意 正如其他人所说,您不能在该内存块内的对象之间使用指针。

这适用于 OS X 10.4,但也适用于任何 BSD 兼容系统。

Its as easy as this:

#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>

// ...

int fd = open("memfile.txt",O_RDWR);
struct stat info;
fstat(fd, &info);
void * page = mmap(0, info.st_size, PROT_READ , MAP_SHARED, fd, 0);

Now you can use whatever you've stored in memfile.txt as a structure and it will be shared between processes.

NOTE as others have said you can't use pointers between objects inside this chunk of memory.

This works for me on OS X 10.4, but should work on any BSD compliant system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文