【C++】使用共享内存实现进程间通信


// ServerCom.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <stdio.h>
#include <windows.h>
#pragma endregion
#define MAP_PREFIX          L"Local\\"
#define MAP_NAME            L"SampleMap"
#define FULL_MAP_NAME       MAP_PREFIX MAP_NAME

// Max size of the file mapping object.
#define MAP_SIZE            65536

// File offset where the view is to begin.
#define VIEW_OFFSET         0

// The number of bytes of a file mapping to map to the view. All bytes of the 
// view must be within the maximum size of the file mapping object (MAP_SIZE). 
// If VIEW_SIZE is 0, the mapping extends from the offset (VIEW_OFFSET) to  
// the end of the file mapping.
#define VIEW_SIZE           1024

// Unicode string message to be written to the mapped view. Its size in byte 
// must be less than the view size (VIEW_SIZE).
#define MESSAGE             L"Message from the first process."


int _tmain(int argc, _TCHAR* argv[])
{
	HANDLE hMapFile = NULL;
	PVOID pView = NULL;

	// Create the file mapping object.
	hMapFile = CreateFileMapping(
		INVALID_HANDLE_VALUE,   // Use paging file - shared memory
		NULL,                   // Default security attributes
		PAGE_READWRITE,         // Allow read and write access
		0,                      // High-order DWORD of file mapping max size
		MAP_SIZE,               // Low-order DWORD of file mapping max size
		FULL_MAP_NAME           // Name of the file mapping object
		);
	if (hMapFile == NULL) 
	{
		wprintf(L"CreateFileMapping failed w/err 0x%08lx\n", GetLastError());
		goto Cleanup;
	}
	wprintf(L"The file mapping (%s) is created\n", FULL_MAP_NAME);

	// Map a view of the file mapping into the address space of the current 
	// process.
	pView = MapViewOfFile(
		hMapFile,               // Handle of the map object
		FILE_MAP_ALL_ACCESS,    // Read and write access
		0,                      // High-order DWORD of the file offset 
		VIEW_OFFSET,            // Low-order DWORD of the file offset 
		VIEW_SIZE               // The number of bytes to map to view
		);
	if (pView == NULL)
	{ 
		wprintf(L"MapViewOfFile failed w/err 0x%08lx\n", GetLastError()); 
		goto Cleanup;
	}
	wprintf(L"The file view is mapped\n");

	// Prepare a message to be written to the view.
	PWSTR pszMessage = MESSAGE;
	DWORD cbMessage = (wcslen(pszMessage) + 1) * sizeof(*pszMessage);

	// Write the message to the view.
	memcpy_s(pView, VIEW_SIZE, pszMessage, cbMessage);

	wprintf(L"This message is written to the view:\n\"%s\"\n",
		pszMessage);

	// Wait to clean up resources and stop the process.
	wprintf(L"Press ENTER to clean up resources and quit");
	getchar();

Cleanup:

	if (hMapFile)
	{
		if (pView)
		{
			// Unmap the file view.
			UnmapViewOfFile(pView);
			pView = NULL;
		}
		// Close the file mapping object.
		CloseHandle(hMapFile);
		hMapFile = NULL;
	}

	return 0;
}



// ClientCom.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#pragma endregion
#define MAP_PREFIX          L"Local\\"
#define MAP_NAME            L"SampleMap"
#define FULL_MAP_NAME       MAP_PREFIX MAP_NAME

// File offset where the view is to begin.
#define VIEW_OFFSET         0

// The number of bytes of a file mapping to map to the view. All bytes of the 
// view must be within the maximum size of the file mapping object. If 
// VIEW_SIZE is 0, the mapping extends from the offset (VIEW_OFFSET) to the 
// end of the file mapping.
#define VIEW_SIZE           1024


int _tmain(int argc, _TCHAR* argv[])
{
	HANDLE hMapFile = NULL;
	PVOID pView = NULL;

	// Try to open the named file mapping identified by the map name.
	hMapFile = OpenFileMapping(
		FILE_MAP_READ,          // Read access
		FALSE,                  // Do not inherit the name
		FULL_MAP_NAME           // File mapping name 
		);
	if (hMapFile == NULL) 
	{
		wprintf(L"OpenFileMapping failed w/err 0x%08lx\n", GetLastError());
		goto Cleanup;
	}
	wprintf(L"The file mapping (%s) is opened\n", FULL_MAP_NAME);

	// Map a view of the file mapping into the address space of the current 
	// process.
	pView = MapViewOfFile(
		hMapFile,               // Handle of the map object
		FILE_MAP_READ,          // Read access
		0,                      // High-order DWORD of the file offset 
		VIEW_OFFSET,            // Low-order DWORD of the file offset
		VIEW_SIZE               // The number of bytes to map to view
		);
	if (pView == NULL)
	{
		wprintf(L"MapViewOfFile failed w/err 0x%08lx\n", GetLastError()); 
		goto Cleanup;
	}
	wprintf(L"The file view is mapped\n");

	// Read and display the content in view.
	wprintf(L"Read from the file mapping:\n\"%s\"\n", (PWSTR)pView);

	// Wait to clean up resources and stop the process.
	wprintf(L"Press ENTER to clean up resources and quit");
	getchar();

Cleanup:

	if (hMapFile)
	{
		if (pView)
		{
			// Unmap the file view.
			UnmapViewOfFile(pView);
			pView = NULL;
		}
		// Close the file mapping object.
		CloseHandle(hMapFile);
		hMapFile = NULL;
	}

	return 0;
}


