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: wwrite some content to a
24 * shared memory segment
26 * Author: S. Piccardi Jan. 2003
28 * $Id: WriteShm.c,v 1.1 2003/02/03 18:07:38 piccardi Exp $
30 *****************************************************************************/
31 #include <sys/types.h>
33 #include <dirent.h> /* directory */
34 #include <stdlib.h> /* C standard library */
41 /* Help printing routine */
44 int main(int argc, char *argv[])
48 char * file_name = NULL;
54 * Input section: decode command line parameters
57 opterr = 0; /* don't want writing to stderr */
58 while ( (i = getopt(argc, argv, "hs:f:")) != -1) {
63 case 's': /* set pause (in sec.) */
64 size = strtol(optarg, NULL, 10);
66 case 'f': /* set pause (in sec.) */
69 case 'h': /* help option */
70 printf("Wrong -h option use\n");
74 case '?': /* unrecognized options */
75 printf("Unrecognized options -%c\n",optopt);
77 default: /* should not reached */
81 /* ***********************************************************
83 * Options processing completed
87 * ***********************************************************/
88 if ((argc - optind) != 1) { /* There must be remaing parameters */
89 printf("Wrong number of arguments %d\n", argc - optind);
92 mutex_file = tempnam("/tmp","mutex");
93 if ((mutex = CreateMutex(mutex_file)) == -1) { /* get a Mutex */
94 perror("Cannot create mutex");
98 shm_ptr = CreateShm(argv[1], size, 0644, 0);
99 if (shm_ptr == NULL) {
100 perror("Error creating shared memory");
104 fd = open(file_name, O_RDONLY);
106 FullRead(fd, shm_ptr, size);
107 // RemoveShm(argv[1]);
108 RemoveMutex(mutex_file);
112 RemoveMutex(mutex_file);
117 * routine to print usage info and exit
120 printf("Program myls: list file in a directory \n");
122 printf(" WriteShm [-h] [-s size] [-f file] name \n");
123 printf(" -h print this help\n");
124 printf(" -s size size of segment \n");
125 printf(" -f file filename to read contents \n");