Click here to Skip to main content
Licence 
First Posted 25 Jun 2005
Views 46,663
Downloads 134
Bookmarked 24 times

Using FileMapping on .NET as IPC

By aalday | 21 Jan 2007
Sample code for using FileMapping on .NET.
3 votes, 33.3%
1

2
2 votes, 22.2%
3
1 vote, 11.1%
4
3 votes, 33.3%
5
3.00/5 - 9 votes
μ 3.00, σa 3.09 [?]

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

aalday

Web Developer

Spain Spain

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalwith read from C++ program Pinmemberftaka22:06 29 Apr '08  
GeneralCode corrected and revised Pinmemberaalday21:18 21 Jan '07  
GeneralSomething wrong PinmemberAlexey_i2:33 21 Oct '06  
GeneralRe: Something wrong Pinmemberaalday21:16 21 Jan '07  
GeneralFaster implementation Pinmemberstudio_ukc9:18 28 Sep '06  
QuestionCopyright/Usage? Pinmemberbrian_birtle7:25 6 Nov '05  
Thanks for posting such a useful module. Is it OK if I use it in my commercial application? We'll make sure your name and a link to this website is clearly listed in the code so all our developers know what a help you've been.
 
Big Grin | :-D
 
- Brian
AnswerRe: Copyright/Usage? Pinmemberaalday10:29 6 Nov '05  
GeneralNext time post in proper category Pinmemberfwsouthern20:51 25 Jun '05  
GeneralRe: Next time post in proper category Pinmemberaalday22:28 25 Jun '05  
GeneralFORTRAN PinsussAnonymous14:23 25 Jun '05  
GeneralSeems great PinmemberDaniel Turini13:26 25 Jun '05  
GeneralRe: Seems great Pinmemberaalday22:40 25 Jun '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 22 Jan 2007
Article Copyright 2005 by aalday
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid