Write whole solutions to shared memory

This commit is contained in:
Kranklyboy 2018-06-07 12:18:36 +02:00
parent 10ec964fec
commit 5a9d00c48a

View File

@ -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);
}