Add clean_up function, add num_nodes logic to generator, change docs

This commit is contained in:
Ivaylo Ivanov 2019-01-13 13:18:02 +01:00
parent 4d9c62cb84
commit 8c423aaa16
3 changed files with 42 additions and 13 deletions

View File

@ -47,12 +47,14 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE);
}
edge_t edges[argc - 2]; ///< Array to hold the edges
int num_edges = argc - 1;
edge_t edges[num_edges]; ///< Array to hold all edges
char edge[128]; ///< Variable to hold a single edge later on
edge[127] = 0;
for(int i=1; i < argc; i++) {
int num_nodes = 0;
for(int i=1; i <= num_edges; i++) {
/// Check the input and terminate if incorrect
if(regexec(&regex, argv[i], 0, NULL, 0) == REG_NOMATCH) {
fprintf(stderr, "ERROR: Incorrect input found\n");
@ -65,13 +67,22 @@ int main(int argc, char *argv[]) {
char *v = strtok(NULL, "");
edges[i-1].u = strtol(u, NULL, 10);
edges[i-1].v = strtol(v, NULL, 10);
if(num_nodes < edges[i-1].u)
num_nodes = edges[i-1].u;
if(num_nodes < edges[i-1].v)
num_nodes = edges[i-1].u;
}
}
num_nodes++; ///< Our first node is labeled 0, so we need to add 1 more
int buffer_shmfd = create_shmfd(BUFF_SHM_NAME);
printf("%d", num_nodes);
/// Allocate resources for circular buffer
buffer_shmfd = create_shmfd(BUFF_SHM_NAME);
circ_buffer = (buffer_t *)open_shm(buffer_shmfd, sizeof(buffer_t));
int wr_pos = 0;
exit(EXIT_SUCCESS);
}

View File

@ -24,20 +24,22 @@ typedef struct buffer {
} 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 none
* @details Flips the terminate variable to true
* @param signo
* @return none
**/
static void signal_handler() {
static void signal_handler(int signo) {
terminate = true;
}
/**
* @brief Function to process the signals passed by the user
* @details creates a new sigaction structure
* @details Creates a new sigaction structure
* and changes the behaviour of the SIGINT and SIGTERM signals
* @param none
* @return none
@ -49,5 +51,3 @@ static void process_signal(void){
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
}
static buffer_t *circ_buffer;

View File

@ -25,16 +25,23 @@
#include "shared/sem.c"
#include "shared/structs.c"
void clean_up(void);
int main(int argc, char *argv[]) {
if(argc > 1) {
fprintf(stderr, "ERROR: Command takes no arguments");
exit(EXIT_FAILURE);
}
if(atexit(clean_up) != 0) {
fprintf(stderr, "ERROR: Cannot set exit function\n");
exit(EXIT_FAILURE);
}
process_signal();
/// Open shared memory objects
int buffer_shmfd = create_shmfd(BUFF_SHM_NAME);
buffer_shmfd = create_shmfd(BUFF_SHM_NAME);
truncate_shm(buffer_shmfd, BUFF_SIZE);
circ_buffer = (buffer_t *)open_shm(buffer_shmfd, sizeof(buffer_t));
@ -71,7 +78,7 @@ int main(int argc, char *argv[]) {
if(fb_arc.edge_num == 0) {
/// If the graph is acyclic, set the terminate flag
puts("The graph is acyclic!");
signal_handler();
signal_handler(SIGINT);
} else if(fb_arc.edge_num < circ_buffer->best_arc_len) {
/// Save ther best feedback arc length and print a solution
circ_buffer->best_arc_len = fb_arc.edge_num;
@ -83,7 +90,19 @@ int main(int argc, char *argv[]) {
printf("\n");
}
}
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);
@ -96,5 +115,4 @@ int main(int argc, char *argv[]) {
/// Close and destroy shared memory objects
close_shm(circ_buffer, sizeof(buffer_t));
destroy_shm(BUFF_SHM_NAME, buffer_shmfd);
exit(EXIT_SUCCESS);
}