最近看侯捷的《Win32多线程程序设计》,学习了一下共享内存,遂决定写一个小例子练练手。这个程序包括一个客户端和一个服务端,服务端创建共享内存,客户端打开共享内存,两者通过两个事件互斥访问共享内存,实现一个小功能,就是服务端进程从控制台读入数据发送给客户端进程。
服务端代码:
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
HANDLE hMutex = NULL;
HANDLE hFileMapping = NULL;
LPVOID lpShareMemory = NULL;
HANDLE hServerWriteOver = NULL;
HANDLE hClientReadOver = NULL;
//create share memory
hFileMapping = CreateFileMapping(INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
1024*1024,
L"ShareMemoryTest");
if (NULL == hFileMapping)
{
cout << "CreateFileMapping fail:" << GetLastError() << endl;
goto SERVER_SHARE_MEMORY_END;
}
lpShareMemory = MapViewOfFile(hFileMapping,
FILE_MAP_ALL_ACCESS,
0,
0, //memory start address
0); //all memory space
if (NULL == lpShareMemory)
{
cout << "MapViewOfFile" << GetLastError() << endl;
goto SERVER_SHARE_MEMORY_END;
}
//373
hMutex = CreateMutex(NULL, FALSE, L"SM_Mutex");
if (NULL == hMutex || ERROR_ALREADY_EXISTS == GetLastError())
{
cout << "CreateMutex" << GetLastError() << endl;
goto SERVER_SHARE_MEMORY_END;
}//多个线程互斥访问
//send data
hServerWriteOver = CreateEvent(NULL,
TRUE,
FALSE,
L"ServerWriteOver");
hClientReadOver = CreateEvent(NULL,
TRUE,
FALSE,
L"ClientReadOver");
if (NULL == hServerWriteOver ||
NULL == hClientReadOver)
{
cout << "CreateEvent" << GetLastError() << endl;
goto SERVER_SHARE_MEMORY_END;
}
char p = 0;
char* q = (char*)lpShareMemory;
do
{
p = getchar();
if (WaitForSingleObject(hClientReadOver, 5*1000) != WAIT_OBJECT_0)
goto SERVER_SHARE_MEMORY_END;
q[0] = p;
if (!ResetEvent(hClientReadOver)) goto SERVER_SHARE_MEMORY_END;
if (!SetEvent(hServerWriteOver)) goto SERVER_SHARE_MEMORY_END;
} while (p != '\n');
SERVER_SHARE_MEMORY_END:
//release share memory
if (NULL != hServerWriteOver) CloseHandle(hServerWriteOver);
if (NULL != hClientReadOver) CloseHandle(hClientReadOver);
if (NULL != lpShareMemory) UnmapViewOfFile(lpShareMemory);
if (NULL != hFileMapping) CloseHandle(hFileMapping);
if (NULL != hMutex) ReleaseMutex(hMutex);
return 0;
}
客户端的代码:
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
HANDLE hMutex = NULL;
HANDLE hFileMapping = NULL;
LPVOID lpShareMemory = NULL;
HANDLE hServerWriteOver = NULL;
HANDLE hClientReadOver = NULL;
hMutex = OpenMutex(MUTEX_ALL_ACCESS,
FALSE,
L"SM_Mutex");
if (NULL == hMutex)
{
if (ERROR_FILE_NOT_FOUND == GetLastError())
{
cout << "OpenMutex fail: file not found!" << endl;
goto CLIENT_SHARE_MEMORY_END;
}
else
{
cout << "OpenMutex fail:" << GetLastError() << endl;
goto CLIENT_SHARE_MEMORY_END;
}
}
if (WaitForSingleObject(hMutex, 5000) != WAIT_OBJECT_0)
{
DWORD dwErr = GetLastError();
goto CLIENT_SHARE_MEMORY_END;
}
//open share memory
hFileMapping = OpenFileMapping(FILE_MAP_ALL_ACCESS,
FALSE,
L"ShareMemoryTest");
if (NULL == hFileMapping)
{
cout << "OpenFileMapping" << GetLastError() << endl;
goto CLIENT_SHARE_MEMORY_END;
}
lpShareMemory = MapViewOfFile(hFileMapping,
FILE_MAP_ALL_ACCESS,
0,
0,
0);
if (NULL == lpShareMemory)
{
cout << "MapViewOfFile" << GetLastError() << endl;
goto CLIENT_SHARE_MEMORY_END;
}
//read and write data
hServerWriteOver = CreateEvent(NULL,
TRUE,
FALSE,
L"ServerWriteOver");
hClientReadOver = CreateEvent(NULL,
TRUE,
FALSE,
L"ClientReadOver");
if (NULL == hServerWriteOver ||
NULL == hClientReadOver)
{
cout << "CreateEvent" << GetLastError() << endl;
goto CLIENT_SHARE_MEMORY_END;
}
char p = 0;
char* q = (char*)lpShareMemory;
do
{
if (!SetEvent(hClientReadOver))
goto CLIENT_SHARE_MEMORY_END;
if (WaitForSingleObject(hServerWriteOver, INFINITE) != WAIT_OBJECT_0)
goto CLIENT_SHARE_MEMORY_END;
p = q[0];
putchar(p);
if (!ResetEvent(hServerWriteOver))
goto CLIENT_SHARE_MEMORY_END;
} while (p != '\n');
CLIENT_SHARE_MEMORY_END:
//release share memory
if (NULL != hServerWriteOver) CloseHandle(hServerWriteOver);
if (NULL != hClientReadOver) CloseHandle(hClientReadOver);
if (NULL != lpShareMemory) UnmapViewOfFile(lpShareMemory);
if (NULL != hFileMapping) CloseHandle(hFileMapping);
if (NULL != hMutex) ReleaseMutex(hMutex);
return 0;
}