C++ IPC question

I put this under game programming too because game programmers might know how to do this.

I need to write a class that creates an object, lets say the type of the object is MACHINE.

This class needs to take this instance of the object and pass it to another process somehow so both processes can access the same instance of MACHINE.

How do I do this?

I know some methods of IPC like using sockets, shared memory, message passing, but I don't think this will help me share an object.

Thanks for any help experts! I will be keeping an eye on this thread.

-Jeff
LVL 6
jeffiepooAsked:
Who is Participating?
 
phoffricConnect With a Mentor Commented:
If multiple platforms, you can use CORBA
Search on "shared objects" in this link:
    http://java.sun.com/developer/onlineTraining/corba/corba.html

On a single platform in a multithreaded application, you can share the object directly amongst the threads.

I have heard that boost can facilitate this goal.
      http://www.quantnet.com/cplusplus-multithreading-boost/ 
0
 
jeffiepooAuthor Commented:
It looks like CORBA is for java. I'm writing this in C++, it also seems kind of involved.

These are separate processes, not separate threads so I can't just have them sharing data.

I am having a single program launch a completely separate program using popen();

-Jeff

0
 
AriMcCommented:
CORBA isn't really language or even OS specific:

   http://en.wikipedia.org/wiki/Common_Object_Request_Broker_Architecture

Anyways, if you told us what operating system you are writing for and if the processes are running on the same computer, we might be able to point to some other alternatives too.

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
phoffricConnect With a Mentor Commented:
>> It looks like CORBA is for java. I'm writing this in C++
No, where supported, it is platform and language independent. (But for each language, you need the proper libraries.) Here's another link for C++:
    http://www.yolinux.com/TUTORIALS/CORBA.html

>> CORBA isn't really language or even OS specific:
True, and that is the beauty of CORBA - it allows different platforms with developers writing in different languages to use a common interface to communicate. Java has their own (free) competing ORB (or at least, they used to).

>> it also seems kind of involved.
Yes, to be able to handle the multi-platform/multi-language communication, there is some learning curve.
0
 
satsumoSoftware DeveloperCommented:
That sounds a bit like a thread-safe COM object.  You don't actually need to involve COM directly, just use a COM style interface and make the functions thread safe (maybe use a mutex).  I guess the object itself would need to be located in shared memory.  I have no idea if that's relevant in this context.
0
 
phoffricCommented:
If only dealing with a single platform, then CORBA is overkill.
0
 
phoffricCommented:
Is the created object a POD-struct? If so, you can place this in shared memory and have the processes get a handle into the shared memory.
0
 
ranidesCommented:
If you work on Windows, just use that Named Shared Memory, and implement simple synchronization using Mutexes...

http://msdn.microsoft.com/en-us/library/ms686927%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/aa366551%28v=vs.85%29.aspx

Maybe explain why are you thinking that: "shared memory [...] doesn't help you share an object. This is exactly that method which you should use to share information between two local processes.
0
 
phoffricCommented:
If you really need a generalized object sharing mechanism, then you will need to store the shared objects in a separate monitor/object-creation process that provides the api for the object's state and functionality. How you wish to synchronize requests is application dependent. (CORBA provides this for you, but since it is ultra-generalized and requires a significant learning curve, you are likely better off to customize your own middle-ware process yourself.)
0
 
phoffricConnect With a Mentor Commented:
I looked a little more into boost (some of which will be incorporated into the upcoming C++ release). I found this link which you may be able to use (sorry, as I said, I haven't used boost, yet):
    Creating named shared memory objects

"You want to create objects in a shared memory segment, giving a string name to them so that any other process can find, use and delete them from the segment when the objects are not needed anymore."

Some comments from the code example look promising:
      //Create an object of MyType initialized to {0.0, 0}

      //Create an array of 3 elements of MyType initializing each one
      //to a different value {0.0, 0}, {1.0, 1}, {2.0, 2}...
0
 
Infinity08Commented:
>> I know some methods of IPC like using sockets, shared memory, message passing, but I don't think this will help me share an object.

The question I would ask is : do you actually need to share the object ? If you don't have to, there's no need for making things complicated.

Can't you just pass messages between the processes to signal changes etc. ?
0
 
sarabandeCommented:
to add to above comments:

a shared object between two processes normally would require a server which manages the object. the clients are the two gamers which wouldn't communicate directly but always asking the server (process). that is called client-server and you exactly would need those things like sockets, message passing you mentioned. the advantage of a client server solution is that the clients may run on different computers and the server on a third computer. you also easily could add more clients or let the server manage more than one game a time.

the disadvantage infinity08 has told. it is more effort to program than a communication between two players only. you wouldn't have a shared object in this case but each player had its own copy of the object and would update it when it receives messages from the other player.

a third approach ranides has mentioned. if the players are on the same computer you could use shared memory to access the same data structure from both players. unlike to the client server solution it requires synchronisation techniques from both players, so it might be a little more difficult than message processing.

Sara

0
 
jeffiepooAuthor Commented:
AriMC: Running on Linux, and they ARE running on the same computer.

phoriffic: No it is not a POD struct, it is a class that handles a struct (linked-list) with all of the correct management methods that are needed for a linked list, this also goes to everybody:

Everybody: I can't use what ranides mentioned because I would need to share the object not the struct, because I need all of the link list's managment methods.

CORBA is an excellent suggestion, and I will look into that in the future if I have any real-world programming challenges. Thank you phoffric.

I looked into the boost library example as well, and that is the only thing that matches exactly what I was looking for in this post.  It pops an entire object, data structures, functions and all into a shared memory segment which is what I was looking for.

In the meantime I implemented it a different way, I just used shared memory segments that pass strings back and forth. The process has to decrypt the message and use it's own methods on it in a specific way which was a little more work. I started this post because I didn't want to have to write another process to manage the linked list...

So I guess I did it Infinity08's way, but the thing is, it made it MORE complicated than sharing the object would have, even though there would have been a little of a learning curve.

Anyway thanks everyone, especially phoffric for the great post. I'll look more into boost in the future.

-Jeff
0
 
Infinity08Commented:
>> So I guess I did it Infinity08's way, but the thing is, it made it MORE complicated than sharing the object would have

That depends on how you do it :) If you implemented this from the ground up, then it will of course have taken more work than a ready-to-use solution.

It also depends on how much communication there has to happen. There's a point where sharing the entire object becomes more convenient indeed, but then you can also ask the question if you really need that much communication. There's usually a way of simplifying things to the basics.
0
 
sarabandeCommented:
if you don't want a server process both clients have to manage their data separately anyway but you could share the data of that object via shared memory.

note, the boost sample doesn't have synchronisation mechanisms (if i didn't oversee them). so you would need to add exclusive access to the shared data yourself. if you have your own linked list it is not difficult to create the nodes and data in shared memory and make each access exclusively by using a named mutex.

Sara
0
 
jeffiepooAuthor Commented:
Yes, synchronisation mechanisms would have to be implemented as well, and have been.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.