diff options
| author | ckrinitsin <101062646+ckrinitsin@users.noreply.github.com> | 2025-03-21 15:20:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-21 15:20:21 +0100 |
| commit | 2a22b4123dce661b0500dc07012d61215bdce161 (patch) | |
| tree | 0e862837248107e4c0a323fffb2632da0bbb4482 /src/common/shared_memory.h | |
| parent | 5fee3dbc7341d2b6c4e72c0f56a121e1bd5553d9 (diff) | |
| parent | 4d0d9f183d7f92a41d66ffdcde15b040ff4032ef (diff) | |
| download | BT-Programming-Assignment-2a22b4123dce661b0500dc07012d61215bdce161.tar.gz BT-Programming-Assignment-2a22b4123dce661b0500dc07012d61215bdce161.zip | |
Merge pull request #3 from ckrinitsin/shm
Shared Memory Buffer
Diffstat (limited to '')
| -rw-r--r-- | src/common/shared_memory.h | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/common/shared_memory.h b/src/common/shared_memory.h new file mode 100644 index 0000000..70554e4 --- /dev/null +++ b/src/common/shared_memory.h @@ -0,0 +1,46 @@ +#pragma once + +#include <pthread.h> +#include <sstream> + +#define SHM_NAME "/hashtable_queue" +#define QUEUE_SIZE 10 + +#define MAX_KEY_SIZE 64 +#define MAX_VALUE_SIZE 128 + +enum Operations { INSERT, DELETE, GET, PRINT }; + +struct Request { + Operations type; + char key[MAX_KEY_SIZE]; + char value[MAX_VALUE_SIZE]; + char response[MAX_VALUE_SIZE]; +}; + +struct SharedMemory { + Request request[QUEUE_SIZE]; + pthread_mutex_t mutex; + pthread_cond_t cond_var; + + int tail; + int head; + bool full; +}; + +template <typename T> +std::string serialize(const T& data) +{ + std::ostringstream oss; + oss << data; + return oss.str(); +} + +template <typename T> +T deserialize(const std::string& str) +{ + std::istringstream iss(str); + T data; + iss >> data; + return data; +} |