sem.c 3.71 KB
Newer Older
1 2 3 4 5 6 7 8 9
#ifndef LIBRARIES
#define LIBRARIES
#define _GNU_SOURCE
#include <fcntl.h> ///< For O_* constants *
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#endif

10
#include <errno.h>
11 12 13
#include <semaphore.h> ///< For semaphores

#define FREE_SEM_NAME "/11777707_free"
14
#define USE_SEM_NAME "/11777707_used"
15 16 17
#define MUTEX_NAME "/11777707_mutex"

static sem_t *free_sem;
18
static sem_t *use_sem;
19 20
static sem_t *mutex;

21 22 23 24
/**
 * @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.
25 26 27
 *          The type parameter is used to determine how the semaphore should be opened:
 *          - as a supervisor: 0
 *          - as a generator: 1
28
 *          Returns a new semaphore
29
 * @param sem_name, sem_size, type
30 31
 * @return res
 **/
32 33 34 35 36 37 38 39
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);

40 41 42 43 44 45 46
    if(res == SEM_FAILED) {
        fprintf(stderr, "ERROR: Failed opening semaphore\n");
        exit(EXIT_FAILURE);
    }
    return res;
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
/**
 * @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
 **/
101 102 103 104 105 106 107
void close_sem(sem_t *sem) {
    if(sem_close(sem) < 0) {
        fprintf(stderr, "ERROR: Failed closing semaphore\n");
        exit(EXIT_FAILURE);
    }
}

108 109 110 111 112 113 114
/**
 * @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
 **/
115 116 117 118 119 120
void destroy_sem(char *sem_name) {
    if(sem_unlink(sem_name) < 0) {
        fprintf(stderr, "ERROR: Failed closing semaphore\n");
        exit(EXIT_FAILURE);
    }
}