1
votes

I have a process that internally makes "std::system" call. (I believe system call spawns a child process). In the system call, I am executing different application as

void generate()
{
   std::system("./childProcess.exe);
}

The above "generate function" would be called by multiple threads. Now, I need to share a "complex object" from Parent process to child process, i.e., childProcess.exe.

I tried with Boost::interprocess::shared_memory but in vain.

The complex object i mentioned earlier internally dynamically allocates memory many a times. I believe those dynamically allocated memories are not associated with my shared memory segment. Please correct me if otherwise

My class is like this

Complex compClass
{
   int cnt;
   subclass *subobj;
}

compClass::compClass()
{
   subobj=new subclass;
   subobj->func;
}
subclass::func()
{
   YClass *y = new YClass;
}

and so on, it internally has many such memory allocations.

When I create a shared memory segment of object type "Comp class" in parent process, and open the shared memory segment in child process, i am able to access "cnt" variable in child process. But, I am not able to access subobject in child process.

I believe this is because subobject is dynamically allocated and we have different dynamic memory allocated in child processes and they are not associated with shared memory segment.

I found even for std::string, boost comes up with boost::interprocess::string as string internally makes "new" call.

Please suggest the best IPC mechanism to share this "Complex object" between multiple processes.

1
boost::interprocess::string is just an alias for boost::containers::string IIRC. The important thing is to use an allocator to allocate from the shared memory segment manager, AFAIR - sehe
That's right. Since String uses its own "new" call, we wanted to make use of allocator. But, in my application, I am allocating memory of different types in many places and it may not be feasible to incorporate these boost::containers in all those places... - user3665224

1 Answers

0
votes

If you want to use that class member arrangement in shared memory, you'll have to replace the calls to new with ones that retrieve memory from a pool also in shared memory: you'll need to find or write a suitable pool allocator for that - the Standard doesn't provide one, but boost provides something pretty close using offsets instead of pointers (which is more flexible because insisting on loading shared memory at specific absolute virtual addresses isn't particularly reliable or scalable) - see here

Alternatives are to use threads, to serialise the data to shared memory and deserialise it (e.g. to a stringstream then copying the .str() data to shared memory), and to store the subclass object directly in Complex instead of having a pointer.