structs.c 1.8 KB
Newer Older
1
#include <stdbool.h> ///< For boolean constants
2
#include <signal.h>
3 4 5 6 7 8

#define BUFF_SIZE 8
#define SET_MAX_SIZE 8

/// Struct for the edge
typedef struct edge {
Ivaylo Ivanov's avatar
Ivaylo Ivanov committed
9
    bool chosen;
10 11 12 13
    int u, v;
} edge_t;

/// Struct for the feedback arc set
14
typedef struct fb_arc_set {
15
    bool valid;
16
    int edge_num;
17
    edge_t edges[SET_MAX_SIZE];
18
} fb_arc_set_t;
19 20 21

/// Struct for the circular buffer
typedef struct buffer {
22 23 24
    bool finished;
    short best_arc_len;
    fb_arc_set_t sets[BUFF_SIZE];
25 26
} buffer_t;

27
static bool terminate = false;
28 29
static int buffer_shmfd;
static buffer_t *circ_buffer;
30 31 32

/**
 * @brief Function to execute when the program gets a signal
33 34
 * @details Flips the terminate variable to true
 * @param signo
35 36
 * @return none
 **/
37
static void signal_handler(int signo) {
38 39 40 41 42
    terminate = true;
}

/**
 * @brief Function to process the signals passed by the user
43
 * @details Creates a new sigaction structure
44 45 46 47 48 49 50 51 52 53 54
 *          and changes the behaviour of the SIGINT and SIGTERM signals
 * @param none
 * @return none
 **/
static void process_signal(void){
    struct sigaction sa;
    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = signal_handler;
    sigaction(SIGINT, &sa, NULL);
    sigaction(SIGTERM, &sa, NULL);
}
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

/**
 * @brief Function to that cleans up the resources
 * @details The function closes and destroys all semaphores,
 *          closes and destroys all shared memory objects and
 *          closes the file descriptors
 * @param none
 * @return none
 **/
static void clean_up(void) {
    /// Close and destory semaphores
    close_sem(free_sem);
    close_sem(use_sem);
    close_sem(mutex);

    destroy_sem(FREE_SEM_NAME);
    destroy_sem(USE_SEM_NAME);
    destroy_sem(MUTEX_NAME);

    /// Close and destroy shared memory objects
    close_shm(circ_buffer, sizeof(buffer_t));
    destroy_shm(BUFF_SHM_NAME, buffer_shmfd);
}