开发者

Shared memory between 2 processes (applications)

开发者 https://www.devze.com 2022-12-15 16:54 出处:网络
I can\'t find any useful answer for this question, although it has been asked in a different way several times.

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.

Is this possible in .开发者_开发技巧NET? How?

Thanks


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.

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.

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.

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.


IPC

If you are talking about Inter Process Communication. There are several possibilities like using tcp/udp sockets, mailslots, named pipes, memory mapped files, windows messages, etc.

.Net also offers some higher level IPC like .Net remoting and WCF which use the previous mentioned techniques. You can read more about it here.

Memory Mapped Files

If you really want to share a block of memory instead of communication then maybe memory mapped files are what you want. .Net 4.0 has MemoryMappedFile for that. If you don't or can't use .Net 4.0 you could implement it yourself as in this win32 example. Or you could try this code or see if you can use MemoryMappedFileStream mentioned here and here. Or use this FileMap class.


Use a named pipe.

It's a mechanism that allows one process to write sequential data into a stream, from which the other process can read.

Note that named pipe only allow sequential read. If you need to use more elaborated queries, you might want to use client-server architecture. In this case, the reader process queries the other process for information using network sockets.


As already mentioned use Memory Mapped Files. Implementation for .NET < 4 available at: http://github.com/tomasr/filemap/
I have used and it works flawless. You may have some security issues when sharing between elevated/not elevated processes - the solution is to initialize the memory mapped file in the elevated process and set its security attributes accordingly.


The is an implementation , plus description at http://www.softwareinteractions.com/blog/2010/1/21/interprocess-communications-using-shared-memory.html

0

精彩评论

暂无评论...
验证码 换一张
取 消