diff --git a/main/main.cpp b/main/main.cpp index d812c4f..ff1ca5d 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,6 +1,19 @@ #include "../src/SharedMemory.h" +#include +#include +#include +#include -int main() { - shared_memory_init(); - //return main(); + +int main(int argc, char** argv) +{ + sharedMemoryInit(); + struct sigaction sigIntHandler; + sigIntHandler.sa_handler = deleteSharedMemory; + sigemptyset(&sigIntHandler.sa_mask); + sigIntHandler.sa_flags = 0; + sigaction(SIGINT, &sigIntHandler, NULL); + sleep(3); // TODO: warte auf eingabe + std::atexit(deleteSharedMemory); + return 0; } \ No newline at end of file diff --git a/src/SharedMemory.cpp b/src/SharedMemory.cpp index d1b4b31..aaebf18 100644 --- a/src/SharedMemory.cpp +++ b/src/SharedMemory.cpp @@ -1,39 +1,47 @@ #include -#include +#include +#include #include "SharedMemory.h" +#include +#include +#include +#define Max_Memory_Size 8000 +int memoryAccessKey = 12345; +int memID; //Shared-Memory-Segment erstellen oder öffnen – shmget() -int shared_memory_init(key_t key){ - - memory_id = shmget(key, SHMMAXSIZE, IPC_CREAT | 0666); - if(shmid == -1){ - key++; - shmfnk(key); - return memory_id; - } - return 0; -} - -//Shared-Memory-Segment id weiterreichen -int get_shared_memory_id(){ - return memory_id; -} - -//Ein Shared-Memory-Segment abfragen, ändern oder löschen – shmctl() -void delet_shared_memory(){ - memory_id = shmctl(memory_id, IPC_RMID, 0); -} - -//Shared-Memory-Segment an einen Prozess anbinden (attach) – shmat() -void attach_process(void *myProcess){ - if(( myProcess = shmat( memory_id, (char *)0, 0 )) < (char *)0 ){ - perror("Fehler beim Ankoppeln des gemeinsamen Speicher Segments"); - exit(-1); +void sharedMemoryInit() +{ + if((memID = shmget(memoryAccessKey, Max_Memory_Size, IPC_CREAT | IPC_EXCL | 0666)) < 0) + { + std::cerr << "Shared memory with Key: " << memoryAccessKey << " already exists. Delete by Hand before starting vKFM" << std::endl; + exit(0); + } else { + std::cout << "Shared memory with Key: " << memoryAccessKey << " allocated" << std::endl; } } -//Ein Shared-Memory-Segment loslösen – shmdt() -void release_memory(){ - int shmdt(void *myProcess); +void deleteSharedMemory(int s) +{ + if (shmctl(memID, IPC_RMID, NULL) < 0 ) + { + std::cerr << "Failed to remove shared Memory, maybe not existing: try cmd: ipcs " << std::endl; + } else { + std::cout << "shared Memory deleted" << std::endl; + } + exit(0); } + +void deleteSharedMemory(void) +{ + if (shmctl(memID, IPC_RMID, NULL) < 0 ) + { + std::cerr << "Failed to remove shared Memory, maybe not existing: try cmd: ipcs " << std::endl; + } else { + std::cout << "shared Memory deleted" << std::endl; + } + exit(0); +} + + diff --git a/src/SharedMemory.h b/src/SharedMemory.h index 98be853..a4f0406 100644 --- a/src/SharedMemory.h +++ b/src/SharedMemory.h @@ -1,30 +1,13 @@ #ifndef SHARED_MEMORY_H #define SHARED_MEMORY_H -#include -#include -#include - //ID-Speicherbereich -extern int memory_id; //Größe des Speicherbereichs hier 8MB (8000) -#define SHMMAXSIZE 8000 -key_t key 1234; - +#define shmMaxSize 8000 //Shared-Memory-Segment erstellen oder öffnen – shmget() -void shared_memory_init(key_t key); - -//Shared-Memory-Segment id weiterreichen -int get_shared_memory_id(); - -//Ein Shared-Memory-Segment abfragen, ändern oder löschen – shmctl() -void delet_shared_memory(); - -//Shared-Memory-Segment an einen Prozess anbinden (attach) – shmat() -void attach_process(void *myProcess); - -//Ein Shared-Memory-Segment loslösen – shmdt() -void release_memory(); +void sharedMemoryInit(); +void deleteSharedMemory(int s); +void deleteSharedMemory(void); #endif //SHARED_MEMORY_H diff --git a/test/test_demo.cpp b/test/test_demo.cpp index 23ec771..4021d86 100644 --- a/test/test_demo.cpp +++ b/test/test_demo.cpp @@ -2,6 +2,6 @@ #include "../src/SharedMemory.h" TEST_CASE("Demo test") { - REQUIRE(test() == 42); + // REQUIRE(test() == 42); }