Original: http://blog.csdn.net/ljianhui/article/details/10253345
Shared memory is mainly achieved through the mapping mechanism.
The address space of processes under Windows is logically isolated from each other, but is physically overlapping. The so-called overlap means that the same piece of memory may be used by multiple processes simultaneously. When calling CreateFileMapping to create a named memory-mapped file object, Windows is requesting a block of memory of the specified size in physical memory, returning a handle to the file-mapping object Hmap. In order to be able to access this area of memory, the MapViewOfFile function must be called, prompting Windows to map this memory space to the process's address space. When other processes access this area of memory, you must use the OpenFileMapping function to obtain the object handle Hmap and call the MapViewOfFile function to get a mapping of this memory space. In this way, the system maps the same piece of memory to the address space of different processes, thus achieving the purpose of shared memory.
The following example shows how to use a memory-mapped file for shared memory.
The first time you run this example, it creates the shared memory and writes the data "This is common data!". As long as the process of creating the shared memory does not close the handle HMAP, the program that runs later reads the data from the shared memory and prints it out. This is the process of using inter-process communication within a share. The program code is as follows.
[CSharp]View PlainCopy
- #include <windows.h>
- #include <iostream>
- #include <string>
- #include <cstring>
- Using namespace std;
- int main ()
- {
- string Strmapname ("sharememory"); //memory-mapped object name
- string Strcomdata ("This is common data!"); //Shared in-memory data
- LPVOID pbuffer; //Shared memory pointers
- //First attempt to open a named memory-mapped file object
- HANDLE Hmap =:: OpenFileMapping (file_map_all_access, 0, Strmapname.c_str ());
- if (NULL = = Hmap)
- { //open failed, created
- Hmap =:: CreateFileMapping (Invalid_handle_value,
- Null
- Page_readwrite,
- 0,
- Strcomdata.length () +1,
- Strmapname.c_str ());
- //Map A view of the object, get pointers to shared memory, set the data inside
- pbuffer =:: MapViewOfFile (Hmap, file_map_all_access, 0, 0, 0);
- strcpy ((char*) pbuffer, Strcomdata.c_str ());
- cout << "Write Shared memory data:" << (char *) pbuffer << Endl;
- }
- Else
- { //open successfully, map object to a view, get pointer to shared memory, show the data inside
- pbuffer =:: MapViewOfFile (Hmap, file_map_all_access, 0, 0, 0);
- cout << "read shared memory data:" << (char *) pbuffer << Endl;
- }
- GetChar (); //Note that after the process is closed, all handles are automatically closed, so pause here
- //de-file mapping, close memory-mapped file object handle
- :: UnmapViewOfFile (pbuffer);
- :: CloseHandle (HMAP);
- System ("pause");
- return 0;
- }
Run the program twice in a row, and their output is as follows:
-
Top
-
0
-
Step
Windows Shared Memory sample