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 $@
*/
/****************************************************************
*
- * Program test_fopen.c:
- * Program to test function fopen
+ * Program ProcInfo.c:
+ * Program to ??? Incomplete
*
* Author: Simone Piccardi
* Dec. 2001
* 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);
--- /dev/null
+/* 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 <sys/types.h> /* primitive system data types */
+#include <sys/stat.h> /* file characteristics constants and functions */
+#include <dirent.h> /* directory operation constants and functions */
+#include <unistd.h> /* unix standard library */
+#include <stdlib.h> /* C standard library */
+#include <string.h> /* C strings library */
+#include <time.h>
+
+#include <semaphore.h>
+#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);
+}
--- /dev/null
+/* 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 <sys/types.h> /* primitive system data types */
+#include <sys/stat.h> /* file characteristics constants and functions */
+#include <dirent.h> /* directory operation constants and functions */
+#include <unistd.h> /* unix standard library */
+#include <stdlib.h> /* C standard library */
+#include <string.h> /* C strings library */
+
+#include <semaphore.h>
+#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);
+}