Creating Named Shared Memory To share data, multiple processes can use memory-mapped files that are backed by the system paging file.First ProcessThe first process creates the file mapping object by calling the CreateFileMapping function with INVALID_HANDLE_VALUE and a name for the object. By using the PAGE_READWRITE flag, the process has read/write permission to the memory through any file views that are created. Then the process uses the file mapping object handle that CreateFileMapping returns in a call to MapViewOfFile to create a view of the file in the process address space. The MapViewOfFile function returns a pointer to the file view.When the process does not need access to the file mapping object, it should call the CloseHandle function. When all handles are closed, the system can free the section of the paging file that the object uses. #include <windows.h> #include <stdio.h> #include <conio.h>#define BUF_SIZE 256 TCHAR szName[]=TEXT("MyFileMappingObject"); TCHAR szMsg[]=TEXT("Message from first process");void main() { HANDLE hMapFile; LPCTSTR pBuf; hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, // use paging file NULL, // default security PAGE_READWRITE, // read/write access 0, // max. object size BUF_SIZE, // buffer size szName); // name of mapping object
if (pBuf == NULL) { printf("Could not map view of file (%d).\n", GetLastError()); return; } CopyMemory((PVOID)pBuf, szMsg, strlen(szMsg)); getch(); UnmapViewOfFile(pBuf); CloseHandle(hMapFile); } Second ProcessA second process can access the same data by calling the OpenFileMapping function with the same name as the first process. Then it can use the MapViewOfFile function to obtain a pointer to the file view. #include <windows.h> #include <stdio.h> #include <conio.h> #include <windows.h>#define BUF_SIZE 256 TCHAR szName[]=TEXT("MyFileMappingObject");void main() { HANDLE hMapFile; LPCTSTR pBuf; hMapFile = OpenFileMapping( FILE_MAP_ALL_ACCESS, // read/write access FALSE, // do not inherit the name szName); // name of mapping object
if (hMapFile == NULL) { printf("Could not open file mapping object (%d).\n", GetLastError()); return; }
To share data, multiple processes can use memory-mapped files that are backed by the system paging file.First ProcessThe first process creates the file mapping object by calling the CreateFileMapping function with INVALID_HANDLE_VALUE and a name for the object. By using the PAGE_READWRITE flag, the process has read/write permission to the memory through any file views that are created. Then the process uses the file mapping object handle that CreateFileMapping returns in a call to MapViewOfFile to create a view of the file in the process address space. The MapViewOfFile function returns a pointer to the file view.When the process does not need access to the file mapping object, it should call the CloseHandle function. When all handles are closed, the system can free the section of the paging file that the object uses.
#include <windows.h>
#include <stdio.h>
#include <conio.h>#define BUF_SIZE 256
TCHAR szName[]=TEXT("MyFileMappingObject");
TCHAR szMsg[]=TEXT("Message from first process");void main()
{
HANDLE hMapFile;
LPCTSTR pBuf; hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // max. object size
BUF_SIZE, // buffer size
szName); // name of mapping object
if (hMapFile == NULL || hMapFile == INVALID_HANDLE_VALUE)
{
printf("Could not create file mapping object (%d).\n",
GetLastError());
return;
}
pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
printf("Could not map view of file (%d).\n",
GetLastError());
return;
}
CopyMemory((PVOID)pBuf, szMsg, strlen(szMsg));
getch(); UnmapViewOfFile(pBuf); CloseHandle(hMapFile);
}
Second ProcessA second process can access the same data by calling the OpenFileMapping function with the same name as the first process. Then it can use the MapViewOfFile function to obtain a pointer to the file view.
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <windows.h>#define BUF_SIZE 256
TCHAR szName[]=TEXT("MyFileMappingObject");void main()
{
HANDLE hMapFile;
LPCTSTR pBuf; hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
szName); // name of mapping object
if (hMapFile == NULL)
{
printf("Could not open file mapping object (%d).\n",
GetLastError());
return;
}
pBuf = MapViewOfFile(hMapFile, // handle to mapping object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
printf("Could not map view of file (%d).\n",
GetLastError());
return;
} MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK); UnmapViewOfFile(pBuf); CloseHandle(hMapFile);
}
至于后一个要求(只要内存中一有数据就进行某种操作),可以额外创建一个event,服务器把信息写进去之后,用event通知客户端,客户端可以一直阻塞等待此event。
只是我没有看懂。
最近正在学习这方面的东西。
俺师傅让的!!
我从没看过书,所以自己用的一些方法也不知道名字,那么请问我所说的空的for循环就是阻塞等待发放吗?