共享内存主要是通过映射机制实现的。
Windows 下进程的地址空间在逻辑上是相互隔离的,但在物理上却是重叠的。所谓的重叠是指同一块内存区域可能被多个进程同时使用。当调用 CreateFileMapping 创建命名的内存映射文件对象时,Windows 即在物理内存申请一块指定大小的内存区域,返回文件映射对象的句柄 hMap。为了能够访问这块内存区域必须调用 MapViewOfFile 函数,促使 Windows 将此内存空间映射到进程的地址空间中。当在其他进程访问这块内存区域时,则必须使用OpenFileMapping 函数取得对象句柄 hMap,并调用 MapViewOfFile 函数得到此内存空间的一个映射。这样一来,系统就把同一块内存区域映射到了不同进程的地址空间中,从而达到共享内存的目的。
写入端:
#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
using namespace std;
#define BUF_SIZE 256
TCHAR szName[] = TEXT("Test"); // 内存映射对象名称
struct stMemory
{
double dTranslationX;
double dTranslationY;
double dTranslationZ;
double dYaw;
double dCenterX;
double dCenterY;
double dCenterZ;
double dCenterAngle;
};//待写入的结构体
typedef stMemory * PST;//定义结构体指针
int p1();
int _tmain(int argc, _TCHAR* argv[])
{
p1();
return 0;
}
int p1()
{
HANDLE hMapFile;
stMemory st;
PST pBuf = &st; // 共享内存指针
//创建共享文件
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
szName); // name of mapping object
/*
调用CreateFileMapping的时候GetLastError的对应错误
ERROR_FILE_INVALID 如果企图创建一个零长度的文件映射, 应有此报
ERROR_INVALID_HANDLE 如果发现你的命名内存空间和现有的内存映射, 互斥量, 信号量, 临界区同名就麻烦了
ERROR_ALREADY_EXISTS 表示内存空间命名已经存在
*/
if (hMapFile == NULL)
{
int error = GetLastError();
_tprintf(TEXT("Could not create file mapping object (%d).\n"), error);
return 1;
}
// 映射对象的一个视图,得到指向共享内存的指针,设置里面的数据
pBuf = (PST)MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile);
return 1;
}
//从文件获取内容,写入共享内存
ifstream in("D:\\M.txt");
if (!in.is_open())
{
cout << "Error opening file"; exit(1);
}
cout << "2017-06-24" << endl;
while (1){
in.open("D:\\M.txt");
in >> pBuf->dTranslationX >> pBuf->dTranslationY >> pBuf->dTranslationZ >> pBuf->dYaw
>> pBuf->dCenterX >> pBuf->dCenterY >> pBuf->dCenterZ >> pBuf->dCenterAngle;
in.close();
}
in.close();
_getch();
//卸载内存映射文件地址指针
UnmapViewOfFile(pBuf);
//关闭内存映射文件
CloseHandle(hMapFile);
return 0;
}
读取端:
#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <iostream>
#include <atlconv.h>
#define BUF_SIZE 256
TCHAR szName[] = TEXT("Test");
struct stMemory
{
double dTranslationX;
double dTranslationY;
double dTranslationZ;
double dYaw;
double dCenterX;
double dCenterY;
double dCenterZ;
double dCenterAngle;
};
typedef stMemory * PST;
int p2();
int _tmain(int argc, _TCHAR* argv[])
{
p2();
return 0;
}
int p2()
{
HANDLE hMapFile;
PST pBuf = nullptr;
// 打开命名共享内存
hMapFile = OpenFileMapping(
FILE_MAP_READ, // read/write access
FALSE, // do not inherit the name
szName); // name of mapping object
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not open file mapping object (%d).\n"),
GetLastError());
return 1;
}
// 映射对象的一个视图,得到指向共享内存的指针,获取里面的数据
pBuf = (PST)MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_READ, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile);
return 1;
}
while (true)
{
std::cout << pBuf->dTranslationX << " " << pBuf->dTranslationY << " " << pBuf->dTranslationZ << " " << pBuf->dYaw
<< " " << pBuf->dCenterX << " " << pBuf->dCenterY << " " << pBuf->dCenterZ << " " << pBuf->dCenterAngle << std::endl;
}
_getch();
//卸载内存映射文件地址指针
UnmapViewOfFile(pBuf);
//关闭内存映射文件
CloseHandle(hMapFile);
return 0;
}
Windows 下进程的地址空间在逻辑上是相互隔离的,但在物理上却是重叠的。所谓的重叠是指同一块内存区域可能被多个进程同时使用。当调用 CreateFileMapping 创建命名的内存映射文件对象时,Windows 即在物理内存申请一块指定大小的内存区域,返回文件映射对象的句柄 hMap。为了能够访问这块内存区域必须调用 MapViewOfFile 函数,促使 Windows 将此内存空间映射到进程的地址空间中。当在其他进程访问这块内存区域时,则必须使用OpenFileMapping 函数取得对象句柄 hMap,并调用 MapViewOfFile 函数得到此内存空间的一个映射。这样一来,系统就把同一块内存区域映射到了不同进程的地址空间中,从而达到共享内存的目的。
写入端:
#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
using namespace std;
#define BUF_SIZE 256
TCHAR szName[] = TEXT("Test"); // 内存映射对象名称
struct stMemory
{
double dTranslationX;
double dTranslationY;
double dTranslationZ;
double dYaw;
double dCenterX;
double dCenterY;
double dCenterZ;
double dCenterAngle;
};//待写入的结构体
typedef stMemory * PST;//定义结构体指针
int p1();
int _tmain(int argc, _TCHAR* argv[])
{
p1();
return 0;
}
int p1()
{
HANDLE hMapFile;
stMemory st;
PST pBuf = &st; // 共享内存指针
//创建共享文件
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
szName); // name of mapping object
/*
调用CreateFileMapping的时候GetLastError的对应错误
ERROR_FILE_INVALID 如果企图创建一个零长度的文件映射, 应有此报
ERROR_INVALID_HANDLE 如果发现你的命名内存空间和现有的内存映射, 互斥量, 信号量, 临界区同名就麻烦了
ERROR_ALREADY_EXISTS 表示内存空间命名已经存在
*/
if (hMapFile == NULL)
{
int error = GetLastError();
_tprintf(TEXT("Could not create file mapping object (%d).\n"), error);
return 1;
}
// 映射对象的一个视图,得到指向共享内存的指针,设置里面的数据
pBuf = (PST)MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile);
return 1;
}
//从文件获取内容,写入共享内存
ifstream in("D:\\M.txt");
if (!in.is_open())
{
cout << "Error opening file"; exit(1);
}
cout << "2017-06-24" << endl;
while (1){
in.open("D:\\M.txt");
in >> pBuf->dTranslationX >> pBuf->dTranslationY >> pBuf->dTranslationZ >> pBuf->dYaw
>> pBuf->dCenterX >> pBuf->dCenterY >> pBuf->dCenterZ >> pBuf->dCenterAngle;
in.close();
}
in.close();
_getch();
//卸载内存映射文件地址指针
UnmapViewOfFile(pBuf);
//关闭内存映射文件
CloseHandle(hMapFile);
return 0;
}
读取端:
#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <iostream>
#include <atlconv.h>
#define BUF_SIZE 256
TCHAR szName[] = TEXT("Test");
struct stMemory
{
double dTranslationX;
double dTranslationY;
double dTranslationZ;
double dYaw;
double dCenterX;
double dCenterY;
double dCenterZ;
double dCenterAngle;
};
typedef stMemory * PST;
int p2();
int _tmain(int argc, _TCHAR* argv[])
{
p2();
return 0;
}
int p2()
{
HANDLE hMapFile;
PST pBuf = nullptr;
// 打开命名共享内存
hMapFile = OpenFileMapping(
FILE_MAP_READ, // read/write access
FALSE, // do not inherit the name
szName); // name of mapping object
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not open file mapping object (%d).\n"),
GetLastError());
return 1;
}
// 映射对象的一个视图,得到指向共享内存的指针,获取里面的数据
pBuf = (PST)MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_READ, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile);
return 1;
}
while (true)
{
std::cout << pBuf->dTranslationX << " " << pBuf->dTranslationY << " " << pBuf->dTranslationZ << " " << pBuf->dYaw
<< " " << pBuf->dCenterX << " " << pBuf->dCenterY << " " << pBuf->dCenterZ << " " << pBuf->dCenterAngle << std::endl;
}
_getch();
//卸载内存映射文件地址指针
UnmapViewOfFile(pBuf);
//关闭内存映射文件
CloseHandle(hMapFile);
return 0;
}