Method for resolving inter-process communication under Windows: Memory map technology

xiaoxiao2021-03-06  105

There is sometimes communication between the processes. The communication mechanism between the processes provided by the system uses memory mapping file technology. All lower 2GB addresses that a process can access are their own address space, which will be limited by the kernel when accessing the kernel address space. Such a process is of course unable to access other processes. In order to solve the problem of inter-process communication, memory map technology is utilized as a solution. Original memory mapped files only map files on a memory that similar to disk. In order to communicate more quickly, the memory mapping file can also submit physical memory. The implementation method is to access the same physical memory by accessing the same memory mapping file object (mapping to physical memory), two processes or multiple processes can access the same physical memory, such a process writes data for physical memory, and other processes can be seen. . WINDOWS CE Although each process accounts only 32MB of address space, and all processes are in the 4GB address space, they are still unable to access each other. In addition to using memory mapping file technology, there is also a method of use, which is to store with object storage. Object storage Itself uses the RAM file system, you can create with an API of a normal operation file, and read the files stored in the object storage area.

The / Windows directory exists within the object storage area. We can use to create files between the / windows directory to implement inter-process communication. This method is simply realized, just calls several file API functions, but also reduces communication time, because the / windows directory exists in physical memory, data I / O is of course soon. Using object storage to implement communication between processes is what I think, MSDN, or other documents does not have this description. It is necessary to pay attention to the size of the object storage area. In addition, it is not as memory mapped file technology from the amount of code implemented.

The following explains how to use the memory map file to implement the communication between the processes. Suppose processes A and process B need to communicate, then process A needs to create a memory mapping file (you don't have to call the createFileFormApping function to create files because you don't need to create a file). This memory map file can be in a permanent memory or in memory. In order to reduce the communication time, it is best to submit physical memory. Process A When calling the createFileMApping function, the parameter 1 is specified as INVALID_HANDLE_VALUE, which means that this memory mapping file object will submit physical memory into the address space. The last parameter must specify a name. Process B also calls the CreateFilemapping function, and the parameter is the same. The kernel will determine if there is a memory map object that has already existed, if you create a handle of the original object is created. I don't have to say it. Refer to 5.1 to execute it. It should be noted that the process B calls the createFileMapping function to execute the result of the function:

Handle HMAP;

HMAP = CREATEFILEMAPPING (INVALID_HANDLE_VALUE,

NULL,

Page_readwrite,

0,

1000,

L "ABC");

IF (hmap == null || getLastError ()! = error_already_exists

{

MessageBox (l "create file mapping fail");

Return;

}

转载请注明原文地址:https://www.9cbs.com/read-97226.html

New Post(0)