### 回答1: 共享内存是一种进程间通信的方式。它允许多个进程访问相同的内存区域,从而实现数据的共享和传输。 在使用共享内存进行进程间通信时,首先需要申请一块共享内存区域,并将其映射到各个进程的地址空间中。这样,所有进程就可以通过读写该内存区域来进行数据的传递和共享。由于共享内存操作的是实际的内存地址,相比其他进程间通信方式,如管道或消息队列,共享内存具有更高的传输效率。 共享内存通信的一个重要问题是同步与互斥。多个进程同时对共享内存进行读写操作时,需要通过互斥手段来避免竞态条件和数据不一致的问题。常用的同步机制包括信号量、互斥锁等。通过使用这些同步机制,进程可以获得对共享内存的独占访问,避免数据冲突。 共享内存通信在某些场景下非常有用,例如多个进程需要共享大量数据、频繁进行数据交换的场合。通过共享内存,可以避免数据复制和编码解码等操作,有效提高系统的性能。 然而,共享内存通信也存在一些问题。首先,由于多个进程可以直接访问该内存区域,因此必须确保进程之间的协调和同步。另外,共享内存具有共享性,一旦出现错误或者异常行为,会影响到所有依赖于该内存区域的进程。 综上所述,共享内存是一种高效的进程间通信方式,可以提供快速的数据传输和共享功能。然而,在使用共享内存通信时,需要注意协调和同步措施,以确保数据的一致性和正确性。 ### 回答2: 共享内存是一种进程间通信的方法,它允许多个进程同时访问同一块内存区域。在使用共享内存进行进程间通信时,多个进程可以通过读写同一块内存来交换数据,从而实现进程间的数据共享。 共享内存实现通常借助于操作系统提供的相关API,例如Linux系统提供了shmget、shmat、shmdt和shmctl等函数,用于创建和控制共享内存区域。 使用共享内存进行进程间通信的优势在于可以实现高效的数据传输,因为数据在内存中的复制效率比较高。而且,由于多个进程可以同时访问同一块内存区域,这种方式也能够更好地支持并发操作。 然而,共享内存也存在一些潜在的问题。首先,由于多个进程可以同时访问共享内存,所以在使用时需要注意对共享资源的互斥保护,以避免数据的竞争和冲突。其次,共享内存使用过程中需要确保数据的一致性和同步,否则可能会导致数据错误或者进程间的死锁。最后,共享内存使用需要仔细考虑安全性问题,以避免恶意进程的非法访问和篡改。 总之,共享内存是一种高效的进程间通信方式,能够实现进程间的数据共享。但在使用时需要注意互斥保护、数据一致性和安全性等问题,确保进程间通信的稳定和可靠性。 ### 回答3: 共享内存是一种用于进程间通信的机制。它可以使多个进程在同一时间访问相同的内存区域,从而实现数据的共享和传递。 在使用共享内存进行进程间通信时,首先需要创建一个共享内存段。多个进程可以通过系统调用(如shmget)来获取这个共享内存段的标识符,以便能够访问它。通过访问这个标识符,进程可以将共享内存映射到自己的地址空间,从而可以对内存进行读写操作。 进程可以通过访问共享内存中的数据来实现通信。可以在共享内存中定义一些共享数据结构,进程可以通过读取和修改这些数据结构来进行通信。由于多个进程可以同时访问共享内存,因此需要对共享内存的访问进行同步和互斥操作,以避免数据竞争等问题。 共享内存使用具有一定的优点和缺点。它的优点是速度快,因为进程可以直接访问内存,无需经过复制和传输等额外开销。同时,由于数据是直接在内存中共享,所以多个进程之间可以实现高效的数据交换和传递。然而,共享内存也有一些缺点,比如需要手动进行同步和互斥操作,否则可能会导致数据不一致等问题。此外,共享内存使用需要进程之间具有一定的协作性,否则可能会导致竞争和死锁等问题。 总的来说,共享内存是一种高效的进程间通信机制,可以在多个进程之间共享数据,从而实现数据的传递和交换。但是,需要注意同步和互斥操作,以及进程之间的协作性问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值