diff --git a/main/main.cpp b/main/main.cpp index ff1ca5d..261829f 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,19 +1,34 @@ -#include "../src/SharedMemory.h" -#include #include -#include #include +#include "../src/SharedMemory.h" +#include +#include int main(int argc, char** argv) { - sharedMemoryInit(); + initSharedMemory(); 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); + std::string eingabe = ""; + while(eingabe != "exit") { + std::cout << "cmd# "; + std::cin >> eingabe; + //if verschieden information ausgaben + if(eingabe == "info") + { + std::cout << "OUTPUT ------------------------ OUTPUT" << std::endl; + std::cout << "memory ID: " << shmget(impl.sharedMemoryKey, 0, 0) << std::endl; + std::cout << "Max memory Size: " << Max_Memory_Size << std::endl; + std::cout << "OUTPUT ------------------------ OUTPUT" << std::endl; + //Memory that is used now + //outprint of all memory + } + } return 0; } \ No newline at end of file diff --git a/src/SharedMemory.cpp b/src/SharedMemory.cpp index 0e36071..3f7c3cc 100644 --- a/src/SharedMemory.cpp +++ b/src/SharedMemory.cpp @@ -1,44 +1,80 @@ #include -#include -#include #include "SharedMemory.h" #include -#define Max_Memory_Size 8000 -int memoryAccessKey = 12345; -int memID; +key_t changedKey() +{ + int newKey; + std::cout << "Type in a new Key for the Shared Memory Segment" << std::endl; + std::cout << "new Key#"; + std::cin >> newKey; + std::cout <<"New Key is -> " << newKey << std::endl; + return key_t(newKey); +} -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); +std::string getAnswerFromUser() +{ + std::cout <<"answer# "; + std::string answer; + std::cin >> answer; + if(answer.compare("y") == 0 || answer.compare("n") == 0|| answer.compare("override") == 0) { + return answer; } else { - std::cout << "Shared memory with Key: " << memoryAccessKey << " allocated" << std::endl; + std::cout << "wrong input: pls answer with (y) for yes, (n) for no, or (override)\n"; + getAnswerFromUser(); } } +void initSharedMemory(void) { + if(impl.sharedMemoryKey != key_t(0)) { + if (shmget(impl.sharedMemoryKey, Max_Memory_Size, IPC_CREAT | IPC_EXCL | 0666) < 0) + { + std::cout << "Shared Memory with Key: " << impl.sharedMemoryKey << " is already in use\n" + <<"You can change the Key( type -> y or n ), or you can override the shared memory Segment(type -> override)\n"; + std::string answer = getAnswerFromUser(); + if(answer.compare("y") == 0) + { + std::cout << "Please type in a new Key:\n"; + std::cout << "#"; + std::cin >> impl.sharedMemoryKey; + std::cout << "new Try with Key:c " << impl.sharedMemoryKey << std::endl; + initSharedMemory(); + } else if(answer.compare("n") == 0) { + std::cout << "This will end shared memory" < " << impl.sharedMemoryKey << " is not allowed here " << std::endl; + impl.sharedMemoryKey = changedKey(); + initSharedMemory(); + } +} + void deleteSharedMemory(int s) { - if (shmctl(memID, IPC_RMID, NULL) < 0 ) + std::cerr << "programm ended with Code: " << s << std::endl; + if (shmctl(shmget(impl.sharedMemoryKey, 0, 0), IPC_RMID, 0) < 0 ) { - std::cerr << "Failed to remove shared Memory, maybe not existing: try cmd: ipcs " << std::endl; + std::cerr << "Failed to remove shared Memory, maybe not existing: try cmd: ipcs, then delete with ipcrm -m $memID " << std::endl; } else { std::cout << "shared Memory deleted" << std::endl; } - exit(0); } void deleteSharedMemory(void) { - if (shmctl(memID, IPC_RMID, NULL) < 0 ) + if (shmctl(shmget(impl.sharedMemoryKey, 0, 0), IPC_RMID, 0) < 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 a4f0406..96aefca 100644 --- a/src/SharedMemory.h +++ b/src/SharedMemory.h @@ -1,13 +1,15 @@ #ifndef SHARED_MEMORY_H #define SHARED_MEMORY_H +#include +#include +#include "internal.h" //ID-Speicherbereich //Größe des Speicherbereichs hier 8MB (8000) #define shmMaxSize 8000 - //Shared-Memory-Segment erstellen oder öffnen – shmget() -void sharedMemoryInit(); +#define Max_Memory_Size 8000 void deleteSharedMemory(int s); void deleteSharedMemory(void); - +void initSharedMemory(void); #endif //SHARED_MEMORY_H diff --git a/test/test_demo.cpp b/test/test_demo.cpp index 4021d86..fde1445 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); }