Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

mmap

sharing

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?

like image 766
anon Avatar asked Jan 18 '10 05:01

anon


People also ask

How to share memory between processes in C?

Steps : Use ftok to convert a pathname and a project identifier to a System V IPC key. Use shmget which allocates a shared memory segment. Use shmat to attache the shared memory segment identified by shmid to the address space of the calling process.

Can two processes share memory?

Context switching between processes is more expensive. Processes don't share memory with other processes. Threads share memory with other threads of the same process.

What is shared memory in Linux?

Shared memory is a feature supported by UNIX System V, including Linux, SunOS and Solaris. One process must explicitly ask for an area, using a key, to be shared by other processes. This process will be called the server. All other processes, the clients, that know the shared area can access it.

How is shared memory region identified?

A thread gets a shared memory identifier by calling the shmget() or shmget64() functions. Depending on the key and shmflg parameters passed in, either a new shared memory segment is created or an existing shared memory segment is accessed. The size of the shared memory segment is specified by the size parameter.


3 Answers

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.

like image 192
Michael Anderson Avatar answered Sep 19 '22 18:09

Michael Anderson


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.

like image 36
vpram86 Avatar answered Sep 18 '22 18:09

vpram86


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.

like image 1
Wayne Conrad Avatar answered Sep 18 '22 18:09

Wayne Conrad