diff --git a/generator.c b/generator.c index 8773c68..43d5259 100644 --- a/generator.c +++ b/generator.c @@ -170,15 +170,26 @@ void printGraph() } } -void putItem(sem_t sUsedSpace, sem_t sFreeSpace, sem_t sWriteEnd, int value) +void putSolution(sem_t sUsedSpace, sem_t sFreeSpace, sem_t sWriteEnd, int *solution) { sem_wait(&sWriteEnd); - sem_wait(&sFreeSpace); - shared->data[shared->tail] = -1; - shared->tail = (shared->tail + 1) % MAX_ITEMS; + if (solution[0] == -1) { + sem_wait(&sFreeSpace); + shared->data[shared->tail] = -1; + shared->tail = (shared->tail + 1) % MAX_ITEMS; + 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) % MAX_ITEMS; + sem_post(&sUsedSpace); + i++; + } + } - sem_post(&sUsedSpace); sem_post(&sWriteEnd); }