使用共享内存进行进程间通信

最近看侯捷的《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;
}


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值