From 3af17e0d8446f28502c687b242d76d70a6c84e73 Mon Sep 17 00:00:00 2001 From: zenon Date: Thu, 7 Jun 2018 10:22:30 +0200 Subject: [PATCH] Write whole solution to shared memory and -1 if acyclic --- generator.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/generator.c b/generator.c index c3a0742..7e9142a 100644 --- a/generator.c +++ b/generator.c @@ -170,10 +170,24 @@ void printGraph() } } -void putSolution(sem_t sUsedSpace, sem_t sFreeSpace, int *solution) +void putSolution(sem_t sUsedSpace, sem_t sFreeSpace, sem_t sWriteEnd, int *solution) { - shared->data[shared->tail] = itemValue; - shared->tail = (shared->tail + 1) % MAX_ITEMS; + sem_wait(&sWriteEnd); + sem_wait(&sFreeSpace); + + if (solution[0] == -1) { + shared->data[shared->tail] = -1; + shared->tail = (shared->tail + 1) % MAX_ITEMS; + } + + int i = 0; + while (solution[i] != -1) { + shared->data[shared->tail] = solution[i]; + shared->tail = (shared->tail + 1) % MAX_ITEMS; + } + + sem_post(&sUsedSpace); + sem_post(&sWriteEnd); } void cleanup()