Due nuovi programmi per illustrare i semafori POSIX.
[gapil.git] / sources / message_getter.c
1 /* message_getter.c
2  * 
3  * Copyright (C) 2010 Simone Piccardi
4  * 
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.
9  * 
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.
14  * 
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.
18  */
19 /*****************************************************************************
20  *
21  * File message_setter: Get amd print a message in a shared memory segment,
22  * protecting it with a semaphore. 
23  *
24  * Author: S. Piccardi Jan. 2010
25  *
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 */
33 #include <time.h>
34
35 #include <semaphore.h>
36 #include "Gapil.h"
37 /*
38  * Program message_setter
39  *
40  * List files and their size inside a given directory
41  */
42 /* Help printing routine */
43 void usage(void);
44
45 #define MSGMAXSIZE 256
46
47 int main(int argc, char *argv[]) 
48 {
49     int i;
50     sem_t * sem;
51     time_t t;
52     char *shmname = "messages";
53     char *semname = "messages";
54     char * res;
55     void * shm_ptr;
56     /*
57      * Input section: decode command line parameters 
58      * Use getopt function
59      */
60     opterr = 0;  /* don't want writing to stderr */
61     while ( (i = getopt(argc, argv, "hf:s:")) != -1) {
62         switch (i) {
63         /* 
64          * Handling options 
65          */ 
66         case 'h':                                            /* help option */
67             printf("Wrong -h option use\n");
68             usage();
69             return -1;
70             break;
71         case 'f':
72             shmname = optarg;
73             break;
74         case 's':
75             semname = optarg;
76             break;
77         case '?':                                    /* unrecognized options */
78             printf("Unrecognized options -%c\n",optopt);
79             usage();
80         default:                                       /* should not reached */
81             usage();
82         }
83     }
84     /* ***********************************************************
85      * 
86      *           Options processing completed
87      *
88      *                Main code beginning
89      * 
90      * ***********************************************************/
91     if ((argc - optind) != 1) {          /* There must be remaing parameters */
92         printf("Wrong number of arguments %d\n", argc - optind);
93         usage();
94     }
95     // Get shared memory segment
96     RemoveShm(shmname);
97     shm_ptr = CreateShm(shmname, MSGMAXSIZE, 0666, 0);
98     if ( shm_ptr == NULL) {
99         perror("Cannot find shared memory");
100         exit(1);
101     }
102     // set initial string
103     strncpy((char *) shm_ptr, argv[optind], MSGMAXSIZE);
104     // open the semaphore or create it locked
105     if ( (sem = sem_open(semname, O_CREAT, 0666, 0)) == SEM_FAILED ) {
106         perror("Cannot open semaphore");
107         exit(1);
108     }
109     // check if first time creation, and unlock
110     if ( sem_getvalue(sem, &i) != 0) {
111         perror("cannot get initial semaphore value");
112         exit(1);
113     } else {
114         if (i == 0) {
115             if ( sem_post(sem) != 0) {
116                 perror("cannot do semaphore initial release");
117                 exit(1);
118             }
119         }
120     }   
121     while(1) {
122         // acquire semaphore
123         if ( sem_wait(sem) != 0) {
124             perror("cannot use semaphore");
125             exit(1);
126         }
127         t = time(NULL);
128         printf("%s", ctime(&t));
129         sem_getvalue(sem, &i);
130         printf("sem=%i,  ", i);
131         printf("message: %s\n", (char *) shm_ptr );
132         if ( sem_post(sem) != 0) {
133             perror("cannot release semaphore");
134             exit(1);
135         }
136         sleep(1);
137    }
138 exit(0);    
139 }
140
141 /*
142  * routine to print usage info and exit
143  */
144 void usage(void) {
145     printf("Program message_setter: put a message in shared memory segment \n");
146     printf("Usage:\n");
147     printf("  message_setter [-h] [-f shmname]  message \n");
148     printf("  -h           print this help\n");
149     printf("  -f shmname   use shmname memory segment\n");
150     printf("  -s semname   use semname semaphore\n");
151     exit(1);
152 }