#ifndef LIBRARIES #define LIBRARIES #define _GNU_SOURCE #include ///< For O_* constants * #include #include #include #endif #include #include #include #define BUFF_SHM_NAME "/11777707_buff" /** * @brief Function that executes shm_open(3) and does error handling. * @details The function executes shm_open(3), giving it a name as a param, * and handles the errors if any. The same behaviour as shm_open(3) should be expected. * Returns a shared memory object file descriptor * @param shm_name * @return shmfd **/ int create_shmfd(char *shm_name) { int shmfd = shm_open(shm_name, O_RDWR | O_CREAT, 0600); if(shmfd == -1) { fprintf(stderr, "ERROR: Failed creating shared memory object\n"); exit(EXIT_FAILURE); } return shmfd; } /** * @brief Function that executes ftruncate(2) and does error handling. * @details The function executes ftruncate(2), giving it a file descriptor and size as parameters, * and handles the errors if any. The same behaviour as ftruncate(2) should be expected. * @param shmfd, shm_size * @return none **/ void truncate_shm(int shmfd, size_t shm_size) { if(ftruncate(shmfd, shm_size) < 0) { fprintf(stderr, "ERROR: Failed truncating shared memory object\n"); exit(EXIT_FAILURE); } } /** * @brief Function that executes mmap(2) and does error handling. * @details The function executes mmap(2), giving it a file descriptor and size as parameters, * and handles the errors if any. The same behaviour as mmap(2) should be expected. * Returns a mapped shared memory object * @param shmfd, shm_size * @return shm **/ void* open_shm(int shmfd, size_t shm_size) { void* shm; shm = mmap(NULL, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0); if(shm == MAP_FAILED) { fprintf(stderr, "ERROR: Failed mapping shared memory object\n"); exit(EXIT_FAILURE); } return shm; } /** * @brief Function to write a value to shared memory object * @details The functions copies the value of a string with a specified size to a shared memory object * @param shm, message, message_size * @return none **/ void write_to_shm(void* shm, char *message, size_t message_size) { memcpy(shm, message, message_size); } /** * @brief Function that executes munmap(2) and does error handling. * @details The function executes munmap(2), giving it a shared memory object and size as parameters, * and handles the errors if any. The same behaviour as munmap(2) should be expected. * Returns a mapped shared memory object * @param shmfd, shm_size * @return none **/ void close_shm(void* shm, size_t shm_size) { if (munmap(shm, shm_size) == -1) { fprintf(stderr, "ERROR: Failed unmapping shared memory object\n"); exit(EXIT_FAILURE); } } /** * @brief Function that executes shm_unlink(3) and close(2) and does error handling. * @details The function executes shm_unlink(3) and close(2), giving them a shared memory object name * and a file descriptor respectively, and handles the errors if any. * The same behaviour as shm_unlink(3) and close(2) should be expected. * @param shm_name, shmfd * @return none **/ void destroy_shm(char *shm_name, int shmfd) { if (shm_unlink(shm_name) == -1) { fprintf(stderr, "ERROR: Failed unlinking shared memory object\n"); exit(EXIT_FAILURE); } if(close(shmfd) < 0) { fprintf(stderr, "ERROR: Failed closing file descriptor\n"); exit(EXIT_FAILURE); } }