Added cleanup() function
This commit is contained in:
parent
51724a3637
commit
8f91b5f974
73
supervisor.c
73
supervisor.c
@ -35,6 +35,26 @@ void usage(void)
|
|||||||
exit(EXIT_FAILURE);
|
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()
|
void initCircBuf()
|
||||||
{
|
{
|
||||||
shared->quit = 0;
|
shared->quit = 0;
|
||||||
@ -49,41 +69,12 @@ void wait_sem(sem_t *sem)
|
|||||||
while (sem_wait(sem) == -1) { // interrupted by syscall?
|
while (sem_wait(sem) == -1) { // interrupted by syscall?
|
||||||
if (errno == EINTR) {
|
if (errno == EINTR) {
|
||||||
if (quit == 1) {
|
if (quit == 1) {
|
||||||
shared->quit = 1;
|
cleanup();
|
||||||
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);
|
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
else continue;
|
else continue;
|
||||||
}
|
}
|
||||||
shared->quit = 1;
|
cleanup();
|
||||||
|
|
||||||
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);
|
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -190,25 +181,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
shared->quit = 1;
|
cleanup();
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user