3 * Copyright (C) 2010 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /*****************************************************************************
21 * File message_setter: Get amd print a message in a shared memory segment,
22 * protecting it with a semaphore.
24 * Author: S. Piccardi Jan. 2010
26 *****************************************************************************/
27 #include <sys/types.h> /* primitive system data types */
28 #include <sys/stat.h> /* file characteristics constants and functions */
29 #include <dirent.h> /* directory operation constants and functions */
30 #include <unistd.h> /* unix standard library */
31 #include <stdlib.h> /* C standard library */
32 #include <string.h> /* C strings library */
35 #include <semaphore.h>
38 * Program message_setter
40 * List files and their size inside a given directory
42 /* Help printing routine */
44 void HandSigInt(int sig);
46 #define MSGMAXSIZE 256
47 char *shmname = "messages";
48 char *semname = "messages";
50 int main(int argc, char *argv[])
57 * Input section: decode command line parameters
60 opterr = 0; /* don't want writing to stderr */
61 while ( (i = getopt(argc, argv, "hf:s:")) != -1) {
66 case 'h': /* help option */
67 printf("Wrong -h option use\n");
77 case '?': /* unrecognized options */
78 printf("Unrecognized options -%c\n",optopt);
80 default: /* should not reached */
84 /* ***********************************************************
86 * Options processing completed
90 * ***********************************************************/
91 if ((argc - optind) != 1) { /* There must be remaing parameters */
92 printf("Wrong number of arguments %d\n", argc - optind);
95 Signal(SIGINT, HandSigInt);
96 // get a shared memory segment
97 if ((shm_ptr = CreateShm(shmname, MSGMAXSIZE, 0666, 0)) == NULL) {
98 perror("Cannot find shared memory");
101 // get a locked semaphore
102 if ((sem = sem_open(semname, O_CREAT|O_EXCL, 0666, 0)) == SEM_FAILED) {
103 perror("Cannot open semaphore");
106 // set initial string
107 strncpy((char *) shm_ptr, argv[optind], MSGMAXSIZE);
108 // do initial release
109 if (sem_post(sem) != 0) {
110 perror("cannot do semaphore initial release");
115 if (sem_getvalue(sem, &i) !=0) { // get sem values
116 perror("cannot get semaphore value");
119 printf("sem=%i, ", i); // print sem values
120 t = time(NULL); // get time
121 printf("%s", ctime(&t)); // print time
122 if (sem_wait(sem) != 0) { // acquire semaphore
123 perror("cannot use semaphore");
126 printf("message: %s\n", (char *) shm_ptr ); // print message
127 if (sem_post(sem) != 0) { // release semaphore
128 perror("cannot release semaphore");
137 * routine to print usage info and exit
140 printf("Program message_setter: put a message in shared memory segment \n");
142 printf(" message_setter [-h] [-f shmname] message \n");
143 printf(" -h print this help\n");
144 printf(" -f shmname use shmname memory segment\n");
145 printf(" -s semname use semname semaphore\n");
149 void HandSigInt(int sig)
151 if (RemoveShm(shmname) != 0) perror("Cannot remove shared memory");
152 if (sem_unlink(semname)!= 0) perror("Cannot remove semaphore") ;