Skip to contentSkip to conversation

Memory sharing question

DISCUSSION Stephen Dolan 2 participants 2 messages Feb 24, 2007

lore
Share & tools
lore
#0 lore raw

Hi.

I'd like to know if it is possible to get two processes to share a memory segment at the same address, e.g. both mmap() the same file and have it return the same address in both.

This could be done by mmap()ing it in one of them, communicating the address to the other (via a socket or whatever), which then mmap()s it with the address as the "start" parameter and MAP_FIXED. However, that tends to fail, and MAP_FIXED can have annoying side-effects (killing off other mappings).

Is it possible to reliably make the same address in two different processes point to the same bit of physical memory?

The point of all this is that I want to pass a large, complex (full of pointers) data structure to a different process, and I don't want the overhead of serialising it down a socket and then parsing it at the other end (the data structure's pretty big, and the other process won't need it all). Is this possible?

Thanks,
Stephen Dolan

#1 lore raw
Quoted 3 lines

with the address as the "start" parameter and MAP_FIXED. However, that tends to fail, and MAP_FIXED can have annoying side-effects (killing off other mappings).

MAP_FIXED requires you know in advance a good place to put the memory, which isn't too hard with some planning but does get fairly platform specific and requires good knowledge.

Quoted 5 lines

The point of all this is that I want to pass a large, complex (full of pointers) data structure to a different process, and I don't want the overhead of serialising it down a socket and then parsing it at the other end (the data structure's pretty big, and the other process won't need it all). Is this possible?

You can also just use relative pointers, which is actually pretty fast on most modern processors