Added cleanup() function

This commit is contained in:
zenon 2018-06-07 17:46:00 +02:00
parent 51724a3637
commit 8f91b5f974

View File

@ -35,6 +35,26 @@ void usage(void)
exit(EXIT_FAILURE);
}
void cleanup()
{
shared->quit = 1;
if (munmap(shared, sizeof(*shared)) == -1) {
perror("munmap");
exit(EXIT_FAILURE);
}
close(shmfd);
if (shm_unlink(SHM_NAME) == -1) {
perror("shm_unlink");
exit(EXIT_FAILURE);
}
sem_close(sUsedSpace);
sem_close(sFreeSpace);
sem_unlink(SEM_USED_SPACE);
sem_unlink(SEM_FREE_SPACE);
}
void initCircBuf()
{
shared->quit = 0;
@ -49,41 +69,12 @@ void wait_sem(sem_t *sem)
while (sem_wait(sem) == -1) { // interrupted by syscall?
if (errno == EINTR) {
if (quit == 1) {
shared->quit = 1;
if (munmap(shared, sizeof(*shared)) == -1) {
perror("munmap");
exit(EXIT_FAILURE);
}
close(shmfd);
if (shm_unlink(SHM_NAME) == -1) {
perror("shm_unlink");
exit(EXIT_FAILURE);
}
sem_close(sUsedSpace);
sem_close(sFreeSpace);
sem_unlink(SEM_USED_SPACE);
sem_unlink(SEM_FREE_SPACE);
cleanup();
exit(EXIT_SUCCESS);
}
else continue;
}
shared->quit = 1;
if (munmap(shared, sizeof(*shared)) == -1) {
perror("munmap");
exit(EXIT_FAILURE);
}
close(shmfd);
if (shm_unlink(SHM_NAME) == -1) {
perror("shm_unlink");
exit(EXIT_FAILURE);
}
sem_close(sUsedSpace);
sem_close(sFreeSpace);
sem_unlink(SEM_USED_SPACE);
sem_unlink(SEM_FREE_SPACE);
cleanup();
exit(EXIT_FAILURE);
}
return;
@ -190,25 +181,7 @@ int main(int argc, char *argv[])
}
shared->quit = 1;
if (munmap(shared, sizeof(*shared)) == -1) {
perror("munmap");
exit(EXIT_FAILURE);
}
close(shmfd);
if (shm_unlink(SHM_NAME) == -1) {
perror("shm_unlink");
exit(EXIT_FAILURE);
}
sem_close(sUsedSpace);
sem_close(sFreeSpace);
sem_unlink(SEM_USED_SPACE);
sem_unlink(SEM_FREE_SPACE);
cleanup();
return 0;
}