0

I am looking for a way to send class object from one process to another.
For example assuming I have a class

class my_class
{
public: 
     my_class_variable; 
     my_class() { my_class_variable = 0; }
};

My main forks a child that child works on an object of class "my_class"

int main()
{
      my_class my_class_obj = new my_class; 
      if (fork() == 0)
      {
           my_class_obj.my_class_variable = 1;
           exit(0); 
      }
      else 
      {
           wait(NULL); 
           my_class new_obj = new my_class; 
           //how do I copy the "my_class_obj" into new_obj
       }
}

This is a rather simple example for the sake of just this example I could just use a pipe and send the value '1' through that pipe. But I am stuck in a scenario where I have class with more than 7 variables that a child process updates.
Now I have noticed that when I exit the child process the instance of class on which child process was working is destroyed (regardless of the fact that the object instance was initialized in the parent and not inside the child). So now I cannot retrieve the values that were updated by the child process on that class object. Accessing member variables of that class outside child process only gives default values not the values that were set by child process.
I have thought of using pipes but how would I implement that and send a class object inside a pipe, because currently I only know how to send integer and string variables through pipes.

Please help how can I transfer class objects instances from one process to another.

  • 1
    There are no secret tricks here, you've pretty much described the most common way to do this -- pipes. One could also use other tricks like shared memory, but using low-level oeprating system resources like this properly with C++ is tricky, and requires thorough understanding of placement new, and other advanced C++ topics. Or, one can use threads instead of different processes, and everything happens in one process. – Sam Varshavchik Apr 24 '22 at 19:01
  • Some common formats for sending data from one process to another is XML, or JSON. – Eljay Apr 24 '22 at 19:01
  • 4
    You can't just "send" it, much like I can't send a thought from my brain to yours. You have to _serialize_ it (and all its baggage, if any), which means encoding it into a well defined format independent of address space - often a string format. Then transmit that encoded data to the other process, then _deserialize_ it on the receiving side and place a new (equivalent) object in memory of the receiving process. POD (plain-old-data) types can be stored in shared memory and shared that way. – Wyck Apr 24 '22 at 19:03
  • Fundamentally speaking you're asking how to do achieve interprocess communication. There are various ways to do that. If you want something very specific to this scenario probably shared memory would work. Here: https://man7.org/linux/man-pages/man2/shmget.2.html Or maybe go through these: https://stackoverflow.com/questions/2281204/which-linux-ipc-technique-to-use – moghya Apr 24 '22 at 19:19

0 Answers0