-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhowToSharedMemory.cpp
121 lines (92 loc) · 3.89 KB
/
howToSharedMemory.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// compile with g++ -std=c++11 -IC:\\boost\\include\boost-1_71\\ -o howToSharedMemory.exe howToSharedMemory.cpp
/*************************************
* This is a simple example of how to use the boost
* library "interprocess" to create virtual memory that
* can be used in more than one process.
*
* This can be used to "recover" from a segmentation
* fault by placing the memory access in a separate
* process and call it from the main one. If the
* subprocess crashes because of a seg fault, the main
* process can resume without losing the virtual memory.
*
*************************************/
#include <stdio.h>
#include <iostream>
#include <windows.h>
#include "timing.hpp"
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
int main(){
printf("\n\n");
printf("--------------------------------\n");
printf("START\n");
printf("--------------------------------\n");
printf("\n\n");
/////////////////////////////////////////////////////////
// Create shared memory
boost::interprocess::shared_memory_object shared_memory(
boost::interprocess::open_or_create,
"shared_memory",
boost::interprocess::read_write
);
// Set size
shared_memory.truncate(8); // memory size in bytes
printf("Created shared memory. \n \n");
// Map the shared_memory in this process
boost::interprocess::mapped_region region(shared_memory, boost::interprocess::read_write);
// Reset memory to some value
std::memset(region.get_address(), 0xBC, region.get_size()); // seems to use a char
printf("Mapped shared memory in main process and reset content. \n \n");
printf("--------------------------------\n");
printf("SHARED MEMORY\n");
printf("Region start: %08x \n", region.get_address());
printf("Region size: %i \n", region.get_size());
printf("Memory : Value\n");
int *mem = static_cast<int*>(region.get_address());
for(std::size_t i = 0; i < region.get_size()/sizeof(int); ++i){
printf("%08x: %08x\n", mem, *mem);
mem++;
}
printf("--------------------------------\n \n");
/////////////////////////////////////////////////////////
// This is a regular int with a pointer in regular memory:
int a = 3;
int * a_ptr = &a;
printf("a : %08x\n", a);
printf("sizeof(a) : %08x byte\n", sizeof(a));
printf("sizeof(a_ptr) : %08x byte\n", sizeof(a_ptr));
printf("a_ptr : %08x\n", a_ptr);
printf("*a_ptr : %08x\n", *a_ptr);
printf("\n\n");
// This is an int with a pointer in the shared memory:
int * b_ptr = (int*) region.get_address();
*b_ptr = 64;
printf("*b_ptr : %08x\n", *b_ptr);
printf("sizeof(*b_ptr): %08x byte\n", sizeof(*b_ptr));
printf("sizeof(b_ptr) : %08x byte\n", sizeof(b_ptr));
printf("b_ptr : %08x\n", b_ptr);
printf("*b_ptr : %08x\n", *b_ptr);
printf("\n\n");
/////////////////////////////////////////////////////////
// Here you could call another process using system(other process.exe)
// There the shared memory can be used with:
/**
// Use shared memory (will throw if shared_memory does not exist)
boost::interprocess::shared_memory_object shared_memory(
boost::interprocess::open_only,
"shared_memory",
boost::interprocess::read_write
);
// Map the shared_memory in this process
boost::interprocess::mapped_region region(shared_memory, boost::interprocess::read_write);
**/
/////////////////////////////////////////////////////////
boost::interprocess::shared_memory_object::remove("shared_memory");
printf("Deleted shared memory. \n \n");
printf("\n\n");
printf("--------------------------------\n");
printf("DONE\n");
printf("--------------------------------\n");
return 0;
}