From: Simone Piccardi Date: Thu, 30 Dec 2010 22:17:36 +0000 (+0000) Subject: Due nuovi programmi per illustrare i semafori POSIX. X-Git-Url: https://gapil.gnulinux.it/gitweb/?a=commitdiff_plain;h=5d621249af8897e27fc0a842a33e7a7ef3b9c2ca;p=gapil.git Due nuovi programmi per illustrare i semafori POSIX. --- diff --git a/sources/Makefile b/sources/Makefile index 2d3a1dc..e3089fa 100644 --- a/sources/Makefile +++ b/sources/Makefile @@ -49,6 +49,12 @@ fortune: FortuneClient.c fortuned: FortuneServer.c FortuneParse.c $(CC) $(CFLAGJ) $^ -o $@ +message_setter: message_setter.c + $(CC) $(CFLAGJ) $^ -o $@ + +message_getter: message_getter.c + $(CC) $(CFLAGJ) $^ -o $@ + barcode: BarCode.c $(CC) $^ -o $@ diff --git a/sources/ProcInfo.c b/sources/ProcInfo.c index d4c012f..2ffbdee 100644 --- a/sources/ProcInfo.c +++ b/sources/ProcInfo.c @@ -18,8 +18,8 @@ */ /**************************************************************** * - * Program test_fopen.c: - * Program to test function fopen + * Program ProcInfo.c: + * Program to ??? Incomplete * * Author: Simone Piccardi * Dec. 2001 @@ -98,9 +98,9 @@ int main(int argc, char *argv[]) * routine to print usage info and exit */ void usage(void) { - printf("Program testfopen : test fopen for a file \n"); + printf("Program procinfo : ??? \n"); printf("Usage:\n"); - printf(" testfopen [-h] file mode \n"); + printf(" procinfo [-h] file mode \n"); printf(" -h print this help\n"); exit(1); diff --git a/sources/message_getter.c b/sources/message_getter.c new file mode 100644 index 0000000..3c94b2d --- /dev/null +++ b/sources/message_getter.c @@ -0,0 +1,152 @@ +/* message_getter.c + * + * Copyright (C) 2010 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 message_setter: Get amd print a message in a shared memory segment, + * protecting it with a semaphore. + * + * Author: S. Piccardi Jan. 2010 + * + *****************************************************************************/ +#include /* primitive system data types */ +#include /* file characteristics constants and functions */ +#include /* directory operation constants and functions */ +#include /* unix standard library */ +#include /* C standard library */ +#include /* C strings library */ +#include + +#include +#include "Gapil.h" +/* + * Program message_setter + * + * List files and their size inside a given directory + */ +/* Help printing routine */ +void usage(void); + +#define MSGMAXSIZE 256 + +int main(int argc, char *argv[]) +{ + int i; + sem_t * sem; + time_t t; + char *shmname = "messages"; + char *semname = "messages"; + char * res; + 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, "hf:s:")) != -1) { + switch (i) { + /* + * Handling options + */ + case 'h': /* help option */ + printf("Wrong -h option use\n"); + usage(); + return -1; + break; + case 'f': + shmname = optarg; + break; + case 's': + semname = optarg; + 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(); + } + // Get shared memory segment + RemoveShm(shmname); + shm_ptr = CreateShm(shmname, MSGMAXSIZE, 0666, 0); + if ( shm_ptr == NULL) { + perror("Cannot find shared memory"); + exit(1); + } + // set initial string + strncpy((char *) shm_ptr, argv[optind], MSGMAXSIZE); + // open the semaphore or create it locked + if ( (sem = sem_open(semname, O_CREAT, 0666, 0)) == SEM_FAILED ) { + perror("Cannot open semaphore"); + exit(1); + } + // check if first time creation, and unlock + if ( sem_getvalue(sem, &i) != 0) { + perror("cannot get initial semaphore value"); + exit(1); + } else { + if (i == 0) { + if ( sem_post(sem) != 0) { + perror("cannot do semaphore initial release"); + exit(1); + } + } + } + while(1) { + // acquire semaphore + if ( sem_wait(sem) != 0) { + perror("cannot use semaphore"); + exit(1); + } + t = time(NULL); + printf("%s", ctime(&t)); + sem_getvalue(sem, &i); + printf("sem=%i, ", i); + printf("message: %s\n", (char *) shm_ptr ); + if ( sem_post(sem) != 0) { + perror("cannot release semaphore"); + exit(1); + } + sleep(1); + } +exit(0); +} + +/* + * routine to print usage info and exit + */ +void usage(void) { + printf("Program message_setter: put a message in shared memory segment \n"); + printf("Usage:\n"); + printf(" message_setter [-h] [-f shmname] message \n"); + printf(" -h print this help\n"); + printf(" -f shmname use shmname memory segment\n"); + printf(" -s semname use semname semaphore\n"); + exit(1); +} diff --git a/sources/message_setter.c b/sources/message_setter.c new file mode 100644 index 0000000..f6a8408 --- /dev/null +++ b/sources/message_setter.c @@ -0,0 +1,132 @@ +/* message_setter.c + * + * Copyright (C) 2010 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 message_setter: Set a message in a shared memory segment, + * protecting it with a semaphore. + * + * Author: S. Piccardi Jan. 2010 + * + *****************************************************************************/ +#include /* primitive system data types */ +#include /* file characteristics constants and functions */ +#include /* directory operation constants and functions */ +#include /* unix standard library */ +#include /* C standard library */ +#include /* C strings library */ + +#include +#include "Gapil.h" +/* + * Program message_setter + * + * List files and their size inside a given directory + */ +/* Help printing routine */ +void usage(void); + +#define MSGMAXSIZE 256 + +int main(int argc, char *argv[]) +{ + int i, t = 0; + sem_t * sem; + char *shmname = "messages"; + char *semname = "messages"; + 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, "hf:s:t:")) != -1) { + switch (i) { + /* + * Handling options + */ + case 'h': /* help option */ + printf("Wrong -h option use\n"); + usage(); + return -1; + break; + case 'f': + shmname = optarg; + break; + case 's': + semname = optarg; + break; + case 't': + t = strtol(optarg, NULL, 10); + 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(); + } + // Get shared memory segment + shm_ptr = FindShm(shmname, MSGMAXSIZE); + if ( shm_ptr == NULL) { + perror("Cannot find shared memory"); + exit(1); + } + // open semaphore + if ( (sem = sem_open(semname, 0)) == SEM_FAILED ) { + perror("Cannot open semaphore"); + exit(1); + } + if ( sem_wait(sem) != 0) { + perror("cannot use semaphore"); + exit(1); + } + strncpy((char *) shm_ptr, argv[optind], MSGMAXSIZE); + printf("Sleeping for %i seconds\n", t); + sleep(t); + if ( sem_post(sem) != 0) { + perror("cannot release semaphore"); + exit(1); + } + exit(0); +} + +/* + * routine to print usage info and exit + */ +void usage(void) { + printf("Program message_setter: put a message in shared memory segment \n"); + printf("Usage:\n"); + printf(" message_setter [-h] [-f shmname] message \n"); + printf(" -h print this help\n"); + printf(" -f shmname use shmname memory segment\n"); + printf(" -s semname use semname semaphore\n"); + printf(" -n time seconds to wait\n"); + exit(1); +}