boost进程间通信常用开发一篇全(消息队列,共享内存,信号)

130 篇文章 0 订阅
78 篇文章 0 订阅

本文概要:

        敏捷开发大家想必知道而且评价甚高,缩短开发周期,提高开发质量。将大工程独立为不同的小app开发,整个开发过程,程序可用可测,所以提高了整体的质量。基于这种开发模式和开发理念,进程间通信必然是童鞋们必掌握技能之一了,而boost库是众多库中平台支持性很好,效果很高之一。做嵌入式或者服务器等应用的人肯定有所涉及。本文以手册方式讲述boost共享内存,信号,以及消息队列的编程方式。很简单,列出最常用用法,供大家拷贝直接使用。

应用思路注意事项:

         信号是进程内通信,非常类似于Qt的信号槽,配合消息队列以及boost多线程使用效果很好。

共享内存:

#include <boost/interprocess/shared_memory_object.hpp>  
#include <boost/interprocess/mapped_region.hpp>  
#include <cstring>  
#include <cstdlib>  
#include <string>  
   
int main(int argc, char *argv[])  
{  
   using namespace boost::interprocess;  
   
   if(argc == 1){  //Parent process  
      //Remove shared memory on construction and destruction  
      struct shm_remove  
      {  
         shm_remove() { shared_memory_object::remove("MySharedMemory"); }  
         ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }  
      } remover;  
   
      //Create a shared memory object.  
      shared_memory_object shm (create_only, "MySharedMemory", read_write);  
   
      //Set size  
      shm.truncate(1000);  
   
      //Map the whole shared memory in this process  
      mapped_region region(shm, read_write);  
   
      //Write all the memory to 1  
      std::memset(region.get_address(), 1, region.get_size());  
   
      //Launch child process  
      std::string s(argv[0]); s += " child ";  
      if(0 != std::system(s.c_str()))  
         return 1;  
   }  
   else{  
      //Open already created shared memory object.  
      shared_memory_object shm (open_only, "MySharedMemory", read_only);  
   
      //Map the whole shared memory in this process  
      mapped_region region(shm, read_only);  
   
      //Check that memory was initialized to 1  
      char *mem = static_cast<char*>(region.get_address());  
      for(std::size_t i = 0; i < region.get_size(); ++i)  
         if(*mem++ != 1)  
            return 1;   //Error checking memory  
   }  
   return 0;  
} 

信号通信:

贴出一个常用带参数的写法,详细的其他用法可以参考文章末的参考文章:

#include <boost/signal.hpp>
 
#include <boost/thread/thread.hpp>
 
#include <boost/date_time/posix_time/posix_time.hpp>
 
#include <iostream>
 
 
 
using namespace std;
 
using namespace boost;
 
 
 
float print_sum(float x, float y)
 
{
 
  std::cout << "The sum is " << x+y << std::endl;
 
  return x+y;
 
}
 
 
 
float print_product(float x, float y)
 
{
 
  std::cout << "The product is " << x*y << std::endl;
 
  return x*y;
 
}
 
 
 
float print_difference(float x, float y)
 
{
 
  std::cout << "The difference is " << x-y << std::endl;
 
  return x-y;
 
}
 
 
 
float print_quotient(float x, float y)
 
{
 
  std::cout << "The quotient is " << x/y << std::endl;
 
  return x/y;
 
}
 
 
 
int main()
 
{
 
  boost::signal<float (float , float )> sig;
 
  sig.connect(0, &print_sum);
 
  sig.connect(1, &print_product);
 
  sig.connect(2, &print_difference);
 
  sig.connect(3, &print_quotient);
 
  // Output 1.6667 because return by the last slot called.
 
  cout << sig(5, 3) << endl;   
 
  return 0;
 
}


信号槽删除及阻塞:

Seg 1: Disconnecting slots.
 
        boost::signals::connection c = sig.connect(HelloWorld());
 
        if (c.connected()) {
 
        // c is still connected to the signal
 
        sig(); // Prints "Hello, World!"
 
        }
 
        c.disconnect(); // Disconnect the HelloWorld object
 
        assert(!c.connected()); //c isn't connected any more
 
        sig(); // Does nothing: there are no connected slots
 
Seg 2:
 
        boost::signals::connection c = sig.connect(HelloWorld());
 
        sig(); // Prints "Hello, World!"
 
 
 
        c.block(); // block the slot
 
        assert(c.blocked());
 
        sig(); // No output: the slot is blocked
 
 
 
        c.unblock(); // unblock the slot
 
        sig(); // Prints "Hello, World!"

消息队列:

消息队列发送:

#include <boost/interprocess/ipc/message_queue.hpp>
#include <iostream>
#include <vector>
 
