Home

OFF-SOFT.net

OFF-SOFT.net

This site is support & information site of WEB,and Software. This site might help you that create software or Web Site…perhaps?[:]

Boost(3) Use of MemmoryMap.

Published on| September 9th, 2009 | No Comment.
Summary:
This article is memory mapped (shared memory (Shared Memory)) to try to use the Boost.
Memory map, WIndows does, API is provided so that, fine, are used. Same behavior to Boost in the memory map, what do I write what I want to describe briefly.

Does, immediately, try using the sample source.
Program used here, Boost easy to understand and the sample source, some are changed.


Original sample source:


Sample source code you used here:

Try console application
To ensure easy operation, the original sample above, only a little, I tried to change to make it easier to understand.
In, I immediately look at the source code.

[sample.cpp]
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
#include <iostream>
#include <string>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib> //std::system
#include <sstream>
 
using namespace std;
using namespace boost;
using namespace boost::interprocess;
 
//	const data
const char * MMMAP_NAME = "MySharedMemory";
int max_share_size = 65536;
int real_share_size = 1024;
 
int main (int argc, char *argv[])
{
	if(argc == 1){ 
		shared_memory_object::remove(MMMAP_NAME);
		// Create a managed shared memory segment
		managed_shared_memory segment(create_only, MMMAP_NAME, max_share_size);
 
		// Allocate a portion of the segment (raw memory)
		std::size_t free_memory = segment.get_free_memory();
		void * shptr = segment.allocate(real_share_size);
 
		// Check invariant
		if(free_memory > segment.get_free_memory()){
 
			char *pbuff=(char *)shptr;
			pbuff[0]='A';
			pbuff[1]='B';
			pbuff[2]='C';
			pbuff[3]=NULL;
 
			// convert memory to handle
			managed_shared_memory::handle_t handle = segment.get_handle_from_address(shptr);
			// set of child process param .
			std::stringstream s;
			s << argv[0] << " " << handle;
			s << std::ends;
			cout << "handle:"  << handle << endl;
			// Launch child process
			int nchild = std::system(s.str().c_str());
 
			cout << "child return:"  << nchild << endl;
			cout << "changed?:"<< pbuff << endl;
 
			//	delete share memory
			segment.deallocate(pbuff);
 
			string sdata;
			cin >> sdata ;	//	any key input!! --> exit.
 
			//	delete share memory
			shared_memory_object::remove(MMMAP_NAME);
 
		} else {
			//	delete share memory
			shared_memory_object::remove(MMMAP_NAME);
 
			return 1;
		}
	} else {
		//Open managed segment
		managed_shared_memory segment(open_only, MMMAP_NAME);
 
		//An handle from the base address can identify any byte of the shared 
		//memory segment even if it is mapped in different base addresses
		managed_shared_memory::handle_t handle = 0;
 
		//Obtain handle value
		std::stringstream s; s << argv[1]; s >> handle;
 
		// Get buffer local address from handle
		void *msg = segment.get_address_from_handle(handle);
 
		char *pbuff=(char *)msg;
 
		cout << "origin:" << pbuff << endl;
 
		//	change data!!
		pbuff[0]='D';
		pbuff[1]='E';
		pbuff[2]='F';
		pbuff[3]=NULL;
 
	}
	return 0;
}

This program, when invoked without arguments, the child automatically (the same program: Handle is specified in the argument) that the start of the memory map information, and can be tried in both reading and writing process.

Does, try a simple explanation.
Line 19, you can not use the same name as the memory map, memory map if its name has been removed.
Line 21 is to create a new memory map. The argument specifies the name and maximum memory size.
Line 24 is to determine the size of the memory map of memory space you created.
Line 25 is a memory map was created, and really use space.
Line 28 is created in the memory map, and check the difference between real space and used space.
More space, if it worked out really use a larger area has been considered abnormal.

30 from the first line, and write information to memory-mapped area.
Line 37 is to obtain information to pass the information to handle the child.
Line 44 is to start a child process.


66 from the line is the process of the child.
Here, open the memory map, arguments received from the handle, and get the address of the region. (Up to 76 lines)
In line 80, to output the actual content, make sure you have read the information.
83 from the first line, now has to write.

Parent process, in line 47, can read and verify the information that has changed.


In fact, try to run and displays the results following its execution.

1
2
3
4
handle:96
origin:ABC
child return:0
changed?:DEF

As I could not read or write.


Boost does so, it uses the information to recognize the memory handle. Become of the need to pass information to handle, the other for inter-process communication, it is a little inconvenient.
Windows only as a fixed string of memory map, so you can read and write to will to do.

Properly, it is also available. Boost does is designed to interact with the object.

25, the first line, as follows to change.
1
MyClass *myclass = segment.construct<MyClass>("MyClassObject")();


Also, when you get the feeling like 76 and change the following line, you can get the address of the appropriate object.
1
2
std::pair<CMemMappedData*, std::size_t> res=segment.find<CMemMappedData>("MyClassObject");
MyClass *myclass = res.first;

MyClass is used here, you can create your own, in its class, new or malloc to ensure it can not share memory and do require attention.

If you want to dynamically allocated memory, as in the previous example, managed_shared_memory to allocate use, should I share the information we have to handle attributes.




Comments

Leave a Reply







  • はてなブックマークへ追加する
  • Facebookでシェアする
  • twitter でつぶやく
  • Google Plusでシェアする
  • Pocketでシェアする
ページトップへ