English 中文(简体)
Two C++ apps sharing a read-only region of memory on Linux
原标题:

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?

最佳回答

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.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签