+ shared memory access cleanup
This commit is contained in:
parent
2dec86e752
commit
9e38b13792
@ -12,8 +12,18 @@
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
|
||||
#include "internal.h"
|
||||
#include "SharedMemoryAccess.h" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */
|
||||
|
||||
#define PERM 0666 /* Zugriffsrechte */
|
||||
#define LOCK -1
|
||||
#define UNLOCK 1
|
||||
#define SEM_KEY 123458L
|
||||
|
||||
//int memoryAccessKey; /* var type is int. but could be another type. */ //TODO: look after type in sharedmemory group
|
||||
int semId;
|
||||
struct sembuf semaphore;
|
||||
|
||||
int initSemaphore (void) {
|
||||
/* Testen, ob das Semaphor bereits existiert */
|
||||
semId = semget (SEM_KEY, 0, IPC_PRIVATE);
|
||||
@ -43,21 +53,21 @@ int semaphoreOperation (int op) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
void writeToShm(char* text, int size) {
|
||||
int shmId = shmget(memoryAccessKey, NULL, 0); // dont init just get the ID if already existing.
|
||||
void writeSharedMemory(char *data, int size, int offset) {
|
||||
int shmId = shmget(impl.sharedMemoryKey, NULL, 0); // dont init just get the ID if already existing.
|
||||
if(shmId < 0 ) {
|
||||
exit(EXIT_FAILURE);
|
||||
/* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/
|
||||
} else {
|
||||
char *shm_pointer = (char *) shmat(shmId, NULL, 0);
|
||||
semaphoreOperation(LOCK);
|
||||
memcpy(shm_pointer, text, size);
|
||||
memcpy(shm_pointer + offset, data, size);
|
||||
semaphoreOperation(UNLOCK);
|
||||
}
|
||||
}
|
||||
|
||||
char* getShmPointer() {
|
||||
int shmId = shmget(memoryAccessKey, NULL, 0);
|
||||
const char* getSharedMemory() {
|
||||
int shmId = shmget(impl.sharedMemoryKey, NULL, 0);
|
||||
if(shmId < 0) {
|
||||
exit(EXIT_FAILURE);
|
||||
/* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/
|
||||
|
@ -5,17 +5,25 @@
|
||||
#ifndef LIBRARY_SHAREDMEMORYACCESSS_H
|
||||
#define LIBRARY_SHAREDMEMORYACCESSS_H
|
||||
|
||||
#define PERM 0666 /* Zugriffsrechte */
|
||||
#define LOCK -1
|
||||
#define UNLOCK 1
|
||||
#define SEM_KEY 123458L
|
||||
|
||||
int memoryAccessKey; /* var type is int. but could be another type. */ //TODO: look after type in sharedmemory group
|
||||
int semId;
|
||||
struct sembuf semaphore;
|
||||
int initSemaphore (void);
|
||||
int semaphoreOperation (int op);
|
||||
char* getShmPointer(void);
|
||||
void writeToShm(char* text, int size);
|
||||
/**
|
||||
* only to read the shared memory
|
||||
* @return pointer to the shared memory
|
||||
*/
|
||||
const char *getSharedMemory();
|
||||
|
||||
/**
|
||||
*
|
||||
* @return the size of the shared memory
|
||||
*/
|
||||
int getSharedMemorySize();
|
||||
|
||||
/**
|
||||
*
|
||||
* @param data poiter to data
|
||||
* @param size of data
|
||||
* @param offset where the data is written on the shared memory
|
||||
*/
|
||||
void writeSharedMemory(char *data, int size, int offset);
|
||||
|
||||
#endif //LIBRARY_SHAREDMEMORYACCESSS_H
|
||||
|
@ -55,10 +55,6 @@ struct Impl {
|
||||
|
||||
extern Impl impl;
|
||||
|
||||
void *getSharedMemory();
|
||||
|
||||
int getSharedMemorySize();
|
||||
|
||||
/**
|
||||
* send a signal to a process
|
||||
* @param pid of the process to which the signal is send
|
||||
|
Loading…
Reference in New Issue
Block a user