boost学习之共享内存练习

来自一位爱吃青菜的程序猿。不喜欢请绕道。有问题请指出,希望多多交流。话不多说,上题。

原文题目是:创建一个通过共享内存通讯的客户端/服务器应用程序。 文件名称应该作为命令行参数传递给客户端应用程序。 这个文件存储在服务器端应用程序启动的目录下,这个文件应经由共享内存发送给服务器端应用程序:

客户端

#include<fstream>
//匿名mutex对象  将mutex存储于共享内存中,可以被所有程序访问
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/managed_shared_memory.hpp> 
//分配器
#include <boost/interprocess/allocators/allocator.hpp>
namespace syc = boost::interprocess;
void Client_Write()
{
	ofstream OutFile("Test.txt", ios::trunc);
	OutFile << "This is a Test12!";
	OutFile.close();

	syc::shared_memory_object::remove("syc_file_Memory");
	syc::managed_shared_memory syc_file(syc::open_or_create, "syc_file_Memory",1024);
	typedef syc::allocator<char, syc::managed_shared_memory::segment_manager> CharAllocator;
	typedef syc::basic_string<char, std::char_traits<char>, CharAllocator> String;


	//把文件以String形式放入内存
	String* str = syc_file.find_or_construct<String>("str")(syc_file.get_segment_manager());

	//定义参数类型
	String* pParam = syc_file.find_or_construct<String>("pParam")(syc_file.get_segment_manager());

	//把锁放入内存
	syc::interprocess_mutex *imtx = syc_file.find_or_construct<syc::interprocess_mutex>("imtx")();
	while (1)
	{
		std::cout << "参数:" << *pParam << std::endl;

		if (*pParam == "Txt")
		{
			//此过程 上锁
			imtx->lock();
			//open txt
			ifstream readFile("Test.txt");
			std::string TxtInfo;
			while (getline(readFile, TxtInfo));
			readFile.close();

			boost::format fmt("%1%");
			fmt%TxtInfo;

			*str = fmt.str().c_str();
			std::cout << "写入数据:" << *str << std::endl;
			imtx->unlock();
			*pParam = "Write_end";
		}
		if (*pParam == "exit")
			break;
	}

}

服务端

需要注意的是,客户端得先启动,不然服务器会打不开共享内存而退出。本文章没有做处理。

//头文件还是那几个 
#include <boost/interprocess/managed_shared_memory.hpp> 
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include<fstream>
int main()
{
	namespace syc = boost::interprocess;
	syc::managed_shared_memory syc_shm(syc::open_only, "syc_file_Memory");
	std::cout << "链接后地址:" << syc_shm.get_address() << std::endl;
	typedef syc::allocator<char, syc::managed_shared_memory::segment_manager> CharAllocator;
	typedef syc::basic_string<char, std::char_traits<char>, CharAllocator> String;
	typedef std::pair<String*, syc::managed_shared_memory::size_type> syc_pair_String;
	typedef std::pair<syc::interprocess_mutex*, syc::managed_shared_memory::size_type> syc_pair_mutex;

	//共享内存中的 内容
	syc_pair_String spi_value1 = syc_shm.find<String>("str");
	//共享内存中的 参数
	syc_pair_String spi_pParam = syc_shm.find<String>("pParam");
	//共享内存中的 锁
	syc_pair_mutex mex = syc_shm.find<syc::interprocess_mutex>("imtx");

	if (!spi_value1.second)
	{
		std::cout << "could not find spi_value1 instance" << std::endl;
		return 0;
	}
	if (!spi_pParam.second)
	{
		std::cout << "could not find spi_mutex instance" << std::endl;
		return 0;
	}
	if (!mex.second)
	{
		std::cout << "could not find mex instance" << std::endl;
		return 0;
	}

	//向共享内存传递参数
	*spi_pParam.first = "Txt";



	while (1)
	{
		if (*spi_pParam.first == "Write_end")
		{
			//写入过程 上锁
			(*mex.first).lock();
			//获取数据内容部分 并写入文件
			ofstream OutFile("Test.txt", ios::trunc);

			std::cout << "内容:" << *spi_value1.first << std::endl;
			OutFile << (*spi_value1.first).c_str();
			OutFile.close();

			(*mex.first).unlock();
			*spi_pParam.first = "";
			break;
		}

	}


	//发送退出参数 ,结束会话
	*spi_pParam.first = "exit";

	//检查内存是否已删除
	std::cout << "删除:" << syc::shared_memory_object::remove("syc_file_Memory") << std::endl;
	::system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值