#include #include "SharedMemory.h" #include 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); } 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 << "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) { 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, then delete with ipcrm -m $memID " << std::endl; } else { std::cout << "shared Memory deleted" << std::endl; } } void deleteSharedMemory(void) { 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; } }