aktuelle version. getestet. Es fehlen noch evtl Feature. aber shared memory wird bereit gestellt. Key weitergabe muss noch besprochen werden.
This commit is contained in:
parent
55e262a889
commit
bda7955726
|
@ -1,19 +1,34 @@
|
|||
#include "../src/SharedMemory.h"
|
||||
#include <unistd.h>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <signal.h>
|
||||
#include "../src/SharedMemory.h"
|
||||
#include <sys/shm.h>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
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;
|
||||
}
|
|
@ -1,44 +1,80 @@
|
|||
#include <cstdlib>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#include "SharedMemory.h"
|
||||
#include <iostream>
|
||||
#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" <<std::endl;
|
||||
exit(0);
|
||||
} else if(answer.compare("override") == 0) {
|
||||
deleteSharedMemory();
|
||||
initSharedMemory();
|
||||
}
|
||||
} else {
|
||||
std::cout << "Shared Memory ist (Re-) allocated with Key: " << impl.sharedMemoryKey << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cerr <<"key -> " << 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);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
#ifndef SHARED_MEMORY_H
|
||||
#define SHARED_MEMORY_H
|
||||
#include <sys/shm.h>
|
||||
#include <iostream>
|
||||
#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
|
||||
|
|
|
@ -2,6 +2,6 @@
|
|||
#include "../src/SharedMemory.h"
|
||||
|
||||
TEST_CASE("Demo test") {
|
||||
// REQUIRE(test() == 42);
|
||||
//REQUIRE(test() == 42);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue