From d6674d7aa4bbc706285cbf1e1b751fad60d23211 Mon Sep 17 00:00:00 2001 From: zenon Date: Fri, 8 Jun 2018 22:18:02 +0200 Subject: [PATCH] Generator(s) no properly exit(s) --- generator.c | 30 +++++++++++++++++++----------- supervisor.c | 5 +++++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/generator.c b/generator.c index 9bdfea7..37c2e8a 100644 --- a/generator.c +++ b/generator.c @@ -181,6 +181,8 @@ void printGraph() void cleanup() { + sem_post(sWriteEnd); + sem_post(sFreeSpace); for (int v = 0; v < graph.V; v++) { struct AdjListNode *pCrawl = graph.array[v].head; struct AdjListNode *tmp; @@ -202,6 +204,8 @@ void cleanup() sem_close(sUsedSpace); sem_close(sFreeSpace); sem_close(sWriteEnd); + + exit(EXIT_SUCCESS); } @@ -223,13 +227,20 @@ void wait_sem(sem_t *sem) void putSolution(int *solution) { - if (shared->quit == 1) - return; - wait_sem(sWriteEnd); + if (shared->quit == 1) { + quit = 1; + cleanup(); + } + for (int i = 0; i < MAX_ITEMS; i++) { wait_sem(sFreeSpace); + if (shared->quit == 1) { + quit = 1; + cleanup(); + } + shared->data[shared->tail] = solution[i]; shared->tail = (shared->tail + 1) % MAX_ITEMS; sem_post(sUsedSpace); @@ -249,6 +260,11 @@ int findIndex(int array[], int value, int size) void genSolution(int permutation[], int V, int *solution) { + if (shared->quit == 1) { + quit = 1; + cleanup(); + } + int r; // initialize array to indices @@ -285,14 +301,6 @@ void genSolution(int permutation[], int V, int *solution) pCrawl = pCrawl->next; } } - - /* - printf("["); - for (int i = 0; i < (count - 1); i++) { - printf("%d, ", solution[i]); - } - printf("%d]\n", solution[count-1]); - */ } int main(int argc, char *argv[]) diff --git a/supervisor.c b/supervisor.c index 4fbebaa..0a4cb4f 100644 --- a/supervisor.c +++ b/supervisor.c @@ -41,6 +41,9 @@ void usage(void) void cleanup() { shared->quit = 1; + sem_wait(sUsedSpace); + sem_post(sWriteEnd); + sem_post(sFreeSpace); if (munmap(shared, sizeof(*shared)) == -1) { perror("munmap"); @@ -58,6 +61,8 @@ void cleanup() sem_unlink(SEM_USED_SPACE); sem_unlink(SEM_FREE_SPACE); sem_unlink(SEM_WRITE_END); + + exit(EXIT_SUCCESS); } void initCircBuf()