Rearrange cleanup() function and copy wait_sem() function from

supervisor
This commit is contained in:
zenon 2018-06-07 17:52:49 +02:00
parent 6ee735a030
commit de4ed6532c

View File

@ -178,29 +178,6 @@ void printGraph()
}
}
void putSolution(sem_t sUsedSpace, sem_t sFreeSpace, sem_t sWriteEnd, int *solution)
{
sem_wait(&sWriteEnd);
if (solution[0] == -1) {
sem_wait(&sFreeSpace);
shared->data[shared->tail] = -1;
shared->tail = (shared->tail + 1) % CIRC_BUF_SIZE;
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) % CIRC_BUF_SIZE;
sem_post(&sUsedSpace);
i++;
}
}
sem_post(&sWriteEnd);
}
void cleanup()
{
for (int v = 0; v < graph.V; v++) {
@ -235,6 +212,46 @@ void cleanup()
sem_unlink(SEM_WRITE_END);
}
void wait_sem(sem_t *sem)
{
while (sem_wait(sem) == -1) { // interrupted by syscall?
if (errno == EINTR) {
if (quit == 1) {
cleanup();
exit(EXIT_SUCCESS);
}
else continue;
}
cleanup();
exit(EXIT_FAILURE);
}
return;
}
void putSolution(int *solution)
{
wait_sem(sWriteEnd);
if (solution[0] == -1) {
wait_sem(sFreeSpace);
shared->data[shared->tail] = -1;
shared->tail = (shared->tail + 1) % CIRC_BUF_SIZE;
wait_sem(sUsedSpace);
} else {
int i = 0;
while (solution[i] != -1) {
wait_sem(sFreeSpace);
shared->data[shared->tail] = solution[i];
shared->tail = (shared->tail + 1) % CIRC_BUF_SIZE;
wait_sem(sUsedSpace);
i++;
}
}
sem_post(sWriteEnd);
}
int findIndex(int array[], int value, int size)
{
int index = 0;
@ -339,19 +356,7 @@ int main(int argc, char *argv[])
int solution[MAX_ITEMS];
genSolution(permutation, graph.V, solution);
printf("[");
if (solution[0] == -1) {
printf("]\n");
break;
}
int j;
for (j = 0; j < (MAX_ITEMS - 1); j++) {
if (solution[j+1] != -1)
printf("%d, ", solution[j]);
else
break;
}
printf("%d]\n", solution[j]);
putSolution(solution);
}
cleanup();