aktuelle version

This commit is contained in:
cigerxwinchaker 2019-11-06 13:07:33 +01:00
parent 734d2e4f28
commit b8773350a9
4 changed files with 59 additions and 55 deletions

View File

@ -1,6 +1,19 @@
#include "../src/SharedMemory.h"
#include <unistd.h>
#include <cstdlib>
#include <iostream>
#include <signal.h>
int main() {
shared_memory_init();
//return main();
int main(int argc, char** argv)
{
sharedMemoryInit();
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);
return 0;
}

View File

@ -1,39 +1,47 @@
#include <cstdlib>
#include <cstdio>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "SharedMemory.h"
#include <iostream>
#include <unistd.h>
#include <signal.h>
#define Max_Memory_Size 8000
int memoryAccessKey = 12345;
int memID;
//Shared-Memory-Segment erstellen oder öffnen shmget()
int shared_memory_init(key_t key){
memory_id = shmget(key, SHMMAXSIZE, IPC_CREAT | 0666);
if(shmid == -1){
key++;
shmfnk(key);
return memory_id;
}
return 0;
}
//Shared-Memory-Segment id weiterreichen
int get_shared_memory_id(){
return memory_id;
}
//Ein Shared-Memory-Segment abfragen, ändern oder löschen shmctl()
void delet_shared_memory(){
memory_id = shmctl(memory_id, IPC_RMID, 0);
}
//Shared-Memory-Segment an einen Prozess anbinden (attach) shmat()
void attach_process(void *myProcess){
if(( myProcess = shmat( memory_id, (char *)0, 0 )) < (char *)0 ){
perror("Fehler beim Ankoppeln des gemeinsamen Speicher Segments");
exit(-1);
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);
} else {
std::cout << "Shared memory with Key: " << memoryAccessKey << " allocated" << std::endl;
}
}
//Ein Shared-Memory-Segment loslösen shmdt()
void release_memory(){
int shmdt(void *myProcess);
void deleteSharedMemory(int s)
{
if (shmctl(memID, IPC_RMID, NULL) < 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);
}
void deleteSharedMemory(void)
{
if (shmctl(memID, IPC_RMID, NULL) < 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);
}

View File

@ -1,30 +1,13 @@
#ifndef SHARED_MEMORY_H
#define SHARED_MEMORY_H
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
//ID-Speicherbereich
extern int memory_id;
//Größe des Speicherbereichs hier 8MB (8000)
#define SHMMAXSIZE 8000
key_t key 1234;
#define shmMaxSize 8000
//Shared-Memory-Segment erstellen oder öffnen shmget()
void shared_memory_init(key_t key);
//Shared-Memory-Segment id weiterreichen
int get_shared_memory_id();
//Ein Shared-Memory-Segment abfragen, ändern oder löschen shmctl()
void delet_shared_memory();
//Shared-Memory-Segment an einen Prozess anbinden (attach) shmat()
void attach_process(void *myProcess);
//Ein Shared-Memory-Segment loslösen shmdt()
void release_memory();
void sharedMemoryInit();
void deleteSharedMemory(int s);
void deleteSharedMemory(void);
#endif //SHARED_MEMORY_H

View File

@ -2,6 +2,6 @@
#include "../src/SharedMemory.h"
TEST_CASE("Demo test") {
REQUIRE(test() == 42);
// REQUIRE(test() == 42);
}