英文:
Is it possible to share a pointer between (non-inherited) processes?
问题
案例很简单,我有两个不相关的进程(用C++编写的应用程序A和B,它们不是彼此的子进程),它们在同一个共享内存对象上操作。
在进程A中,我创建一个表示要在进程B中访问的GPU内存句柄,所以基本上我有一个存储在共享内存对象中的void指针和表示支持指针的内存大小的uint_64。
在进程B中,我成功地映射到相同的共享内存,并从中获取句柄和所需的内存大小,这两个变量的值与它们在进程A中的值相同。所以到目前为止,一切看起来都不错,问题是如果我尝试使用句柄并导入内存,似乎它是无效的。
根据我所读,不同的进程具有完全不同的内存映射,所以即使指针的值相同,它也可以指向完全不同的内存部分。这是真的吗?如果是这种情况,是否有任何方法可以在(非父->子)进程之间共享指针,以使A中的值指向与B中相同的内存?
是否可能?我在Windows上,我可以接受特定于平台的解决方案。
英文:
The case is simple I have two unrelated processes (Plain C++ written apps A and B, one is not a childprocess of the other one), and they operate on same shared memory object.
In process A, I create a handle representing a handle to GPU memory, which I want to access in process B. So basically I have a void pointer stored in the Shared memory object and uint_64 representing size of the memory backing the pointer.
In process B I successfully map to the same shared mem. and I obtain the handle from it and required mem. size, the both variables have same values as they had in process A. So until heree it seems good, the problem is that if I try to use the handle and import the memory it seems it is invalid.
From what I read the different processes have completely different memory mapping, so even if the value of the pointer is same, it can point to the completely different piece of memory. Is that really true? And if that is the case is there any way how to share pointers between (non parent->child) processes, in a way that the value in A points to same memory as in B?
Is it possible or not? I am on Windows and I am OK with platform specific solution.
答案1
得分: 1
一般来说,每个进程都有自己的内存映射。内存映射是一个从虚拟地址到物理地址的内存区域列表,即一组(vm_start,physical_start,size)元组(可能还包括权限标志等其他字段)。
因此,如果进程P1和进程P2的内存映射在指针附近相同,那么在进程P1中的指针与进程P2中的指针操作的是相同的值。默认情况下,这并不成立,如果需要的话,您需要向这两个进程的内存映射中添加相同的条目(具有相同的physical_start)。通过一些Windows API函数应该可以实现这一点,甚至可以用于与GPU共享的内存。具体如何做到这一点,我不清楚。
如果不进行设置,进程P2在使用来自进程P1的指针时通常会出现错误,或者P2会在无关的值上进行静默修改(导致进程P2的未来出现问题)。
这些进程之间的父子关系并不相关。
英文:
In general, each process has its own memory map. A memory map is a list of memory regions mapping from virtual addresses to physical addresses, i.e. a list of (vm_start, physical_start, size) tuples (and possibly other fields such as permission flags).
Thus a pointer in process P1 operates on the same value as in process P2 iff the memory map in these processes is the same near the pointer. This is not the case by default, and if you need it, you need to add the same entry (with the same physical_start) to the memory map of both processes. This should be possible using some Windows API functions, even for memory shared with the GPU. How exactly to do it, I don't know.
Without setting it up, process P2 will typically abort with an error if a pointer from process P1 is used, or P2 will silently modify an unrelated value (causing problems in the future of process P2).
The parent-child relationship of the processes is not relevant.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论