开发者

Two C++ apps sharing a read-only region of memory on Linux

开发者 https://www.devze.com 2022-12-17 10:53 出处:网络
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.

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 fam开发者_JAVA百科iliar how to do this / gotchas?


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号