i007.cc

i007.cc

优先队列-降维打击

【Boost】Interprocess – 共享内存、文件映射介绍

【Boost】Interprocess – 共享内存、文件映射介绍


一、用法介绍

通过Interprocess,可以实现在共享内存、文件映射中保存vector、map等STL对象,并且可以使用自定义的类,官方文档介绍的也很详细了,下面是几个精简的示例。

  • 示例:基于文件映射的Map使用
[cpp] view plain copy

  1. #include <boost/interprocess/managed_mapped_file.hpp>
  2. #include <boost/interprocess/containers/map.hpp>
  3. #include <boost/interprocess/allocators/allocator.hpp>
  4. #include <functional>
  5. #include <cstdlib> //std::system
  6. #include <utility>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <exception>
  10. #include <sys/mman.h>
  11. #include <sys/stat.h>        /*  For mode constants */
  12. #include <fcntl.h>           /*  For O_* constants */
  13. #include <string>
  14. using namespace boost::interprocess;
  15. using std::string;
  16. class Item
  17. {
  18.     public:
  19.     Item(){}
  20.     ~Item(){}
  21.     int id;
  22.     int size;
  23.     string name;
  24. };
  25. typedef int KeyType;
  26. typedef Item MappedType;
  27. typedef std::pair<const int, Item> ValueType;
  28. typedef allocator<ValueType, managed_mapped_file::segment_manager> ShmemAllocator;
  29. typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MyMap;
  30. int main()
  31. {
  32.     try {
  33.         // init
  34.         managed_mapped_file segment(open_or_create, “SharedMemory”, 65536);
  35.         MyMap *mymap = segment.find<MyMap>(“MyMap”).first;
  36.         if (mymap == NULL)
  37.         {
  38.             const ShmemAllocator alloc_inst (segment.get_segment_manager());
  39.             mymap = segment.construct<MyMap>(“MyMap”) (std::less<int>(), alloc_inst);
  40.         }
  41.         Item v;
  42.         for(int i = 0; i < 100; ++i){
  43.             v.id = i;
  44.             mymap->insert(std::pair<const int, Item>(i, (Item)v));
  45.         }
  46.         for (MyMap::iterator it = mymap->begin(); it != mymap->end(); it++) {
  47.             printf(“%d “, it->second.id);
  48.         }
  49.         printf(“\n”);
  50.         //file_mapping::remove(“SharedMemory”);
  51.     }
  52.     catch (const std::exception & e) {
  53.         printf(“Exception:%s\n”, e.what());
  54.         //file_mapping::remove(“SharedMemory”);
  55.     }
  56.     return 0;
  57. }

执行后可以看到当前目录下已创建了内存文件。

[plain] view plain copy

  1. [root@SH-todo-1412181717 /home/derrywang/boost/boost_1_60_0/demo]# ./interprocess_map_file
  2. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  3. [root@SH-todo-1412181717 /home/derrywang/boost/boost_1_60_0/demo]# ls -al SharedMemory
  4. -rw-r–r– 1 root root 65536 Feb 17 18:54 SharedMemory
  • 示例:基于共享内存的Map使用
[cpp] view plain copy

  1. #include <boost/interprocess/managed_shared_memory.hpp>
  2. #include <boost/interprocess/managed_mapped_file.hpp>
  3. #include <boost/interprocess/containers/map.hpp>
  4. #include <boost/interprocess/allocators/allocator.hpp>
  5. #include <functional>
  6. #include <cstdlib>
  7. #include <utility>
  8. #include <sstream>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <exception>
  12. #include <sys/mman.h>
  13. #include <sys/stat.h>
  14. #include <fcntl.h>  
  15. #include <string>
  16. using namespace boost::interprocess;
  17. using std::string;
  18. class Item
  19. {
  20.     public:
  21.         Item(){}
  22.         ~Item(){}
  23.         int id;
  24.         int size;
  25.         string name;
  26. };
  27. typedef int KeyType;
  28. typedef Item MappedType;
  29. typedef std::pair<const int, Item> ValueType;
  30. typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;
  31. typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MyMap;
  32. int main()
  33. {
  34.     try {
  35.         // init
  36.         managed_shared_memory segment(create_only, “SharedMemory”, 65536);
  37.         const ShmemAllocator alloc_inst (segment.get_segment_manager());
  38.         MyMap * mymap = segment.construct<MyMap>(“MyMap”) (std::less<int>(), alloc_inst);
  39.         Item v;
  40.         for(int i = 0; i < 100; ++i){
  41.             v.id = i;
  42.             mymap->insert(std::pair<const int, Item>(i, (Item)v));
  43.         }
  44.         for (MyMap::iterator it = mymap->begin(); it != mymap->end(); it++) {
  45.             printf(“%d “, it->second.id);
  46.         }
  47.         printf(“\n”);
  48.         shared_memory_object::remove(“SharedMemory”);
  49.     }
  50.     catch (const std::exception & e) {
  51.         printf(“Exception:%s\n”, e.what());
  52.         shared_memory_object::remove(“SharedMemory”);
  53.     }
  54.     return 0;
  55. }
  • 示例:基于共享内存的Vector使用
