Inter-process communication between Managed Process
There
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.
Memory
mapped files can be categorized into two types.
1.
Physical
memory mapped file.
2.
Virtual
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
hard disk.
Virtual Memory Mapped File
This
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.
Let's
have a closer look at programming the inter-process communication using the
memory mapped files.
As any
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.
MemoryMappedFile.CreateNew static
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.
The
following table lists the entire access mode supported.
Enumerator: MemoryMappedFileAccess
Member name |
Description |
ReadWrite |
Read and write
access to the file. |
Read |
Read-only access to
the file. |
Write |
Write-only access to
file. |
CopyOnWrite |
Read and write
access to the file, with the restriction that any write operations will
not be seen by other processes. |
ReadExecute |
Read access to the
file that can store and run executable code. |
ReadWriteExecute |
Read and write
access to the file that can can store and run executable code. |
Once
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.
MemoryMappedfile.CreateViewStream
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.
Once
the stream is created, the applications can perform regular read and write
operations on the file.
If
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.
When
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.