From de4ed6532c0da7e5c6794031335cddc0501b3d8d Mon Sep 17 00:00:00 2001 From: zenon Date: Thu, 7 Jun 2018 17:52:49 +0200 Subject: [PATCH] Rearrange cleanup() function and copy wait_sem() function from supervisor --- generator.c | 77 ++++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/generator.c b/generator.c index 49642b8..7df27f9 100644 --- a/generator.c +++ b/generator.c @@ -178,29 +178,6 @@ void printGraph() } } -void putSolution(sem_t sUsedSpace, sem_t sFreeSpace, sem_t sWriteEnd, int *solution) -{ - sem_wait(&sWriteEnd); - - if (solution[0] == -1) { - sem_wait(&sFreeSpace); - shared->data[shared->tail] = -1; - shared->tail = (shared->tail + 1) % CIRC_BUF_SIZE; - sem_post(&sUsedSpace); - } else { - int i = 0; - while (solution[i] != -1) { - sem_wait(&sFreeSpace); - shared->data[shared->tail] = solution[i]; - shared->tail = (shared->tail + 1) % CIRC_BUF_SIZE; - sem_post(&sUsedSpace); - i++; - } - } - - sem_post(&sWriteEnd); -} - void cleanup() { for (int v = 0; v < graph.V; v++) { @@ -235,6 +212,46 @@ void cleanup() sem_unlink(SEM_WRITE_END); } + +void wait_sem(sem_t *sem) +{ + while (sem_wait(sem) == -1) { // interrupted by syscall? + if (errno == EINTR) { + if (quit == 1) { + cleanup(); + exit(EXIT_SUCCESS); + } + else continue; + } + cleanup(); + exit(EXIT_FAILURE); + } + return; +} + +void putSolution(int *solution) +{ + wait_sem(sWriteEnd); + + if (solution[0] == -1) { + wait_sem(sFreeSpace); + shared->data[shared->tail] = -1; + shared->tail = (shared->tail + 1) % CIRC_BUF_SIZE; + wait_sem(sUsedSpace); + } else { + int i = 0; + while (solution[i] != -1) { + wait_sem(sFreeSpace); + shared->data[shared->tail] = solution[i]; + shared->tail = (shared->tail + 1) % CIRC_BUF_SIZE; + wait_sem(sUsedSpace); + i++; + } + } + + sem_post(sWriteEnd); +} + int findIndex(int array[], int value, int size) { int index = 0; @@ -339,19 +356,7 @@ int main(int argc, char *argv[]) int solution[MAX_ITEMS]; genSolution(permutation, graph.V, solution); - printf("["); - if (solution[0] == -1) { - printf("]\n"); - break; - } - int j; - for (j = 0; j < (MAX_ITEMS - 1); j++) { - if (solution[j+1] != -1) - printf("%d, ", solution[j]); - else - break; - } - printf("%d]\n", solution[j]); + putSolution(solution); } cleanup();