X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2FWriteShm.c;fp=sources%2FWriteShm.c;h=8f472d69e28c8e3a188ee1a1afa3259c8388a092;hp=0000000000000000000000000000000000000000;hb=b1ead1b930038e4f7cd6ee7f737d7ee4699a068c;hpb=d12bc3e1e4b3ee762036d1c226c3b2ba1a720fb9 diff --git a/sources/WriteShm.c b/sources/WriteShm.c new file mode 100644 index 0000000..8f472d6 --- /dev/null +++ b/sources/WriteShm.c @@ -0,0 +1,127 @@ +/* WriteShm.c + * + * Copyright (C) 2002 Simone Piccardi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +/***************************************************************************** + * + * File WriteShm.c: + * + * An example of POSIX shared memory use: wwrite some content to a + * shared memory segment + * + * Author: S. Piccardi Jan. 2003 + * + * $Id: WriteShm.c,v 1.3 2003/05/02 09:55:14 piccardi Exp $ + * + *****************************************************************************/ +#include +#include +#include /* directory */ +#include /* C standard library */ +#include +#include + +#include "Gapil.h" +#include "macros.h" + +/* Help printing routine */ +void usage(void); + +int main(int argc, char *argv[]) +{ + int i; + int size = 4096; + char * file_name = NULL; + int fd = 0; + int mutex; + char * mutex_file; + void * shm_ptr; + /* + * Input section: decode command line parameters + * Use getopt function + */ + opterr = 0; /* don't want writing to stderr */ + while ( (i = getopt(argc, argv, "hs:f:")) != -1) { + switch (i) { + /* + * Handling options + */ + case 's': /* set pause (in sec.) */ + size = strtol(optarg, NULL, 10); + break; + case 'f': /* set pause (in sec.) */ + file_name = optarg; + break; + case 'h': /* help option */ + printf("Wrong -h option use\n"); + usage(); + return -1; + break; + case '?': /* unrecognized options */ + printf("Unrecognized options -%c\n",optopt); + usage(); + default: /* should not reached */ + usage(); + } + } + /* *********************************************************** + * + * Options processing completed + * + * Main code beginning + * + * ***********************************************************/ + if ((argc - optind) != 1) { /* There must be remaing parameters */ + printf("Wrong number of arguments %d\n", argc - optind); + usage(); + } + mutex_file = tempnam("/tmp","mutex"); + if ((mutex = CreateMutex(mutex_file)) == -1) { /* get a Mutex */ + perror("Cannot create mutex"); + goto errmutex; + } + /* main loop */ + shm_ptr = CreateShm(argv[1], size, 0644, 0); + if (shm_ptr == NULL) { + perror("Error creating shared memory"); + goto errshared; + } + if (file_name) { + fd = open(file_name, O_RDONLY); + } + FullRead(fd, shm_ptr, size); +// RemoveShm(argv[1]); + RemoveMutex(mutex_file); + exit(0); + errshared: + RemoveShm(argv[1]); + RemoveMutex(mutex_file); + errmutex: + exit(1); +} +/* + * routine to print usage info and exit + */ +void usage(void) { + printf("Program myls: list file in a directory \n"); + printf("Usage:\n"); + printf(" WriteShm [-h] [-s size] [-f file] name \n"); + printf(" -h print this help\n"); + printf(" -s size size of segment \n"); + printf(" -f file filename to read contents \n"); + exit(1); +}