2019-01-12 11:39:01 +00:00
|
|
|
#ifndef LIBRARIES
|
|
|
|
#define LIBRARIES
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <fcntl.h> ///< For O_* constants *
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2019-01-12 22:00:50 +00:00
|
|
|
#include <errno.h>
|
2019-01-12 11:39:01 +00:00
|
|
|
#include <semaphore.h> ///< For semaphores
|
|
|
|
|
|
|
|
#define FREE_SEM_NAME "/11777707_free"
|
2019-01-12 22:00:50 +00:00
|
|
|
#define USE_SEM_NAME "/11777707_used"
|
2019-01-12 11:39:01 +00:00
|
|
|
#define MUTEX_NAME "/11777707_mutex"
|
|
|
|
|
|
|
|
static sem_t *free_sem;
|
2019-01-12 22:00:50 +00:00
|
|
|
static sem_t *use_sem;
|
2019-01-12 11:39:01 +00:00
|
|
|
static sem_t *mutex;
|
|
|
|
|
2019-01-12 22:00:50 +00:00
|
|
|
/**
|
|
|
|
* @brief Function that executes sem_open(3) and does error handling.
|
|
|
|
* @details The function executes sem_open(3), giving it a semaphor name and size as parameters,
|
|
|
|
* and handles the errors if any. The same behaviour as sem_open(3) should be expected.
|
2019-01-13 13:18:20 +00:00
|
|
|
* The type parameter is used to determine how the semaphore should be opened:
|
|
|
|
* - as a supervisor: 0
|
|
|
|
* - as a generator: 1
|
2019-01-12 22:00:50 +00:00
|
|
|
* Returns a new semaphore
|
2019-01-13 13:18:20 +00:00
|
|
|
* @param sem_name, sem_size, type
|
2019-01-12 22:00:50 +00:00
|
|
|
* @return res
|
|
|
|
**/
|
2019-01-13 13:18:20 +00:00
|
|
|
sem_t * open_sem(char *sem_name, size_t sem_size, short type) {
|
|
|
|
sem_t *res;
|
|
|
|
|
|
|
|
if(type == 0)
|
|
|
|
res = sem_open(sem_name, O_CREAT | O_EXCL, 0600, sem_size);
|
|
|
|
else
|
|
|
|
res = sem_open(sem_name, sem_size);
|
|
|
|
|
2019-01-12 11:39:01 +00:00
|
|
|
if(res == SEM_FAILED) {
|
|
|
|
fprintf(stderr, "ERROR: Failed opening semaphore\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:00:50 +00:00
|
|
|
/**
|
|
|
|
* @brief Function that executes sem_getvalue(3) and does error handling.
|
|
|
|
* @details The function executes sem_getvalue(3), giving it a semaphor and size as parameters,
|
|
|
|
* and handles the errors if any. The same behaviour as sem_getvalue(3) should be expected.
|
|
|
|
* Writes the value of a semaphore to a chosen pointer
|
|
|
|
* @param sem, rem
|
|
|
|
* @return none
|
|
|
|
**/
|
|
|
|
void getval_sem(sem_t *sem, int *res) {
|
|
|
|
if(sem_getvalue(sem, res) == -1) {
|
|
|
|
fprintf(stderr, "ERROR: Failed getting semaphore value\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Function that executes sem_wait(3) and does error handling.
|
|
|
|
* @details The function executes sem_wait(3), giving it a semaphor as a parameter,
|
|
|
|
* and handles the errors if any. The same behaviour as sem_wait(3) should be expected.
|
|
|
|
* If the process was terminated, the function exits
|
|
|
|
* @param sem
|
|
|
|
* @return none
|
|
|
|
**/
|
|
|
|
void wait_sem(sem_t *sem) {
|
|
|
|
if(sem_wait(sem) == -1) {
|
|
|
|
if(errno == EINTR) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "ERROR: Failed locking semaphore\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Function that executes sem_post(3) and does error handling.
|
|
|
|
* @details The function executes sem_post(3), giving it a semaphor as a parameter,
|
|
|
|
* and handles the errors if any. The same behaviour as sem_post(3) should be expected.
|
|
|
|
* @param sem
|
|
|
|
* @return none
|
|
|
|
**/
|
|
|
|
void post_sem(sem_t *sem) {
|
|
|
|
if(sem_post(sem) == -1) {
|
|
|
|
fprintf(stderr, "ERROR: Failed unlocking semaphore\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Function that executes sem_close(3) and does error handling.
|
|
|
|
* @details The function executes sem_close(3), giving it a semaphor as a parameter,
|
|
|
|
* and handles the errors if any. The same behaviour as sem_close(3) should be expected.
|
|
|
|
* @param sem
|
|
|
|
* @return none
|
|
|
|
**/
|
2019-01-12 11:39:01 +00:00
|
|
|
void close_sem(sem_t *sem) {
|
|
|
|
if(sem_close(sem) < 0) {
|
|
|
|
fprintf(stderr, "ERROR: Failed closing semaphore\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:00:50 +00:00
|
|
|
/**
|
|
|
|
* @brief Function that executes sem_unlink(3) and does error handling.
|
|
|
|
* @details The function executes sem_unlink(3), giving it a semaphor name as a parameter,
|
|
|
|
* and handles the errors if any. The same behaviour as sem_unlink(3) should be expected.
|
|
|
|
* @param sem_name
|
|
|
|
* @return none
|
|
|
|
**/
|
2019-01-12 11:39:01 +00:00
|
|
|
void destroy_sem(char *sem_name) {
|
|
|
|
if(sem_unlink(sem_name) < 0) {
|
|
|
|
fprintf(stderr, "ERROR: Failed closing semaphore\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|