tags:

views:

215

answers:

4

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?

+1  A: 

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.

Aviator
The addressing could be simplified if the layout is known in advance, then a simple struct that describes it would suffice. If not, then an address-table to the objects might help - create once per process and hand around as needed.
Georg Fritzsche
A: 

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.

Michael Anderson
+1  A: 

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.

Wayne Conrad
+4  A: 

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.

Michael Anderson