Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared memory between 2 processes (applications)

Tags:

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

like image 360
Tea Bee Avatar asked Jan 05 '10 08:01

Tea Bee


People also ask

Can you share memory between processes?

File mapping can be used to share a file or memory between two or more processes. To share a file or memory, all of the processes must use the name or the handle of the same file mapping object. To share a file, the first process creates or opens a file by using the CreateFile function.

Can two processes share data?

Yes, two processes can both attach to a shared memory segment.

How do I share data between processes?

To share big data: named shared-memory The fastest way to handle this is to create a file mapping object, map the file object into memory space, notify the parent process of the file mapping handle and data size, and then dump the data to the mapped buffer for the parent process to read.

What is shared memory in programming?

In computer programming, shared memory is a method by which program processes can exchange data more quickly than by reading and writing using the regular operating system services. For example, a client process may have data to pass to a server process that the server process is to modify and return to the client.


2 Answers

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.

like image 82
Hans Passant Avatar answered Nov 02 '22 13:11

Hans Passant


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.

like image 22
Lars Truijens Avatar answered Nov 02 '22 13:11

Lars Truijens