Participating in the relocation of an old system, where Shared Memory is used to communicate across processes (e.g., calling the Window Service ASP.NET), and is forced to understand the importance of this technology. NET developers' unpopular technology, special notes.
[What is Shared Memory?] 】 There is a term for cross-process communication, Interprocess Communictaion (IPC), which has the following options on the Windows platform: Reference:The hyperlink login is visible.
Clipboard Program A pastes the content into the clipboard, and Program B retrieves the content from the clipboard. COM OLE Compound Document allows Word files to be embedded in Excel worksheets, and Excel can be called up for editing with two clicks. Data Copy Program A sends WM_COPYDATA messages to Program B in the agreed format DDE DDE is a communication protocol that allows different applications to exchange data in different formats, which can be regarded as an extension of the clipboard, and can be transferred continuously in addition to one-time transfers. (Relatively poor performance, no longer recommended) File Mapping File Mapping refers to simulating a file as a piece of memory in a process, when multiple applications exchange data through shared file mapping, called Named Shared Memory, which is the best performance among various IPC methods, but must be prevented by synchronization mechanisms such as Mutex. Mailslots One-way communication, Mailslot Client sends messages to Mailslot Server, and the messages are deleted after being read by the server, supporting cross-machine transmission, and can also be broadcast one-to-many. (The length of the broadcast message is limited to 400 bytes, and the length of the message is determined by the Mailslot Server when it is established for one-to-one transmission) Pipes Bidirectional transmission is divided into Anonymous Pipe and Named Pipe. Anonymous Pipe is generally used for the standard input/output orientation between the parent program and the child program, and two pipes should be built for two-way communication, which cannot be across the network and is limited to processes with dependent relationships. Named Pipe can be used to exchange data between any process and support cross-network process transfer. RPC Remote Procedure Call (RPC) allows applications to call functions provided by other applications and can be called across networks. Windows RPC complies with the ISO DCE standard and supports cross-operating system integration. Windows Sockets Abstract communication interface based on TCP/IP or other network protocols, and data exchange is carried out through network connections at the bottom layer. Shared Memory is a common data exchange method used by C/C++ developers (Google can find many examples of IPC implemented with Shared Memory on Linux), so C/C++ developers often choose it as a communication channel on the Windows platform.
【Shared Memory Practical Exercise】 Although there are fewer people who use it, .NET has a built-in System.IO.MemoryMappedFiles namespace, it is not difficult to play Shared Memory, almost the same as operating files, as long as you have experience in FileStream-related operations, you can get started quickly, refer to the MSDN example, I wrote a mini program exercise.The hyperlink login is visible.
I wrote two programs, ProcessA to create a space of 1024 bytes with MemoryMappedFile.CreateNew() and practice passing and catching with the other ProcessB. Since 1024 bytes are shared by two programs, I plan the first 512 to be ProcessA written to ProcessB and the last 512 to be ProcessB written to ProcessA read, and the program uses CreateViewStream to pass in the start address and length to point to its own area. To avoid read/write conflicts when ProcessA and ProcessB access the MemoryMappedFile, I use Mutex lock to control that only one Process can access the MemoryMappedFile at a single time. The test process creates a MemoryMappedFile for ProcessA, writes the message string – > ProcessB reads the message string and writes the response string – > ProcessA reads the response string, and ends.
The ProcessA procedure is as follows:
The ProcessB procedure is as follows:
The test was successful!
【Supplementary Tips】
1. How do I view the MemoryMappedFile that is currently enabled in Windows?
SystemInternals has an AccessChk tool that lists all files, folders, registries, objects, and Windows services that are accessible to Windows. MemoryMappedFile is a Windows object, and you can use the following command to list all objects and archive them accesschk -osv > e:\objList.txt Look for the name of MemoryMappedFile in it, and if it exists, you can see a record similar to the following: \Sessions\1\BaseNamedObjects\DARKTHREAD Type: Section Medium Mandatory Level (Default) [No-Write-Up] RW NT AUTHORITY\SYSTEM SECTION_ALL_ACCESS RW DOMAIN\UserName SECTION_ALL_ACCESS RW DOMAIN\UserName-S-1-5-5-0-954410 SECTION_ALL_ACCESS
AccessChk tool download:The hyperlink login is visible.
2. The MemoryMappedFile preset is enabled in the user's session, and the preset cannot be used across sessions. For example, if two ASP.NET belonging to different AppPools have different execution identities, even if the MemoryMappedFile name is the same, it is still one copy of each one, so it is necessary to confirm that the execution identity used by both parties is the same.
3. To communicate across different execution identities, the MemoryMappedFile can be named "Global\ Filename" (note that the case of Global is different, I stepped on the thunder that was mistakenly written as GLOBAL path is invalid), so that it can be accessed across execution identities. However, it should be noted that processes other than Session 0 (Windows Service) need to have the SeCreateGlobalPrivilege permission to create a Global\... MemoryMappedFile。 (MSDN file) For Session 0, you can refer to this article by MVP on the other side - Penetrating Session 0 Isolation (1) (The hyperlink login is visible.There is a very detailed introduction in it.
Reprinted from:The hyperlink login is visible.
|