using namespace boost::interprocess;
 
int main ()
{
   try{
      //Erase previous message queue
      message_queue::remove("message_queue");
 
      //Create a message_queue.
      message_queue mq
         (create_only               //only create
         ,"message_queue"           //name
         ,100                       //max message number
         ,sizeof(int)               //max message size
         );
 
      //Send 100 numbers
      for(int i = 0; i < 100; ++i){
         mq.send(&i, sizeof(i), 0);
      }
 
 
   }
   catch(interprocess_exception &ex){
      std::cout << ex.what() << std::endl;
      return 1;
   }
 
   return 0;
}


消息队列接收:

#include <boost/interprocess/ipc/message_queue.hpp>
#include <iostream>
#include <vector>
 
using namespace boost::interprocess;
 
int main ()
{
   try{
      //Open a message queue.
      message_queue mq
         (open_only        //only create
         ,"message_queue"  //name
         );
 
      unsigned int priority;
      message_queue::size_type recvd_size;
 
      //Receive 100 numbers
      for(int i = 0; i < 100; ++i)
	  {
         int number;
         mq.receive(&number, sizeof(number), recvd_size, priority);
		 printf("I:%d Rec:%d\n",i,number);
         if(number != i || recvd_size != sizeof(number))
            return 1;
      }
   }
   catch(interprocess_exception &ex){
      message_queue::remove("message_queue");
      std::cout << ex.what() << std::endl;
      return 1;
   }
   message_queue::remove("message_queue");
   return 0;
}

编译命令:

  1. [root@localhost tmp]# g++ boost_queue_send.cpp -o queue_send -lboost_thread -lboost_system

  2. [root@localhost tmp]# g++ boost_queue_rec.cpp -o queue_rec -lboost_thread -lboost_system


 

将消息队列整理了一下,可以直接方便使用如下:

注(BoostMsg.h)用于将函数外部化,无其他用处。

#include <BoostMsg.h>
 
#include <boost/interprocess/ipc/message_queue.hpp>  
#include <iostream>  
#include <vector>  
 
using namespace boost::interprocess;  
 
int BoostMsg_CreateMsg(const char* MsgQueName,int MaxQueLen,int MaxMsgLen)
{
	message_queue::remove(MsgQueName);  
	//Create a message_queue.  
	message_queue mq  
		(create_only               //only create  
		,MsgQueName						//name  
		,MaxQueLen                       //max message number  
		,MaxMsgLen              //max message size  
		);  
 
	printf("Boost Msg Created Name:%s QueLen:%d MsgMaxLen:%d \n",MsgQueName,MaxQueLen,MaxMsgLen);
 
	return 0;
}
 
int BoostMsg_OpenMsgToSend(char* BufMsg,const char* MsgQueName,int MsgLen)
{  
	try{  
		//Erase previous message queue  
		//message_queue::remove("message_queue");  
		//Create a message_queue.  
		message_queue mq  
			(open_only               //only create  
			,MsgQueName						//name  
			);  
 
		mq.send(BufMsg, MsgLen, 0);  
	}  
	catch(interprocess_exception &ex){  
		std::cout << ex.what() << std::endl;  
		return 1;  
	}  
	return 0;  
} 
 
 
 
int BoostMsg_OpenMsgToRec(char* RecivedMsg,const char* MsgQueName,int MaxReceivedLen)
{  
	try{  
		//Open a message queue.  
		message_queue mq  
			(open_only        //only create  
			,MsgQueName  //name  
			);  
 
		unsigned int priority;  
		message_queue::size_type recvd_size;  
 
		memset(RecivedMsg,0,MaxReceivedLen);
		mq.receive(RecivedMsg, MaxReceivedLen, recvd_size, priority);  
		return recvd_size;
 
	}  
	catch(interprocess_exception &ex){  
		message_queue::remove("message_queue");  
		std::cout << ex.what() << std::endl;  
		return 1;  
	}  
	message_queue::remove("message_queue");  
	return 0;  
}  

使用方法很简单,就是Create 然后Send 和Rec,进程之间通信 很方便。

 

参考文章:

Boost.Interprocess使用手册翻译之四:在进程间共享内存 (Sharing memory between processes)

http://blog.csdn.net/great3779/article/details/7226388

Windows多进程编程

http://blog.csdn.net/bxhj3014/article/details/2082255

如何使用BOOST信号(一)

http://blog.csdn.net/liuchangyu23/article/details/4584045

Boost.Interprocess 强大的进程间通讯库

http://blog.csdn.net/linkerlin/article/details/2249906

如何使用BOOST信号(二)

http://blog.csdn.net/liuchangyu23/article/details/4584346

  • 0
    点赞
  • 4
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值