-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
53 lines (48 loc) · 1.83 KB
/
client.cpp
File metadata and controls
53 lines (48 loc) · 1.83 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
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
#include <cstring>
#include <chrono>
int main ()
{
int value;
using namespace boost::interprocess;
using Clock = std::chrono::high_resolution_clock;
try{
while(1){
//Open already created shared memory object.
shared_memory_object shm (open_only, "shared_memory", read_only);
shared_memory_object shm_feedback (open_only, "feedback", read_write);
//Map the whole shared memory in this process
mapped_region region(shm, read_only);
mapped_region region_fd(shm_feedback, read_write);
//Check that memory was initialized to 1
const char *mem = static_cast<char*>(region.get_address());
for(std::size_t i = 0; i < region.get_size(); ++i){
value = *mem;
// if(*mem++ != 1){
// std::cout << "Error checking memory!" << std::endl;
// return 1;
// }
}
if(value == 98)
{
constexpr auto num = Clock::period::num;
constexpr auto den = Clock::period::den;
std::cout << "Read from shared memory!" << std::endl;
std::memset(region_fd.get_address(), 1, region_fd.get_size());
std::cout << Clock::now().time_since_epoch().count()
<< " [" << num << '/' << den << "] units since epoch\n";
std::cout << "Feedback memory written!" << std::endl;
break;
}
}
}
catch(interprocess_exception &ex){
std::cout << "Unexpected exception: " << ex.what() << std::endl;
shared_memory_object::remove("shared_memory");
return 1;
}
// shared_memory_object::remove("shared_memory");
return 0;
}