2 个进程(应用程序)之间的共享内存 [英] Shared memory between 2 processes (applications)

查看:26
本文介绍了2 个进程(应用程序)之间的共享内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到对这个问题有用的答案,尽管有人以不同的方式提出过多次.

I can't find any useful answer for this question, although it has been asked in a different way several times.

我想在两个进程(两个不同的应用程序)之间共享一个内存,以便其中一个可以写入该内存,另一个可以读取.

I want to share a memory between two processes (two different applications), so that one of them can write to that memory and the other can read.

这在 .NET 中可行吗?怎么样?

Is this possible in .NET? How?

谢谢

推荐答案

目前,.NET 不支持节(也称为内存映射文件).很快,4.0 版本就有了 System.IO.MemoryMappedFiles 命名空间.花了这么长时间是有充分理由的,也是您对这个添加不满意的原因.在 MMF 中必须使用指针,共享内存在特定地址处可用.要共享值,您必须将其写入特定地址.

Right now, .NET doesn't support sections (aka Memory Mapped Files). It will soon, the 4.0 version has the System.IO.MemoryMappedFiles namespace. There is a good reason it took so long and also the reason you are not going to be happy with this addition. Using pointers is mandatory in MMFs, the shared memory is made available at a specific address. To share a value, you'll have to write it to a specific address.

然而,指针从根本上与托管内存模型不兼容.对象在垃圾收集堆中创建,收集器根据需要移动它们以保持堆压缩.在托管语言中,您拥有对此类对象的引用,也称为跟踪句柄".C/C++ 等价物是一个指针,但它是一个带铃铛的指针,垃圾收集器总是可以找到它并更新它的值.CLR 确实支持固定"的概念,它将引用转换为指针.它通过将对象标记为不可移动来实现这一点.然而,这无助于通过 MMF 实现共享内存,对象被固定在 GC 堆中,而不是 MMF 视图所在的虚拟内存地址.

Pointers are however fundamentally incompatible with the managed memory model. Objects are created in a garbage collected heap, the collector moves them around as needed to keep the heap compacted. In a managed language, you have a reference to such an object, also knows as a "tracking handle". The C/C++ equivalent is a pointer, but it is one with bells on, the garbage collector can always find it back and update its value. The CLR does support the notion of "pinning", it converts a reference to a pointer. It implements this by marking the object as unmoveable. That however doesn't help implement shared memory through an MMF, the object is pinned in the GC heap instead of the virtual memory address where the MMF view is located.

要使 MMF 工作,需要将对象从 GC 堆复制到共享内存.这需要序列化..NET 4.0 类名为 MemoryMappedViewStream.您可能会看到这是怎么回事,这与使用命名管道或套接字没有区别.将数据输入和输出 MMF 需要付出相同的努力.MMF 只是稍微高效一点,因为底层缓冲区不在内核内存池中.

To make an MMF work, the object needs to be copied from the GC heap to the shared memory. That requires serialization. The .NET 4.0 class is named MemoryMappedViewStream. You probably can see where this is going, this is indistinguishable from using a named pipe or a socket. Getting data in and out of an MMF takes the same amount of effort. An MMF is merely slightly more efficient because the underlying buffer is not in the kernel memory pool.

你可以打破规则,今天就这样做.您可以 P/Invoke 创建 MMF 所需的 CreateFileMapping、OpenFileMapping 和 MapViewOfFile.并使用 unsafe 关键字来创建指针.您需要为共享内存元素使用值类型(如结构)或使用 Marshal 类.

You can break the rulez and do so today. You can P/Invoke the CreateFileMapping, OpenFileMapping and MapViewOfFile you need to create an MMF. And use the unsafe keyword so you can create pointers. You'll need to use value types (like struct) for the shared memory elements or use the Marshal class.

这篇关于2 个进程(应用程序)之间的共享内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