+ shared memory access cleanup
This commit is contained in:
parent
2dec86e752
commit
9e38b13792
@ -12,8 +12,18 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <cstring>
|
#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)) */
|
#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) {
|
int initSemaphore (void) {
|
||||||
/* Testen, ob das Semaphor bereits existiert */
|
/* Testen, ob das Semaphor bereits existiert */
|
||||||
semId = semget (SEM_KEY, 0, IPC_PRIVATE);
|
semId = semget (SEM_KEY, 0, IPC_PRIVATE);
|
||||||
@ -43,21 +53,21 @@ int semaphoreOperation (int op) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeToShm(char* text, int size) {
|
void writeSharedMemory(char *data, int size, int offset) {
|
||||||
int shmId = shmget(memoryAccessKey, NULL, 0); // dont init just get the ID if already existing.
|
int shmId = shmget(impl.sharedMemoryKey, NULL, 0); // dont init just get the ID if already existing.
|
||||||
if(shmId < 0 ) {
|
if(shmId < 0 ) {
|
||||||
exit(EXIT_FAILURE);
|
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*/
|
/* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/
|
||||||
} else {
|
} else {
|
||||||
char *shm_pointer = (char *) shmat(shmId, NULL, 0);
|
char *shm_pointer = (char *) shmat(shmId, NULL, 0);
|
||||||
semaphoreOperation(LOCK);
|
semaphoreOperation(LOCK);
|
||||||
memcpy(shm_pointer, text, size);
|
memcpy(shm_pointer + offset, data, size);
|
||||||
semaphoreOperation(UNLOCK);
|
semaphoreOperation(UNLOCK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char* getShmPointer() {
|
const char* getSharedMemory() {
|
||||||
int shmId = shmget(memoryAccessKey, NULL, 0);
|
int shmId = shmget(impl.sharedMemoryKey, NULL, 0);
|
||||||
if(shmId < 0) {
|
if(shmId < 0) {
|
||||||
exit(EXIT_FAILURE);
|
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*/
|
/* 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
|
#ifndef LIBRARY_SHAREDMEMORYACCESSS_H
|
||||||
#define 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;
|
* only to read the shared memory
|
||||||
struct sembuf semaphore;
|
* @return pointer to the shared memory
|
||||||
int initSemaphore (void);
|
*/
|
||||||
int semaphoreOperation (int op);
|
const char *getSharedMemory();
|
||||||
char* getShmPointer(void);
|
|
||||||
void writeToShm(char* text, int size);
|
/**
|
||||||
|
*
|
||||||
|
* @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
|
#endif //LIBRARY_SHAREDMEMORYACCESSS_H
|
||||||
|
@ -55,10 +55,6 @@ struct Impl {
|
|||||||
|
|
||||||
extern Impl impl;
|
extern Impl impl;
|
||||||
|
|
||||||
void *getSharedMemory();
|
|
||||||
|
|
||||||
int getSharedMemorySize();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* send a signal to a process
|
* send a signal to a process
|
||||||
* @param pid of the process to which the signal is send
|
* @param pid of the process to which the signal is send
|
||||||
|
Loading…
Reference in New Issue
Block a user