From 8f91b5f974caa43e72a3ceddea81a0daca2a9cf3 Mon Sep 17 00:00:00 2001 From: zenon Date: Thu, 7 Jun 2018 17:46:00 +0200 Subject: [PATCH] Added cleanup() function --- supervisor.c | 73 +++++++++++++++++----------------------------------- 1 file changed, 23 insertions(+), 50 deletions(-) diff --git a/supervisor.c b/supervisor.c index 23126c6..6b5c352 100644 --- a/supervisor.c +++ b/supervisor.c @@ -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; }