-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
63 lines (57 loc) · 2.07 KB
/
server.cpp
File metadata and controls
63 lines (57 loc) · 2.07 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
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
#include <cstring>
#include <chrono>
int main ()
{
int count = 0,exit_cnt = 0;
using namespace boost::interprocess;
using Clock = std::chrono::high_resolution_clock;
try{
//Erase previous shared memory
shared_memory_object::remove("shared_memory");
shared_memory_object::remove("feedback");
//Create a shared memory object.
shared_memory_object shm (create_only, "shared_memory", read_write);
shared_memory_object shm_feedback (create_only, "feedback", read_write);
//Set size
shm.truncate(1000);
//Set size
shm_feedback.truncate(1000);
//Map the whole shared memory in this process
mapped_region region(shm, read_write);
mapped_region region_fd(shm_feedback, read_write);
std::cout << "Mapped Shared memory... wait for feedback" << std::endl;
while(1)
{
shared_memory_object shm (open_only, "feedback", read_only);
const char *mem = static_cast<char*>(region_fd.get_address());
for(std::size_t i = 0; i < region.get_size(); ++i){
//Write all the memory to 1
std::memset(region.get_address(), count, region.get_size());
}
count ++;
if(count > 99)
{
count = 0;
exit_cnt++;
}
if(*mem != NULL)
{
constexpr auto num = Clock::period::num;
constexpr auto den = Clock::period::den;
std::cout << Clock::now().time_since_epoch().count()
<< " [" << num << '/' << den << "] units since epoch\n";
std::cout << "Feedback memory read" << std::endl;
break;
}
}
}
catch(interprocess_exception &ex){
shared_memory_object::remove("shared_memory");
std::cout << ex.what() << std::endl;
return 1;
}
return 0;
}