Write complete solution array to shared memory without any delimiters
This commit is contained in:
parent
065f445c79
commit
051a727caf
11
generator.c
11
generator.c
@ -225,20 +225,11 @@ void putSolution(int *solution)
|
|||||||
{
|
{
|
||||||
wait_sem(sWriteEnd);
|
wait_sem(sWriteEnd);
|
||||||
|
|
||||||
if (solution[0] == -1) {
|
for (int i = 0; i < MAX_ITEMS; i++) {
|
||||||
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) {
|
|
||||||
wait_sem(sFreeSpace);
|
wait_sem(sFreeSpace);
|
||||||
shared->data[shared->tail] = solution[i];
|
shared->data[shared->tail] = solution[i];
|
||||||
shared->tail = (shared->tail + 1) % MAX_ITEMS;
|
shared->tail = (shared->tail + 1) % MAX_ITEMS;
|
||||||
sem_post(sUsedSpace);
|
sem_post(sUsedSpace);
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sem_post(sWriteEnd);
|
sem_post(sWriteEnd);
|
||||||
|
|||||||
@ -91,17 +91,11 @@ void getSolution(sem_t *sUsedSpace, sem_t *sFreeSpace)
|
|||||||
solution[i] = -1;
|
solution[i] = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shared->data[shared->head] == -1) {
|
for (int i = 0; i < MAX_ITEMS; i++) {
|
||||||
shared->head = (shared->head + 1) % MAX_ITEMS;
|
|
||||||
} else {
|
|
||||||
int i = 0;
|
|
||||||
while (shared->data[shared->head] != -1) {
|
|
||||||
wait_sem(sUsedSpace);
|
wait_sem(sUsedSpace);
|
||||||
solution[i] = shared->data[shared->head];
|
solution[i] = shared->data[shared->head];
|
||||||
shared->head = (shared->head + 1) % MAX_ITEMS;
|
shared->head = (shared->head + 1) % MAX_ITEMS;
|
||||||
sem_post(sFreeSpace);
|
sem_post(sFreeSpace);
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Code for testing if new solution is better than old
|
// Code for testing if new solution is better than old
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user