Modify cleanup() function and make semaphores and shared memory global

This commit is contained in:
zenon 2018-06-07 17:46:41 +02:00
parent 8f91b5f974
commit 6ee735a030

View File

@ -13,6 +13,14 @@ static const char *pname;
static struct circ_buf *shared;
static int shmfd;
static sem_t *sUsedSpace;
static sem_t *sFreeSpace;
static sem_t *sWriteEnd;
struct sigaction sa;
struct AdjListNode {
@ -205,6 +213,26 @@ void cleanup()
}
}
free(graph.array);
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_close(sWriteEnd);
sem_unlink(SEM_USED_SPACE);
sem_unlink(SEM_FREE_SPACE);
sem_unlink(SEM_WRITE_END);
}
int findIndex(int array[], int value, int size)
@ -276,7 +304,7 @@ int main(int argc, char *argv[])
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
int shmfd = shm_open(SHM_NAME, O_RDWR | O_CREAT, PERMISSION);
shmfd = shm_open(SHM_NAME, O_RDWR | O_CREAT, PERMISSION);
if (shmfd == -1) {
perror(pname);
exit(EXIT_FAILURE);
@ -288,9 +316,9 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
sem_t *sFreeSpace = sem_open(SEM_FREE_SPACE, 0);
sem_t *sUsedSpace = sem_open(SEM_USED_SPACE, 0);
sem_t *sWriteEnd = sem_open(SEM_WRITE_END, O_CREAT, PERMISSION, 1);
sFreeSpace = sem_open(SEM_FREE_SPACE, 0);
sUsedSpace = sem_open(SEM_USED_SPACE, 0);
sWriteEnd = sem_open(SEM_WRITE_END, O_CREAT, PERMISSION, 1);
if (sFreeSpace == SEM_FAILED ||
sUsedSpace == SEM_FAILED ||
sWriteEnd == SEM_FAILED) {