Add own wait function

This commit is contained in:
zenon 2018-06-07 17:08:49 +02:00
parent 8b9d48e4d8
commit 3057b4a0d4

View File

@ -38,6 +38,20 @@ void initCircBuf()
return;
}
void wait_sem(sem_t *sem)
{
while (sem_wait(sem) == -1) { // interrupted by system call?
printf("Interupted with error!\n"); //Just to check
if (errno == EINTR) {
if(quit == 1)
exit(EXIT_SUCCESS);
else continue;
}
exit(EXIT_FAILURE);
}
return;
}
void getSolution(sem_t sUsedSpace, sem_t sFreeSpace)
{
int solution[MAX_ITEMS];
@ -46,13 +60,13 @@ void getSolution(sem_t sUsedSpace, sem_t sFreeSpace)
}
if (shared->head == -1) {
sem_wait(&sUsedSpace);
wait_sem(&sUsedSpace);
shared->head = (shared->head + 1) % MAX_ITEMS;
sem_post(&sFreeSpace);
} else {
int i = 0;
while (shared->head != -1) {
sem_wait(&sUsedSpace);
wait_sem(&sUsedSpace);
solution[i] = shared->head;
shared->head = (shared->head + 1) % MAX_ITEMS;
sem_post(&sFreeSpace);