~ vkvm namespace

This commit is contained in:
Julian Hinxlage 2019-11-13 13:49:02 +01:00
parent 5fd9a70a72
commit 7935cddd71
3 changed files with 19 additions and 17 deletions

View File

@ -3,20 +3,22 @@
#include "../src/SharedMemory.h" #include "../src/SharedMemory.h"
#include <sys/shm.h> #include <sys/shm.h>
#include <iostream> #include <iostream>
#include "vkvm.h" #include "vkvm.hpp"
int main(int argc, char** argv) { int main(int argc, char** argv) {
initialize(0); vkvm::initialize(0);
initSharedMemory(); initSharedMemory();
struct sigaction sigIntHandler; struct sigaction sigIntHandler;
sigIntHandler.sa_handler = deleteSharedMemory; sigIntHandler.sa_handler = deleteSharedMemory;
sigemptyset(&sigIntHandler.sa_mask); sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0; sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, nullptr); sigaction(SIGINT, &sigIntHandler, nullptr);
setDefaultValues(); vkvm::setDefaultValues();
std::atexit(deleteSharedMemory); std::atexit(deleteSharedMemory);
std::string eingabe; std::string eingabe;
while(eingabe != "exit") { while(eingabe != "exit") {
std::cout << "cmd# "; std::cout << "cmd# ";
std::cin >> eingabe; std::cin >> eingabe;
@ -24,7 +26,7 @@ int main(int argc, char** argv) {
if(eingabe == "info") if(eingabe == "info")
{ {
std::cout << "OUTPUT ------------------------ OUTPUT\n" << std::endl; std::cout << "OUTPUT ------------------------ OUTPUT\n" << std::endl;
std::cout << "memory ID: " << shmget(impl.sharedMemoryKey, 0, 0) << std::endl; std::cout << "memory ID: " << shmget(vkvm::impl.sharedMemoryKey, 0, 0) << std::endl;
std::cout << "Max memory Size: " << Max_Memory_Size << std::endl; std::cout << "Max memory Size: " << Max_Memory_Size << std::endl;
std::cout << "OUTPUT ------------------------ OUTPUT\n" << std::endl; std::cout << "OUTPUT ------------------------ OUTPUT\n" << std::endl;
//Memory that is used now //Memory that is used now
@ -32,7 +34,7 @@ int main(int argc, char** argv) {
} else if(eingabe == "write") { } else if(eingabe == "write") {
std::cout << "text: "; std::cout << "text: ";
std::cin >> eingabe; std::cin >> eingabe;
setText(eingabe); vkvm::setText(eingabe);
//write a bitMap for example 2 (width) * 2 (high) * 3 (color) in Schared Memory //write a bitMap for example 2 (width) * 2 (high) * 3 (color) in Schared Memory
/* /*
char bitMap[12] = {0}; char bitMap[12] = {0};
@ -41,7 +43,7 @@ int main(int argc, char** argv) {
writeSharedMemory(bitMap, sizeof(bitMap),1); writeSharedMemory(bitMap, sizeof(bitMap),1);
*/ */
} else if(eingabe == "read") { } else if(eingabe == "read") {
auto str = getText(); auto str = vkvm::getText();
std::cout << str << std::endl; std::cout << str << std::endl;
/* /*
char content[12] = {0}; char content[12] = {0};

View File

@ -34,16 +34,16 @@ std::string getAnswerFromUser()
void initSharedMemory(void) { void initSharedMemory(void) {
if(impl.sharedMemoryKey != key_t(0)) { if(vkvm::impl.sharedMemoryKey != key_t(0)) {
if (shmget(impl.sharedMemoryKey, Max_Memory_Size, IPC_CREAT | IPC_EXCL | 0666) < 0) if (shmget(vkvm::impl.sharedMemoryKey, Max_Memory_Size, IPC_CREAT | IPC_EXCL | 0666) < 0)
{ {
std::cout << "Shared Memory with Key: " << impl.sharedMemoryKey << " is already in use\n" std::cout << "Shared Memory with Key: " << vkvm::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"; <<"You can change the Key( type -> y or n ), or you can override the shared memory Segment(type -> override)\n";
std::string answer = getAnswerFromUser(); std::string answer = getAnswerFromUser();
if(answer.compare("y") == 0) if(answer.compare("y") == 0)
{ {
impl.sharedMemoryKey = changedKey(); vkvm::impl.sharedMemoryKey = changedKey();
std::cout << "new Try with Key:c " << impl.sharedMemoryKey << std::endl; std::cout << "new Try with Key:c " << vkvm::impl.sharedMemoryKey << std::endl;
initSharedMemory(); initSharedMemory();
} else if(answer.compare("n") == 0) { } else if(answer.compare("n") == 0) {
std::cout << "This will end shared memory" <<std::endl; std::cout << "This will end shared memory" <<std::endl;
@ -53,11 +53,11 @@ void initSharedMemory(void) {
initSharedMemory(); initSharedMemory();
} }
} else { } else {
std::cout << "Shared Memory is (Re-) allocated with Key: " << impl.sharedMemoryKey << std::endl; std::cout << "Shared Memory is (Re-) allocated with Key: " << vkvm::impl.sharedMemoryKey << std::endl;
} }
} else { } else {
std::cerr <<"key -> " << impl.sharedMemoryKey << " is not allowed here " << std::endl; std::cerr <<"key -> " << vkvm::impl.sharedMemoryKey << " is not allowed here " << std::endl;
impl.sharedMemoryKey = changedKey(); vkvm::impl.sharedMemoryKey = changedKey();
initSharedMemory(); initSharedMemory();
} }
} }
@ -65,7 +65,7 @@ void initSharedMemory(void) {
void deleteSharedMemory(int s) void deleteSharedMemory(int s)
{ {
std::cerr << "programm ended with Code: " << s << std::endl; std::cerr << "programm ended with Code: " << s << std::endl;
if (shmctl(shmget(impl.sharedMemoryKey, 0, 0), IPC_RMID, 0) < 0 ) if (shmctl(shmget(vkvm::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; std::cerr << "Failed to remove shared Memory, maybe not existing: try cmd: ipcs, then delete with ipcrm -m $memID " << std::endl;
} else { } else {
@ -75,7 +75,7 @@ void deleteSharedMemory(int s)
void deleteSharedMemory(void) void deleteSharedMemory(void)
{ {
if (shmctl(shmget(impl.sharedMemoryKey, 0, 0), IPC_RMID, 0) < 0 ) if (shmctl(shmget(vkvm::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 " << std::endl;
} else { } else {

View File

@ -2,7 +2,7 @@
#define SHARED_MEMORY_H #define SHARED_MEMORY_H
#include <sys/shm.h> #include <sys/shm.h>
#include <iostream> #include <iostream>
#include "internal.h" #include "internal.hpp"
//ID-Speicherbereich //ID-Speicherbereich
//Größe des Speicherbereichs hier 8MB (8000) //Größe des Speicherbereichs hier 8MB (8000)