Background
In Unix-like systems for IPC (Inter-process communication) communication, Shared memory is the most efficient, I call it the king of IPC.
Introduction
This article describes how to use Shared Memory (Shared Memory) for IPC (inter-process communication) in Windows Mobile and Windows Embedded CE ). Demonstrate how to use Shared Memory to share data, use Named Event to wake up other processes, and use Named Mutex to lock Shared data.
Main IPC Methods
In Windows Mobile and Windows Embedded CE systems, the main IPC methods are as follows.
Method |
Notification |
Data Storage |
Data size |
Named events |
Indirect |
N/ |
N/ |
Windows messages |
Indirect |
In Message |
Very small. Only Integer can be transmitted or objects can be transmitted using COPYDATASTRUCT |
Point-to-point message queues |
Indirect |
In Message |
Small, with boxing and unboxing Problems |
MSMQ |
Direct |
In Message |
Small, with boxing and unboxing Problems |
TCP sockets |
Direct |
Direct stream) |
Moderate |
Memory mapped files |
N/ |
Mapped file |
Moderate |
Registry |
N/ |
Registry |
Moderate |
File system |
N/ |
File |
Large |
Database |
N/ |
Database |
Large |
WCF |
N/ |
WCF message |
Moderate |
The above table references Interprocess Communication with the. NET Compact Framework 1.0
There is no better method for the above IPC, and it should be determined based on the specific needs. This article focuses on Named events and Shared Memory. I have also written an article about other IPC methods. For details, refer to the following:
Windows Message
Windows Message for inter-process communication under. NET Compact Framework
MSMQ
Install MSMQ in Windows Mobile and WinCe
MSMQ development for inter-process communication under. NET Compact Framework
Registry
Development of registry export tool under. NET Compact Framework
File System
Use TinyXML for Native C ++ development in Windows Mobile and Wince
Database
Use of SQL CE under. NET Compact Framework (implement the SqlCeHepler encapsulation test class of SqlCeHepler, see unit test under. NET Compact Framework)
Encapsulation of access to SqlCe by Native C ++ in Windows Mobile
SQL Server Express and SQL Server Compact applications
Compatibility of SQL CE in. NET Campact Framework
Access Native C ++ encapsulation of Sqlite in Windows Mobile
How to compress SQLite data files
Some topics, such as Point-to-point message queues and TCP sockets, are not written. If someone wishes to summarize them, leave a message and I will add it later.
Implementation of Shared Memory
The implementation code mainly refers to the Smart Device Framework of OpenNETCF.
Three key classes
MemoryMappedFileUsed to encapsulate shared Memory. The shared Memory under Windows Embedded CE is a Memory Mapped File, that is, a Memory ing File, which maps files in the Memory that can be accessed by all processes, this shared memory operation is similar to a disk physical file. So it inherits from Stream and reads and writes through Stream.
NamedMutexIt is a process-Level Lock. In Native C ++, CRITICAL_SECTION is used for the lock, while in. NET Compact Framework uses monitor. Microsoft has encapsulated monitor into the lock keyword. Note that this lock is not a function and is an embedded keyword in C. Lock is equivalent to Monitor.
For the use of lock and CRITICAL_SECTION, refer to the following article.
Use. NET Compact Framework to develop multi-threaded programs on Windows Mobile
Windows Mobile uses Native C ++ to develop multi-threaded programs
So, since the lock and CRITICAL_SECTION still need Mutex, the performance of Mutex is lower than that of Monitor, that is, lock, so I usually use lock instead of Mutex, however, lock does not support cross-process locking, so in this case, I used Mutex.
. NET Compact Framework itself provides a Mutex class, but unfortunately only supports the unknown Mutex. Mutex can be named Mutex and Mutex without any name. Mutex without any name can only be used within the same process and cannot be used across processes. Therefore, a NamedMutex is encapsulated to support the naming of Mutex, it supports cross-process lock operations.
EventWaitHandleIs a notification Event .. NET Compact Framework encapsulates AutoResetEvent and ManualResetEvent, but neither of them supports cross-process, so it encapsulates EventWaitHandle to implement cross-process Event notification.
SharedMemoryWriter
SharedMemoryWriter writes data to the shared memory.
private void StartSharedMemoryWriting()
{
MemoryMappedFile mmf = MemoryMappedFile.CreateInMemoryMap("SharedMemoryBlock");
int i = 100;
while (started)
{
string s = "SharedMemory:" + i.ToString();
UpdateMessageList(s);
byte[] dataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(s);
// Wait until it is safe to enter.
mutex.WaitOne();
try
{
mmf.Position = 0;
mmf.Write(dataBuffer, 0, dataBuffer.Length);
}
finally
{
// Release the Mutex.
mutex.ReleaseMutex();
}
// Raise the event
namedEvent.Set();
++i;
if (i > 999)
{
i = 100;
}
System.Threading.Thread.Sleep(500);
}
mmf.Close();
}
Generate the shared memory with the unique name of the system. In this example, the SharedMemoryBlock is used. The memory is locked through Named Mutex each time the shared memory is written. After the write is complete, use the Named Event to notify the SharedMemoryReader (read shared memory) process.
SharedMemoryReader
SharedMemoryReader reads data from the shared memory.
private void StartSharedMemoryReading()
{
MemoryMappedFile mmf = MemoryMappedFile.CreateInMemoryMap("SharedMemoryBlock");
byte[] dataBuffer = new byte[1024];
while (started)
{
if (namedEvent.WaitOne())
{
if (!started)
{
break;
}
namedEvent.Reset();
// Wait until it is safe to enter.
if (mutex.WaitOne())
{
try
{
mmf.Position = 0;
mmf.Read(dataBuffer, 0, 50);
}
finally
{
// Release the Mutex.
mutex.ReleaseMutex();
}
}
string s = System.Text.ASCIIEncoding.ASCII.GetString(dataBuffer, 0, 50);
UpdateMessageList(s);
}
}
mmf.Close();
}
Open the shared memory with the same name (SharedMemoryBlock). The process suspends until the message of the Named Event is received. The Named Mutex is used to lock each read operation.
Source code: http://files.cnblogs.com/procoder/SharedMemoryDemo.rar
Environment: Windows Mobile 5 PPC +. NET Compact Framework 2.0