sharing pointers between process using shared memory.

I have process A which has some structure type data_t and a pointer to that struct data_t. I want process B to also have access to the instance of data_t created in process A. What is the best way to do this? I have tried using shared memory, but does it make sense to share a pointer between too processes when they have there own address space? I know I asked similar question before and I was to told to use opaque types. can anyone post a sample code for opaque type approach to this?

Also when process A links shared library called foo.so and process B also links foo.so, do process A and B all get a seperate copy of variables declared in foo.so?

Ultimately what I am trying to figure out is, I have a struct type in foo.so which process A initializes after linking, but when process B links to foo.so, process B does not see the process A initialized struct values.

Thanks for your input.

It’s impossible to share pointers even via a shared library.

You must store all possible instance of data_t in share memory and also share an index or offset to which of the data_t must be referenced. If there is only once instance of data_t then it’s simple just put that structure into shared memory.

Each process will get a different pointer (via mmap) but these two pointers will point to the same physical memory.

Then is it possible for a shared library to have a one static instance of data_t and when process A and process B links to this shared library it uses the library functions to read and write to data_t instance ? This way, will there be only one instance of data_t even when link to more than one processes?

In detail, I have a DOM tree that is built from xml files and I have functions to read and write to this DOM tree. I have made these functions shared library so that any process can link it and be able to read and write to this one global DOM tree. Is having one instance of DOM tree possible in shared library even though many process will link to this shared library?

I guess if this is not possible, then I have to find a way to build the DOM on shared memory.

Not that I know off, shared library is not an IPC method. Don’t expect a shared library to offer more feature then a statically link library :wink:

It’s not possible, only solution I can think of is that your tree, instead of containing pointers, must contain or be treated as offsets.

By the way the possibility that the tree be read and write at the same time by more then one process/thread requires careful design. And multi-core are coming fast so think SMP :wink:

You could build a resource manager that would create a virtual directory tree that maps the DOM tree into the file system. Or build a generic driver that handles DOM, all reading and writing would be message based.

I like the idea of resource manager with read and write msg. I guess it all comes down to building resource manager again ;)

thanks for your input.

Doesn’t have to be a resource manager (which take more time to design and code IMHO) a custom driver with a match API in the form of a shared lib could also do the job.