3 * Copyright (C) 2002 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 /*****************************************************************************
23 * An example of POSIX shared memory use: write some content to a
24 * shared memory segment
26 * Author: S. Piccardi Jan. 2003
28 *****************************************************************************/
29 #include <sys/types.h> /* primitive system data types */
30 #include <sys/stat.h> /* file characteristics constants and functions */
31 #include <dirent.h> /* directory operation constants and functions */
32 #include <stdlib.h> /* C standard library */
33 #include <unistd.h> /* unix standard library */
34 #include <stdio.h> /* standard I/O library */
39 /* Help printing routine */
42 int main(int argc, char *argv[])
46 char * file_name = NULL;
52 * Input section: decode command line parameters
55 opterr = 0; /* don't want writing to stderr */
56 while ( (i = getopt(argc, argv, "hs:f:")) != -1) {
61 case 's': /* set pause (in sec.) */
62 size = strtol(optarg, NULL, 10);
64 case 'f': /* set pause (in sec.) */
67 case 'h': /* help option */
68 printf("Wrong -h option use\n");
72 case '?': /* unrecognized options */
73 printf("Unrecognized options -%c\n",optopt);
75 default: /* should not reached */
79 /* ***********************************************************
81 * Options processing completed
85 * ***********************************************************/
86 if ((argc - optind) != 1) { /* There must be remaing parameters */
87 printf("Wrong number of arguments %d\n", argc - optind);
91 mutex_file = tempnam("/tmp","mutex");
92 if ((mutex = CreateMutex(mutex_file)) == -1) {
93 perror("Cannot create mutex");
97 shm_ptr = CreateShm(argv[1], size, 0644, 0);
98 if (shm_ptr == NULL) {
99 perror("Error creating shared memory");
103 fd = open(file_name, O_RDONLY);
105 FullRead(fd, shm_ptr, size);
106 // RemoveShm(argv[1]);
107 RemoveMutex(mutex_file);
111 RemoveMutex(mutex_file);
116 * routine to print usage info and exit
119 printf("Program myls: list file in a directory \n");
121 printf(" WriteShm [-h] [-s size] [-f file] name \n");
122 printf(" -h print this help\n");
123 printf(" -s size size of segment \n");
124 printf(" -f file filename to read contents \n");