sharing objects

Now i am able to access the data members of the class through the shared memory in another process.But i am not able to access member functions in another process.It’s giving undefined reference to function.i tried to use virtual functions then it’s giving undefined reference to virtual table.Is it possible to access the member functions of one class in another through shared mem or not.thanks in advance.

If you understand how memory is allocated and protected you would realize that it is quite a stretch to think that this might work. Code is in special memory that is executable and read only. When you instantiate a class, the code is already there having been established at load time. The data however is created dynamically, so in principle it can be accessed through shared memory. To access a member function, you would need to shared (at least part of) the code segment. In addition, if you mount the first processes code segment in your memory space, the code will have to be relocatable since there is no way to force it to align on the addresses it is originally loaded at.

QNX has a wonderful mechanism to get around all these problems cleanly. It’s called message passing. The only downside is slightly slower access to memory which rarely is a problem.

Thanks
Message passing is slower compare to shared mem it’s not a problem for me.but i want to access the member functions of class in another process.I have used message passing to pass the struct and data successfully.But I am confused here how to use that in this case(passing obj and accesing member fun).And as you told we need to locate code segment in shared mem.how to locate that one in shared mem.Please let me know.

Definition of “a stretch”: not likely.

Why don’t you map a shared memory object to some pointers member of your class?. In the constructor of the class (or other method, for instance: connect_shmem()). And then you have access to the shmem for any instance of your class.

So if in processA you create (if it not exist) a shmem in the constructor of the class, then in processB, you create a new instance of the same class (and with the constructor mapping the (existing) shmem objetcs to those pointers, and you have a easy access to shmem with the methods of the class for any program which have an instance of that “connected to shmem” class.

Maybe you have to focouse on synchronization, depending of the implementation.

Do you actually need to place the object (class) data and code in shmem, and why?

Juan Manuel

This is what shared object ( .so ) are for. Trying to access a member function, or any function for that matter, of another process is to be avoided, it’s just not the right thing to do.

It seems to me your lack experience is sending you on the wrong track. Maybe you can give us a bigger picture of what you need to achieve.

This is what shared object ( .so ) are for. Trying to access a member function, or any function for that matter, of another process is to be avoided, it’s just not the right thing to do.

The fact that you even tried to use virtual fonctions to do that, tells me you lack basic understand of the subject at hand (absolutely no offense intented). It seems to me your lack experience is sending you on the wrong track. Maybe you can give us a bigger picture of what you need to achieve.

This works fine. You need to understand the “placement new” operator in C++. Of course, you do need to insure that the different processes that access the shared class have the same virtual address mapping for these objects. QNX 6.4 does have a special facility that allows you to control this.

If you aren’t familiar with “placement new”, or how to control the virtual address allocations within a process, then you have some reading to do. If you do understand these, then it is trivial to instantiate any class into shared memory (you don’t even have to modify a single line of code in a class you want to instantiate, since it is the placement new operator that determines where the class is instantiated).

All you need to do is:

  1. Create a shared memory area with identical virtual mappings in each process.
  2. instantiate any class you want in shared memory with placement new

e.g.

void* shared_mem_ptr=mmap(…); // virtually controlled mmap of shared object
Class foo* foop=new(shared_mem_ptr) foo();

// foop is now in the shared memory at the address held in shared_mem_ptr, and any process that can access this memory, can invoke methods on foop

That said; I agree with the other posters, that messaging is far more elegant in general.

You should come around more often ;-) What is this special facility that QNX6.4 provide for helping implement this. The way I see it it would require some special step to ensure the virtual addresses are the same.

Not sure on the details myself. I think it is some syspage entry that defines a “preferred” virtual address for shared objects. Not sure if it is documented (other than in the source :slight_smile: You might need to make sure that your space for shared objects is allocated before any other shared objects as well. In any case, once the proper incantation is known, it should be pretty straightforward after that.

Hey, I did say, that if one didn’t know how to do this, that there would be some reading involved (I just didn’t mention that this would include me as well :-)))

Thinking about it some more. I don’t think it would work if a function template is used in a class, if the class’s objects perform some dynamic allocation like using std::string

Of course you do need to insure that all objects you wish to share, allocate via placement new. So while there is no inherent issue with it, you are right that you probably wouldn’t want to allocate member objects as pointers (not because you can’t, but because managing the placement new, would quickly become tedious). As long as all members are in-line members, you only need a single placement new when instantiating the class.