-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterprocess.h
139 lines (116 loc) · 4.14 KB
/
interprocess.h
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//MIT License
//
//Copyright (c) 2023 TzeLun
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
//
//Author: Tze Lun, Lok
//Date : February 6, 2023
#ifndef INTERPROCESS_H
#define INTERPROCESS_H
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <cstring>
class SharedMemory {
public:
SharedMemory(const char* name="shmem", size_t size=0, bool create=true);
~SharedMemory();
void write(std::string msg);
const char* read();
size_t get_size();
const char* get_name();
int get_fd();
bool fail(); // call this for proper error handling of shared memory creation
private:
void* buffer; // memory address
size_t size; // bytes
const char* name; // name of shared memory
int fd; // file descriptor
bool unsuccessful = false; // safety flag to ensure proper clean up of memory
};
// Creates shared memory which is used for both read and write
// Set "create flag" to false to access an existing shared memory
SharedMemory::SharedMemory(const char* name, size_t size, bool create) {
this->name = name;
this->size = size;
// Set up file descriptor for shared memory
if (create) {
this->fd = shm_open(this->name, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
}
else {
this->fd = shm_open(this->name, O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
}
if (this->fd == -1) {
perror("shm_open");
this->unsuccessful = true;
}
// configure the size of the shared memory
if (ftruncate(this->fd, this->size) == -1) {
perror("ftruncate");
this->unsuccessful = true;
}
// Creating the memory address for the shared memory
this->buffer = mmap(0, this->size, PROT_WRITE | PROT_READ, MAP_SHARED, this->fd, 0);
if (this->buffer == MAP_FAILED) {
perror("mmap");
this->unsuccessful = true;
}
std::cout << "SUCCESSFULLY SETUP SHARED MEMORY :: <<NAME :: " << this->name << "; SIZE :: " << this->size << ">>\n";
}
SharedMemory::~SharedMemory() {
shm_unlink(this->name);
}
// Write a value to the shared memory. String type supported at the moment
void SharedMemory::write(std::string msg) {
strncpy((char *)this->buffer, msg.data(), this->size);
}
// Read from the shared memory
const char* SharedMemory::read() {
return (char*)this->buffer;
}
size_t SharedMemory::get_size() {
return this->size;
}
const char* SharedMemory::get_name() {
return this->name;
}
int SharedMemory::get_fd() {
return this->fd;
}
bool SharedMemory::fail() {
return this->unsuccessful;
}
// Shared memory exclusive process controller
namespace PROCESS {
void wait(SharedMemory* shm, std::string criteria);
}
// Hold the program until the criteria is met
void PROCESS::wait(SharedMemory* shm, std::string criteria) {
bool loop = true;
while (loop) {
if (std::string(shm->read()) == criteria) {
loop = false;
}
}
}
#endif