#define _SVID_SOURCE 1 //#define _DEFAULT_SOURCE 1 #define _POSIX_C_SOURCE 200112L #include #include #include #include #include #include "shr_mem.h" #include "error.h" #include #include #include #include sem_t * sem = NULL; int createShm(int totalSize, void ** shmPointer) { int fd; if ((fd = shm_open("/BottlerSHM", O_CREAT | O_RDWR, S_IRWXU)) < 0) printSystemError("shm_open() -- ERROR"); ftruncate(fd, totalSize); //* shmPointer = mmap(0, totalSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); return fd; } int writeShm(void * shmPointer, char * str, int len) { if (sem == NULL) { if ((sem = sem_open("/EMPTY", O_CREAT | O_RDWR, S_IRWXU, 0)) == SEM_FAILED) printSystemError("sem_open() -- ERROR"); } if (memcpy(shmPointer, str, len) == NULL) printError("memcpy() -- ERROR"); if (sem_post(sem) < 0) printSystemError("sem_post() -- ERROR"); return EXIT_SUCCESS; } void terminateShm(const char * name, int fd, void * shmPointer, int totalSize) { if (munmap(shmPointer, totalSize) == -1) printSystemError("munmap() -- ERROR"); if (shm_unlink(name) == -1) printSystemError("shm_unlink() -- ERROR"); if (close(fd) == -1) printSystemError("close() -- ERROR"); return; } void closeSem() { if (sem_close(sem) < 0) printSystemError("sem_close() -- ERROR"); if (sem_unlink("/EMPTY") < 0) printSystemError("sem_unlink() -- ERROR"); } int copyShm(char * destPointer, char * srcPointer, int len) { int i; //for (i = 0; srcPointer[i] != '\0' && i < len; i++) { for (i = 0; srcPointer[i] != '\n' && srcPointer[i] != '\0' && i < len; i++) { destPointer[i] = srcPointer[i]; } destPointer[i++] = '\0'; return i; } int readShm(char * shmPointer, char * str, int len) { if (sem == NULL) { if ((sem = sem_open("/EMPTY", O_CREAT | O_RDWR, S_IRWXU, 0)) == SEM_FAILED) printSystemError("sem_open() -- ERROR"); } if (sem_wait(sem) < 0) printSystemError("sem_wait() -- ERROR"); int copied = copyShm(str, shmPointer, len); // Creo que es lo mismo si hacemos: /* memcpy(str, shmPointer, len); *shmPointer += len; */ return copied; }