Using FileMapping on .NET as IPC





3.00/5 (9 votes)
Jun 25, 2005
1 min read

72707

407
Sample code for using FileMapping on .NET.
Introduction
.NET inter-process mechanisms are very slow. With some system calls, it becomes easy to use file mapping and that's the faster mechanism for IPC.
Classes
MemMap
This class encapsulates a file mapping zone with system page-file support. All you need to do is give it a name and size.
Usage:
'Process 1 dim mem1 as MemMap=new MemMap("ZONE1",2048) mem1.writeString(0,"String to share") 'Process 2 dim mem1 as MemMap=new MemMap("ZONE1",2048) dim sharedString=mem1.readString(0) mem1.close() 'at the end of both programs
This class handles the creation or reuse of file mapping within the "
new
" method and has methods for reading and writing integers and strings on a given offset.It gives also a locking function based on InterLockedExchanged with protection against process elimination while having holding a lock.
MemChannel
This class uses the
MemMap
class to implement a FIFO queue of strings. You can create any number of channels by simply giving different names to each one of them.Usage:
'Process1 dim cha1 as MemChannel=new MemChannel("CHANAME1") cha1.putMsg("This is the message") 'Process2 dim cha1 as MemChannel=new MemChannel("CHANAME1") msgbox cha1.getMsg() cha1.close() 'at the end of both programs
EXEs
- MServer.exe
Example of a server reading from a channel.
- Client.exe
Example of a client writing to a channel.
Points of interest
The sample programs included here runs at 250.000 msgs per second on a PIV 3 GHz. But they block each other while accessing the channel and the CPU is not 100% busy.
With MemMap
class support, it becomes easy to implement other IPC mechanisms as shared Dictionaries.
History
- 25-06-2005 - First version.
- 20-01-2007 - Second version