Write complete solution array to shared memory without any delimiters

This commit is contained in:
zenon 2018-06-08 11:03:54 +02:00
parent 065f445c79
commit 051a727caf
2 changed files with 6 additions and 21 deletions

View File

@ -225,20 +225,11 @@ void putSolution(int *solution)
{
wait_sem(sWriteEnd);
if (solution[0] == -1) {
wait_sem(sFreeSpace);
shared->data[shared->tail] = -1;
shared->tail = (shared->tail + 1) % MAX_ITEMS;
sem_post(sUsedSpace);
} else {
int i = 0;
while (solution[i] != -1) {
for (int i = 0; i < MAX_ITEMS; i++) {
wait_sem(sFreeSpace);
shared->data[shared->tail] = solution[i];
shared->tail = (shared->tail + 1) % MAX_ITEMS;
sem_post(sUsedSpace);
i++;
}
}
sem_post(sWriteEnd);

View File

@ -91,17 +91,11 @@ void getSolution(sem_t *sUsedSpace, sem_t *sFreeSpace)
solution[i] = -1;
}
if (shared->data[shared->head] == -1) {
shared->head = (shared->head + 1) % MAX_ITEMS;
} else {
int i = 0;
while (shared->data[shared->head] != -1) {
for (int i = 0; i < MAX_ITEMS; i++) {
wait_sem(sUsedSpace);
solution[i] = shared->data[shared->head];
shared->head = (shared->head + 1) % MAX_ITEMS;
sem_post(sFreeSpace);
i++;
}
}
// Code for testing if new solution is better than old