X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2Fmessage_getter.c;h=8b3ec2d3c0856bffc35f74b61cfe5a0975303647;hp=3c94b2d4bc46cf845fb58e45a420769fcf4a2884;hb=26f7a8bb19c6cb198c213757a97b6ac79e40db4b;hpb=5d621249af8897e27fc0a842a33e7a7ef3b9c2ca diff --git a/sources/message_getter.c b/sources/message_getter.c index 3c94b2d..8b3ec2d 100644 --- a/sources/message_getter.c +++ b/sources/message_getter.c @@ -41,18 +41,18 @@ */ /* 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; + time_t t; /* * Input section: decode command line parameters * Use getopt function @@ -92,44 +92,39 @@ int main(int argc, char *argv[]) printf("Wrong number of arguments %d\n", argc - optind); usage(); } - // Get shared memory segment - RemoveShm(shmname); - shm_ptr = CreateShm(shmname, MSGMAXSIZE, 0666, 0); - if ( shm_ptr == NULL) { + Signal(SIGINT, HandSigInt); + // get a shared memory segment + if ((shm_ptr = CreateShm(shmname, MSGMAXSIZE, 0666, 0)) == NULL) { perror("Cannot find shared memory"); exit(1); } - // set initial string - strncpy((char *) shm_ptr, argv[optind], MSGMAXSIZE); - // open the semaphore or create it locked - if ( (sem = sem_open(semname, O_CREAT, 0666, 0)) == SEM_FAILED ) { + // get a locked semaphore + if ((sem = sem_open(semname, O_CREAT|O_EXCL, 0666, 0)) == SEM_FAILED) { perror("Cannot open semaphore"); exit(1); } - // check if first time creation, and unlock - if ( sem_getvalue(sem, &i) != 0) { - perror("cannot get initial semaphore value"); + // set initial string + strncpy((char *) shm_ptr, argv[optind], MSGMAXSIZE); + // do initial release + if (sem_post(sem) != 0) { + perror("cannot do semaphore initial release"); exit(1); - } else { - if (i == 0) { - if ( sem_post(sem) != 0) { - perror("cannot do semaphore initial release"); - exit(1); - } - } - } + } + // main loop while(1) { - // acquire semaphore - if ( sem_wait(sem) != 0) { + if (sem_getvalue(sem, &i) !=0) { // get sem values + perror("cannot get semaphore value"); + exit(1); + } + printf("sem=%i, ", i); // print sem values + t = time(NULL); // get time + printf("%s", ctime(&t)); // print time + if (sem_wait(sem) != 0) { // acquire semaphore perror("cannot use semaphore"); exit(1); } - t = time(NULL); - printf("%s", ctime(&t)); - sem_getvalue(sem, &i); - printf("sem=%i, ", i); - printf("message: %s\n", (char *) shm_ptr ); - if ( sem_post(sem) != 0) { + printf("message: %s\n", (char *) shm_ptr ); // print message + if (sem_post(sem) != 0) { // release semaphore perror("cannot release semaphore"); exit(1); } @@ -150,3 +145,10 @@ void usage(void) { printf(" -s semname use semname semaphore\n"); exit(1); } + +void HandSigInt(int sig) +{ + if (RemoveShm(shmname) != 0) perror("Cannot remove shared memory"); + if (sem_unlink(semname)!= 0) perror("Cannot remove semaphore") ; + exit(0); +}