IPC Inter-process communication+ Shared Memory mapping
IPC(inter-process communication, interprocess communication).
file Mapping (Mapping) is a technique for mapping the contents of a file to a memory address , and reading and writing a file is as simple as reading and writing memory by mapping memory. Multiple processes map the
same file-mapping object , that is, multiple processes are mapped to the same physical storage page, so when a process writes data to the mapped memory, Other processes can read data through mapped memory, and this mechanism enables interprocess communication.
1. The memory file mapping mapping file :
a process creates a named mapping object and writes the data that needs to be shared in the mapped memory. the b process opens the mapping object through the object name, maps the mapping object, and reads the data from the mapped memory.
2. Basic API Functions :
founding mapping objects:
HANDLEcreatefilemapping(
HANDLE hfile,//physical file handle
lpsecurity_attributes Lpattributes,Security settings
DWORD Flprotect,//Protection settings
DWORD Dwmaximumsizehigh,//High file size
DWORD Dwmaximumsizelow,//Low file size
LPCTSTR lpname//Shared memory name
);
The function returns the created mapping object. To
create a mapped memory:
lpvoidMapViewOfFile(
HANDLE Hfilemappingobject,//mapping Object
DWORD dwDesiredAccess,//Access Categories
DWORD Dwfileoffsethigh,//Map file High
DWORD Dwfileoffsetlow,//Map file status
size_t Dwnumberofbytestomap//Number of mapped bytes
);
This function is used to create the mapped memory of the mapping object and return the mapped memory.
Memory Replication :
VOIDCopyMemory(
PVOID Destination, //The destination address of the memory block to be copied
CONST VOID *source,//To copy the source address of the memory block
size_t Length//Number of bytes copied
);
This function is used to copy data to mapped memory.
Open the Mapping object:
HANDLEopenfilemapping(
DWORD dwDesiredAccess,//access rights
BOOL bInheritHandle,//Inheritance settings, generally set to false
LPCTSTR lpname//Mapping object name
);
This function is used to open an existing mapping object. Returns a handle to the mapping object.
3. kind:
Run the process1 program in VC6.0 first, run the PROCESS2 program:
operating effect :
Process1 Program:
Process2 Program: