Inter-process communication between Managed Process
are certain cases, where the developer needs to pass data
to another process running on the same system. One of the efficient ways of
handling the situation in a Win32 application is using memory mapped files.
Mapping between a file and memory allows multiple applications to read
and write in the file at the same time. Starting from .NET 4.0, Managed
applications can use the memory mapped file functionality as the traditional
Win32 application does.
mapped files can be categorized into two types.
memory mapped file.
memory mapped file.
Physical Memory Mapped File
Physical memory mapped files are also referred as Persisted memory mapped files.
This category of memory mapped file is always associated with a physical file in
your hard disk. Very suitable for applications which need to work with huge
data files. Once the last process is finished working with the physical memory
mapped file, the contents of the file is written back to the source file on the
Virtual Memory Mapped File
category is also known as Non-Persisted memory mapped file. As the name means
indicates, it is not associated with a source file on hard disk. It is very effective
for run time data communication between two processes.
have a closer look at programming the inter-process communication using the
memory mapped files.
other files programing you do, the first step is to create the memory mapped
file. System.IO.MemoryMappedFile class provides many different static
functions to create the memory mapped file.
function takes 2 arguments. The first one is a string parameter specifying a
unique name for the file. This name will uniquely identify the memory mapped
file across the processes. The second parameter is a long data type, used to
specify the maximum number of bytes that can be stored by the memory mapped file.
While creating the file you can also specify the access mode for restricting the
access to that file.
following table lists the entire access mode supported.
Read and write access to the file.
Read-only access to the file.
Write-only access to file.
Read and write access to the file, with the restriction that any write operations will not be seen by other processes.
Read access to the file that can store and run executable code.
Read and write access to the file that can can store and run executable code.
the file is created, the application must create a view of the file in order to
read and write to the file. The view can point to the complete file or to a
specific portion of the file.
instance function creates a view stream which provides read write operations to
the Memory mapped file. You can also create multiple views to the same part of
the memory-mapped file, thereby creating concurrent memory. For two views to
remain concurrent, they have to be created from the same memory-mapped file.
the stream is created, the applications can perform regular read and write
operations on the file.
another process wants to access the same file for read/write purposes, the
second process should use "MemoryMappedFile.OpenExisting" function to
open the file. OpenExisting function takes file name as an argument. This file
name is a unique name, which was provided in the MemoryMappedFile.CreateNew
function by the process one.
two process trying to access the same file, of course it is the programmer's
responsibility to make sure that we take care of the synchronization part of it. In
the attached sample application, I have used a mutex to implement the
synchronization between the processes.