[cpp] view plain copy

  1. #include <boost/interprocess/managed_shared_memory.hpp>
  2. #include <boost/interprocess/containers/vector.hpp>
  3. #include <boost/interprocess/allocators/allocator.hpp>
  4. #include <cstdlib> //std::system
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <exception>
  8. #include <sys/mman.h>
  9. #include <sys/stat.h>        /*  For mode constants */
  10. #include <fcntl.h>           /*  For O_* constants */
  11. #include <string>
  12. using namespace boost::interprocess;
  13. using std::string;
  14. class Item
  15. {
  16.     public:
  17.         Item(){}
  18.         ~Item(){}
  19.         int id;
  20.         int size;
  21.         string name;
  22. };
  23. typedef allocator<Item, managed_shared_memory::segment_manager>  ShmemAllocator;
  24. typedef vector<Item, ShmemAllocator> MyVector;
  25. int main()
  26. {
  27.     try {
  28.         // init
  29.         managed_shared_memory segment(create_only, “SharedMemory”, 65536);
  30.         const ShmemAllocator alloc_inst (segment.get_segment_manager());
  31.         MyVector *myvector = segment.construct<MyVector>(“MyVector”)(alloc_inst);
  32.         // push_back
  33.         Item v;
  34.         for(int i = 0; i < 100; i++) {
  35.             v.id = i;
  36.             myvector->push_back(v);
  37.             v.name = “hello”;
  38.         }
  39.         // loop
  40.         MyVector *myvector2 = segment.find<MyVector>(“MyVector”).first;
  41.         printf(“vector size:%d\n”, myvector2->size());
  42.         for (int j = 0; j < myvector2->size(); j++)
  43.             printf(“%d  “, (*myvector2)[j].id);
  44.         printf(“\n”);
  45.         shared_memory_object::remove(“SharedMemory”);
  46.     }
  47.     catch (const std::exception & e) {
  48.         printf(“Exception:%s\n”, e.what());
  49.         shared_memory_object::remove(“SharedMemory”);
  50.     }
  51.     return 0;
  52. }

Makefile如下,因为interprocess库不依赖库,所以头文件只需要包含boost顶层目录即可,仅依赖系统库编辑时需要指定-lrt.

[plain] view plain copy

  1. BIN = $(patsubst %.cpp,%,$(wildcard *.cpp))
  2. INC = -I ../
  3. LIB = -lrt
  4. RED = \\e[1m\\e[31m
  5. RESET = \\e[m
  6. GREEN = \\e[1m\\e[32m
  7. all:$(BIN)
  8. %:%.cpp
  9.         @echo -e “Make $(GREEN)$@$(RESET) begin……\c”
  10.         g++ -g -pthread -o $@ $< $(INC) $(LIB)
  11.         @echo -e $(RED)”ok.”$(RESET)
  12. clean:
  13.         rm $(BIN)
  14.         @echo “make clean done.”

二、生命周期说明

机制上和Linux系统是一致的,分为进程级(进程退出销毁)、内核级(系统重启销毁)、文件系统级(文件删除销毁),这里不再赘述,附上官方原文。
One of the biggest issues with interprocess communication mechanisms is the lifetime of the interprocess communication mechanism. It’s important to know when an interprocess communication mechanism disappears from the system. InBoost.Interprocess, we can have 3 types of persistence:

  • Process-persistence: The mechanism lasts until all the processes that have opened the mechanism close it, exit or crash.
  • Kernel-persistence: The mechanism exists until the kernel of the operating system reboots or the mechanism is explicitly deleted.
  • Filesystem-persistence: The mechanism exists until the mechanism is explicitly deleted.

Some native POSIX and Windows IPC mechanisms have different persistence so it’s difficult to achieve portability between Windows and POSIX native mechanisms.Boost.Interprocess classes have the following persistence:

Table 14.1. Boost.Interprocess Persistence Table

Mechanism Persistence
Shared memory Kernel or Filesystem
Memory mapped file Filesystem
Process-shared mutex types Process
Process-shared semaphore Process
Process-shared condition Process
File lock Process
Message queue Kernel or Filesystem
Named mutex Kernel or Filesystem
Named semaphore Kernel or Filesystem
Named condition Kernel or Filesystem
个人分类: C++

发表回复