From ff14545d827f9ba650a58c66db9794b9dcbaea42 Mon Sep 17 00:00:00 2001 From: Ivaylo Ivanov Date: Sun, 13 Jan 2019 18:48:43 +0100 Subject: [PATCH] Move the clean_up function and add it to generator --- fb_arc_set/generator.c | 7 ++++++- fb_arc_set/shared/structs.c | 23 +++++++++++++++++++++++ fb_arc_set/supervisor.c | 24 ------------------------ 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/fb_arc_set/generator.c b/fb_arc_set/generator.c index d411f28..c1eb62a 100644 --- a/fb_arc_set/generator.c +++ b/fb_arc_set/generator.c @@ -42,6 +42,11 @@ int main(int argc, char *argv[]) { exit(EXIT_FAILURE); } + if(atexit(clean_up) != 0) { + fprintf(stderr, "ERROR: Cannot set exit function\n"); + exit(EXIT_FAILURE); + } + process_signal(); /// Create a regex to check the input against @@ -138,7 +143,7 @@ int main(int argc, char *argv[]) { for(int i=0; i < num_edges; i++) { if(buf_edges[i].chosen == true) { res.edge_num++; - res.edges[--new_best] = edges[i]; ///< We begin counting from 0 + res.edges[--new_best] = buf_edges[i]; ///< We begin counting from 0 } } res.valid = true; diff --git a/fb_arc_set/shared/structs.c b/fb_arc_set/shared/structs.c index bbd27e1..0f45b71 100644 --- a/fb_arc_set/shared/structs.c +++ b/fb_arc_set/shared/structs.c @@ -52,3 +52,26 @@ static void process_signal(void){ 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); +} diff --git a/fb_arc_set/supervisor.c b/fb_arc_set/supervisor.c index 166c633..12c2c55 100644 --- a/fb_arc_set/supervisor.c +++ b/fb_arc_set/supervisor.c @@ -94,27 +94,3 @@ int main(int argc, char *argv[]) { } exit(EXIT_SUCCESS); } - - -/** - * @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 - **/ -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); -}