From 5563867f1f414d31234ee8fa3855fcd073543dd0 Mon Sep 17 00:00:00 2001 From: Cigerxwin Chaker Date: Wed, 6 Nov 2019 11:05:11 +0100 Subject: [PATCH] first commit --- src/SharedMemoryAccess.cpp | 65 ++++++++++++++++++++++++++++++++++++++ src/SharedMemoryAccess.h | 20 ++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/SharedMemoryAccess.cpp create mode 100644 src/SharedMemoryAccess.h diff --git a/src/SharedMemoryAccess.cpp b/src/SharedMemoryAccess.cpp new file mode 100644 index 0000000..73a7d46 --- /dev/null +++ b/src/SharedMemoryAccess.cpp @@ -0,0 +1,65 @@ +// +// Created by Cigerxwin Chaker on 05.11.19. +// +#include +#include +#include +#include +#include +#include +#include +//#include "sharedMemory.h" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */ +#include +#include + +int initSemaphore (void) { + /* Testen, ob das Semaphor bereits existiert */ + semId = semget (semKey, 0, IPC_PRIVATE); + if (semId < 0) { + /* ... existiert noch nicht, also anlegen */ + /* Alle Zugriffsrechte der Dateikreierungsmaske */ + /* erlauben */ + umask(0); + semId = semget (semKey, 1, IPC_CREAT | IPC_EXCL | PERM); + if (semId < 0) { + return -1; + } + /* Semaphor mit 1 initialisieren */ + if (semctl (semId, 0, SETVAL, (int) 1) == -1) + return -1; + } + return 1; +} + +int semaphoreOperation (int op) { + semaphore.sem_op = op; + semaphore.sem_flg = SEM_UNDO; + if( semop (semId, &semaphore, 1) == -1) { + perror(" semop "); + exit (EXIT_FAILURE); + } + return 1; +} + +void writeToShm(char* text, int size) { + int shmId = shmget(memoryAccessKey, NULL, 0); + if(shmId < 0 ) { + exit(EXIT_FAILURE); + /* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/ + } else { + char *shm_pointer = (char *) shmat(shmId, NULL, 0); + semaphore_operation(LOCK); + memcpy(shm_pointer, text, size); + semaphore_operation(UNLOCK); + } +} + +char* getShmPointer() { + int shmId = shmget(memoryAccessKey, NULL, 0); + if(shmId < 0) { + exit(EXIT_FAILURE); + /* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/ + } else { + return (char *) shmat(shmId, NULL, 0); + } +} \ No newline at end of file diff --git a/src/SharedMemoryAccess.h b/src/SharedMemoryAccess.h new file mode 100644 index 0000000..9f26857 --- /dev/null +++ b/src/SharedMemoryAccess.h @@ -0,0 +1,20 @@ +// +// Created by Cigerxwin Chaker on 05.11.19. +// + +#ifndef LIBRARY_SHAREDMEMORYACCESSS_H +#define LIBRARY_SHAREDMEMORYACCESSS_H + +#define PERM 0666 /* Zugriffsrechte */ +#define LOCK -1 +#define UNLOCK 1 +#define SemKey 123458L + +int memoryAccessKey; /* var type is int. but could be another type. */ //TODO: look after type in sharedmemory group +int semId; +struct sembuf semaphore; +int initSemaphore (void); +int semaphoreOperation (int op); +char* getShmPointer(void); +void writeToShm(char* text, int size); +#endif //LIBRARY_SHAREDMEMORYACCESSS_H