-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshm_test.cpp
More file actions
103 lines (85 loc) · 2.62 KB
/
shm_test.cpp
File metadata and controls
103 lines (85 loc) · 2.62 KB
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
#include <iostream>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <sys/stat.h>
using namespace std;
using namespace boost::interprocess;
struct ProgramPosition{
char name[100];
int pid;
double latitude;
double longitude;
};
boost::interprocess::shared_memory_object mShmobj;
size_t mShmSize;
struct ProgramPosition TestPosRd;
int sharedMemoryWrite(void * BufferData, int Offset, size_t size)
{
if( (size <= 0) || (size > 1024) ) {
cout << "Wrong passed Size " << size << endl;
return -1;
}
if( (Offset < 0) || ((Offset + size) >= 1024) ) {
cout << "Wrong passed Offset = " << Offset << endl;
return -2;
}
//Map the whole shared memory in this process
mapped_region region(mShmobj,read_write, Offset, size);
if(region.get_address() == NULL) {
return -3;
}
//Obtain the shared structure
memcpy(region.get_address(), BufferData, size);
return size;
}
int sharedMemoryRead(void * BufferData, int Offset, size_t size)
{
if( (size <= 0) || (size > 1024) ) {
cout << "Wrong passed Size" << endl;
return -1;
}
if( (Offset < 0) || ((Offset + size) >= 1024) ) {
cout << "Wrong passed Offset" << endl;
return -2;
}
//map the whole shared memory in this process
mapped_region region(mShmobj,read_write, Offset, size);
//get pointer to data
void *mem_map = static_cast<void*>(region.get_address());
if(mem_map == NULL) {
cout << "Cannot read From SHM" << endl;
return -3;
}
memcpy(BufferData, mem_map, size);
return size;
}
int main()
{
std::string mshmName("shmNew1");
try {
mShmobj = shared_memory_object(open_only, mshmName.c_str(), read_write);
int Rdsize = sharedMemoryRead((void*)&TestPosRd, 0, sizeof(TestPosRd));
if(Rdsize > 0) {
cout << "Read SHM Data Amount = " << Rdsize << endl;
cout << "Read Struct Name = " << TestPosRd.name << endl;
cout << "Read Struct Pid = " << TestPosRd.pid << endl;
cout << "Read Struct Latitude = " << TestPosRd.latitude << endl;
cout << "Read Struct Longitude = " << TestPosRd.longitude << endl;
}else {
cout << "Cannot read from Shared Memory" << endl;
}
TestPosRd.latitude += 0.10;
TestPosRd.longitude += 0.10;
int Wrsize = sharedMemoryWrite((void*)&TestPosRd, 0, sizeof(TestPosRd));
if(Wrsize < 0) {
cout << "Cannot Write To Shared Memory" << endl;
} else {
cout << "GPS Data Updated successfully to Shared Memory" << endl;
}
} catch (interprocess_exception &e) {
cout << "Error processing Shared Memory = " << e.get_error_code() << endl;
return 1;
}
return 0;
}