This repository has been archived on 2021-08-17. You can view files and clone it, but cannot push or open issues or pull requests.
unix/fb_arc_set/shared/structs.c

78 lines
1.8 KiB
C

#include <stdbool.h> ///< For boolean constants
#include <signal.h>
#define BUFF_SIZE 8
#define SET_MAX_SIZE 8
/// Struct for the edge
typedef struct edge {
bool chosen;
int u, v;
} edge_t;
/// Struct for the feedback arc set
typedef struct fb_arc_set {
bool valid;
int edge_num;
edge_t edges[SET_MAX_SIZE];
} fb_arc_set_t;
/// Struct for the circular buffer
typedef struct buffer {
bool finished;
short best_arc_len;
fb_arc_set_t sets[BUFF_SIZE];
} buffer_t;
static bool terminate = false;
static int buffer_shmfd;
static buffer_t *circ_buffer;
/**
* @brief Function to execute when the program gets a signal
* @details Flips the terminate variable to true
* @param signo
* @return none
**/
static void signal_handler(int signo) {
terminate = true;
}
/**
* @brief Function to process the signals passed by the user
* @details Creates a new sigaction structure
* 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);
}
/**
* @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);
}