From f9c866231b4124ffedf6481b6ff772b3cdb4ad09 Mon Sep 17 00:00:00 2001 From: Shaohua Tong Date: Fri, 8 Nov 2019 18:30:27 +0100 Subject: [PATCH] getShareMemory(char *address, int size, int offset) hinzufuegen, ob man es braucht. --- src/SharedMemoryAccess.cpp | 17 +++++++++++++++++ src/SharedMemoryAccess.h | 13 +++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/SharedMemoryAccess.cpp b/src/SharedMemoryAccess.cpp index 0fd13dd..412d54b 100644 --- a/src/SharedMemoryAccess.cpp +++ b/src/SharedMemoryAccess.cpp @@ -61,10 +61,27 @@ void writeSharedMemory(char *data, int size, int offset) { return; /* we could return -1, not sure if it will be useful to continue after there is no shm allocated for any reason*/ } else { + std::cout << "writing" << std::endl; char *shm_pointer = (char *) shmat(shmId, NULL, 0); semaphoreOperation(LOCK); memcpy(shm_pointer + offset, data, size); + shmdt(shm_pointer); //close shm_pointer semaphoreOperation(UNLOCK); + std::cout << "writing successed" << std::endl; + } +} + +void getSharedMemory(char *address, int size, int offset) { + int shmId = shmget(impl.sharedMemoryKey, NULL, 0); + if(shmId < 0) { + return; + } else { + char *shm_pointer = (char *) shmat(shmId, NULL, 0); + semaphoreOperation(LOCK); + memcpy(address, shm_pointer + offset, size); + shmdt(shm_pointer); //close shm_pointer + semaphoreOperation(UNLOCK); + return; } } diff --git a/src/SharedMemoryAccess.h b/src/SharedMemoryAccess.h index 949307b..f50dffb 100644 --- a/src/SharedMemoryAccess.h +++ b/src/SharedMemoryAccess.h @@ -12,6 +12,15 @@ */ char *getSharedMemory(); +/** + * use lock and unlock when writing, + * set content to the address + * @param *address target address + * @param size of data + * @param offset where the data is written on the shared memory + */ +void getSharedMemory(char *address, int size, int offset); + /** * * @return the size of the shared memory @@ -24,10 +33,10 @@ void unlockSharedMemory(); /** * - * @param data poiter to data + * @param data pointer 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); +void writeSharedMemory(char *date, int size, int offset); #endif //LIBRARY_SHAREDMEMORYACCESSS_H