+ fixed SharedMemoryAccess (build errors)

This commit is contained in:
Julian Hinxlage 2019-11-06 11:54:26 +01:00
parent 255b05cd19
commit 9ce7b06c83
2 changed files with 10 additions and 7 deletions

View File

@ -2,25 +2,27 @@
// Created by Cigerxwin Chaker on 05.11.19. // Created by Cigerxwin Chaker on 05.11.19.
// //
#include <iostream> #include <iostream>
#include <sys/_types.h> #include <sys/types.h>
#include <sys/ipc.h> #include <sys/ipc.h>
#include <sys/shm.h> #include <sys/shm.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h> #include <sys/stat.h>
//#include "sharedMemory.h" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */
#include <sys/sem.h> #include <sys/sem.h>
#include <unistd.h> #include <unistd.h>
#include <cstring>
#include "SharedMemoryAccess.h" /* header is important for the shmID. name could be different. maybe not needed cause: (shmget(memory_access_key, NULL, 0)) */
int initSemaphore (void) { int initSemaphore (void) {
/* Testen, ob das Semaphor bereits existiert */ /* Testen, ob das Semaphor bereits existiert */
semId = semget (semKey, 0, IPC_PRIVATE); semId = semget (SEM_KEY, 0, IPC_PRIVATE);
if (semId < 0) { if (semId < 0) {
/* ... existiert noch nicht, also anlegen */ /* ... existiert noch nicht, also anlegen */
/* Alle Zugriffsrechte der Dateikreierungsmaske */ /* Alle Zugriffsrechte der Dateikreierungsmaske */
/* erlauben */ /* erlauben */
umask(0); umask(0);
semId = semget (semKey, 1, IPC_CREAT | IPC_EXCL | PERM); semId = semget (SEM_KEY, 1, IPC_CREAT | IPC_EXCL | PERM);
if (semId < 0) { if (semId < 0) {
return -1; return -1;
} }
@ -48,9 +50,9 @@ void writeToShm(char* text, int size) {
/* 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);
semaphore_operation(LOCK); semaphoreOperation(LOCK);
memcpy(shm_pointer, text, size); memcpy(shm_pointer, text, size);
semaphore_operation(UNLOCK); semaphoreOperation(UNLOCK);
} }
} }

View File

@ -8,7 +8,7 @@
#define PERM 0666 /* Zugriffsrechte */ #define PERM 0666 /* Zugriffsrechte */
#define LOCK -1 #define LOCK -1
#define UNLOCK 1 #define UNLOCK 1
#define SemKey 123458L #define SEM_KEY 123458L
int memoryAccessKey; /* var type is int. but could be another type. */ //TODO: look after type in sharedmemory group int memoryAccessKey; /* var type is int. but could be another type. */ //TODO: look after type in sharedmemory group
int semId; int semId;
@ -17,4 +17,5 @@ int initSemaphore (void);
int semaphoreOperation (int op); int semaphoreOperation (int op);
char* getShmPointer(void); char* getShmPointer(void);
void writeToShm(char* text, int size); void writeToShm(char* text, int size);
#endif //LIBRARY_SHAREDMEMORYACCESSS_H #endif //LIBRARY_SHAREDMEMORYACCESSS_H