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

查看:379
本文介绍了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命名空间。有一个很好的理由花了这么长时间,你也不会快乐与此另外的原因。使用指针是强制性的货币市场基金,共享内存是在一个特定的地址可用。要共享的价值,你必须将其写入到一个特定的地址。

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.

您可以打破rulez,今天这样做。您可以P /调用的CreateFileMapping,OpenFileMapping和MapViewOfFile你需要创建一个MMF。而使用不安全的关键字,所以您可以创建指针。您需要使用值类型(如结构)的共享存储元件或使用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天全站免登陆