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