From: Simone Piccardi Date: Thu, 30 Dec 2010 22:40:50 +0000 (+0000) Subject: Adding signal handler to remove shared mem and semaphore X-Git-Url: https://gapil.gnulinux.it/gitweb/?a=commitdiff_plain;h=b89b7fbeeba4fab9f8990dd8373cdc41c7052eeb;p=gapil.git Adding signal handler to remove shared mem and semaphore --- diff --git a/sources/message_getter.c b/sources/message_getter.c index 3c94b2d..e1aff0c 100644 --- a/sources/message_getter.c +++ b/sources/message_getter.c @@ -41,16 +41,17 @@ */ /* Help printing routine */ void usage(void); +void HandSigInt(int sig); #define MSGMAXSIZE 256 +char *shmname = "messages"; +char *semname = "messages"; int main(int argc, char *argv[]) { int i; sem_t * sem; time_t t; - char *shmname = "messages"; - char *semname = "messages"; char * res; void * shm_ptr; /* @@ -92,8 +93,8 @@ int main(int argc, char *argv[]) printf("Wrong number of arguments %d\n", argc - optind); usage(); } + Signal(SIGINT, HandSigInt); // Get shared memory segment - RemoveShm(shmname); shm_ptr = CreateShm(shmname, MSGMAXSIZE, 0666, 0); if ( shm_ptr == NULL) { perror("Cannot find shared memory"); @@ -150,3 +151,10 @@ void usage(void) { printf(" -s semname use semname semaphore\n"); exit(1); } + +void HandSigInt(int sig) +{ + RemoveShm(shmname); + sem_unlink(semname); + exit(0); +}