Hi Guys,
This is the first part of inter-process communication through memory mapped files. In this part, I will teach you
What is Memory Mapped File?
How to create Memory Mapped File?
How to write in Memory Mapped File?
How to read Memory Mapped File?
What is Memory Mapped File?
A memory-mapped file is a file which contains the contents of a file in virtual memory. This mapping between a file and memory space enables an application, including multiple processes, to modify the file by reading and writing directly to the memory.
There are two types of memory-mapped files:
· Persisted memory-mapped files
Persisted files are memory-mapped files that are associated with a source file on a disk. When the last process has finished working with the file, the data is saved to the source file on the disk. These memory-mapped files are suitable for working with extremely large source files.
· Non-persisted memory-mapped files
Non-persisted files are memory-mapped files that are not associated with a file on a disk. When the last process has finished working with the file, the data is lost and the file is reclaimed by garbage collection. These files are suitable for creating shared memory for inter-process communications (IPC).
Non-persisted memory-mapped files are files which are not associated with a file on a disk. Therefore, we can use these files to create shared memory and use this memory for inter-process communication. In this tutorial, I will create non-persisted memory files for inter-process communication
How to create Memory Mapped File?
Add following namespace before creating file
using System.IO.MemoryMappedFiles;
MemoryMappedFile file = MemoryMappedFile.CreateNew("MyMemoryMapFile",4096,MemoryMappedFileAccess.ReadWrite);
How to write in Memory Mapped File?
To work with a memory-mapped file, you must create a view of the entire memory-mapped file or a part of it. If you want to write in memory mapped file, then you need to create memory view accessor to access it and then use its write method to write.
MemoryMappedViewAccessor accessor = file.CreateViewAccessor();
accessor.Write();
How to read Memory Mapped File?
To work with a memory-mapped file, you must create a view of the entire memory-mapped file or a part of it. If you want to write in memory mapped file, then you need to create memory view accessor to access it and then use its Read method to write.
MemoryMappedViewAccessor accessor = file.CreateViewAccessor();
accessor.Read();
Let’s create two console application to see inter-process communication in action. The first application will create a memory mapped file, write data structure in it and then second application will access the file and read data from it.
Console Application 1:
1. Create Console application in Visual Studio
2. Copy and paste below code in Program.cs
3. Press F5 to run the console application and keep running the application so that we can read what we have written in memory mapped file in console application 1.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.MemoryMappedFiles;
namespace MemoyMapFilesTest
{
class Program
{
public struct MyDataStructure
{
public Int32 MyValueA { get; set; }
public Int32 MyValueB { get; set; }
}
static void Main(string[] args)
{
using (MemoryMappedFile file = MemoryMappedFile.CreateNew("MyMemoryMapFile",4096,MemoryMappedFileAccess.ReadWrite))
{
using(MemoryMappedViewAccessor accessor = file.CreateViewAccessor())
{
MyDataStructure data = new MyDataStructure();
data.MyValueA = 12;
data.MyValueB = 20;
accessor.Write<MyDataStructure>(0,ref data);
//write info
Console.WriteLine("Memory Mapped File created");
//wait
Console.ReadKey();
}
}
}
}
}
Output
Console Application 2:
1. Create Console application in Visual Studio
2. Copy and paste below code in Program.cs
3. Press F5 to run the console application and see the output, it read what we have written in console application 1.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.MemoryMappedFiles;
namespace MemoryMapFileTest2
{
public struct MyDataStructure
{
public Int32 MyValueA { get; set; }
public Int32 MyValueB { get; set; }
}
class Program
{
static void Main(string[] args)
{
MyDataStructure data = new MyDataStructure();
using (MemoryMappedFile file = MemoryMappedFile.OpenExisting("MyMemoryMapFile", MemoryMappedFileRights.Read))
{
using (MemoryMappedViewAccessor accessor = file.CreateViewAccessor(0,0,MemoryMappedFileAccess.Read))
{
accessor.Read<MyDataStructure>(0,out data);
}
}
//write info
Console.WriteLine("Memory Mapped File read");
Console.WriteLine("MyValueA = " + data.MyValueA.ToString());
Console.WriteLine("MyValueB = " + data.MyValueB.ToString());
//wait
Console.ReadKey();
}
}
}
Output
So, we have implemented the inter-process communication through memory mapped file. In this part, I have created a structure MyDataStructure to pass between processes. Also, note that we have created MyDataStructure in both applications.
In next part, I will implement to transfer a class object from one application to another.
Thanks for reading & Keep Learning with Programming Passion.