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

View File

@ -34,16 +34,16 @@ std::string getAnswerFromUser()
void initSharedMemory(void) {
if(impl.sharedMemoryKey != key_t(0)) {
if (shmget(impl.sharedMemoryKey, Max_Memory_Size, IPC_CREAT | IPC_EXCL | 0666) < 0)
if(vkvm::impl.sharedMemoryKey != key_t(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";
std::string answer = getAnswerFromUser();
if(answer.compare("y") == 0)
{
impl.sharedMemoryKey = changedKey();
std::cout << "new Try with Key:c " << impl.sharedMemoryKey << std::endl;
vkvm::impl.sharedMemoryKey = changedKey();
std::cout << "new Try with Key:c " << vkvm::impl.sharedMemoryKey << std::endl;
initSharedMemory();
} else if(answer.compare("n") == 0) {
std::cout << "This will end shared memory" <<std::endl;
@ -53,11 +53,11 @@ void initSharedMemory(void) {
initSharedMemory();
}
} 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 {
std::cerr <<"key -> " << impl.sharedMemoryKey << " is not allowed here " << std::endl;
impl.sharedMemoryKey = changedKey();
std::cerr <<"key -> " << vkvm::impl.sharedMemoryKey << " is not allowed here " << std::endl;
vkvm::impl.sharedMemoryKey = changedKey();
initSharedMemory();
}
}
@ -65,7 +65,7 @@ void initSharedMemory(void) {
void deleteSharedMemory(int s)
{
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;
} else {
@ -75,7 +75,7 @@ void deleteSharedMemory(int s)
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;
} else {

View File

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