⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 ACE的共享内存操作
💻 CPP
字号:
#include <ace/ACE.h>
#include <ace/OS.h>
#include <ace/Shared_Memory_MM.h>
#include <ace/Malloc.h>
#include <ace/Malloc_T.h>
#include "ace/MMAP_Memory_Pool.h"
#include "ace/Process_Mutex.h"

#define DATA_SIZE 100
#define MESSAGE1 "Hiya over there client process"
#define MESSAGE2 "Did you hear me the first time?"
char* poolname="321123";

typedef ACE_Malloc<ACE_MMAP_MEMORY_POOL, ACE_Process_Mutex> Malloc_Allocator;

static void server (void)
{
	
	//Create the memory allocator passing it the shared memory
	//pool that you want to use
	Malloc_Allocator shm_allocator(poolname);
	//Create a message, allocate memory for it and bind it with
	//a name so that the client can the find it in the memory
	//pool
	
	char* Message1=(char*)shm_allocator.malloc(strlen(MESSAGE1));
	ACE_OS::strcpy(Message1,MESSAGE1);
	shm_allocator.bind("FirstMessage",Message1);
	ACE_DEBUG((LM_DEBUG,"<<%s\n",Message1));

	//How about a second message
	char* Message2=(char*)shm_allocator.malloc(strlen(MESSAGE2));
	ACE_OS::strcpy(Message2,MESSAGE2);
	shm_allocator.bind("SecondMessage",Message2);
	
	ACE_DEBUG((LM_DEBUG,"<<%s\n",Message2));
	//Set the Server to go to sleep for a while so that the client has
	//a chance to do its stuff
	ACE_DEBUG((LM_DEBUG,
		"Server done writing.. going to sleep zzz..\n\n\n"));
	ACE_OS::sleep(2);

	shm_allocator.remove();
}

static void client(void)
{
	//Create a memory allocator. Be sure that the client passes
	// in the "right" name here so that both the client and the
	//server use the same memory pool. We wouldn’t want them to
	// BOTH create different underlying pools.
	Malloc_Allocator shm_allocator(poolname);
	//Get that first message. Notice that the find is looking up the
	//memory based on the "name" that was bound to it by the server.
	void *Message1;
	if(shm_allocator.find("FirstMessage", Message1)==-1)
	{
		ACE_ERROR((LM_ERROR,
			"Client: Problem cant find data that server has sent\n"));
		ACE_OS::exit(1);
	}

}

int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
	server();
	//client();

	//ACE_OS::sleep(1000);
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -