简体   繁体   English

我可以使用共享内存或其他IPC吗

[英]Can i use shared memory or other IPC

I have a process that internally makes "std::system" call. 我有一个内部进行“ std :: system”调用的过程。 (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, ie, childProcess.exe. 现在,我需要从父进程到子进程共享一个“复杂对象”,即childProcess.exe。

I tried with Boost::interprocess::shared_memory but in vain. 我尝试使用Boost :: interprocess :: shared_memory但徒劳。

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. 当我在父进程中创建对象类型为“ Comp class”的共享内存段,并在子进程中打开共享内存段时,我能够在子进程中访问“ cnt”变量。 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. 我发现即使对于std :: string,boost也会附带boost :: interprocess :: string,因为string内部会进行“新”调用。

Please suggest the best IPC mechanism to share this "Complex object" between multiple processes. 请提出最佳IPC机制,以在多个进程之间共享此“复杂对象”。

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 如果要在共享内存中使用该类成员的安排,则必须用从共享池中也从池中检索内存的调用替换对new的调用:您需要为此找到或编写一个合适的池分配器-标准不提供,但boost使用偏移而不是指针提供了相当接近的功能(这更加灵活,因为坚持在特定的绝对虚拟地址上加载共享内存不是特别可靠或可扩展)-请参见此处

Alternatives are to use threads, to serialise the data to shared memory and deserialise it (eg 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. 替代方法是使用线程,将数据序列化到共享内存并反序列化(例如,到stringstream然后将.str()数据复制到共享内存),并将subclass对象直接存储在Complex而不使用指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM