Aggiunto il programma di lettura che era rimasto fuori, fatte alcune
authorSimone Piccardi <piccardi@gnulinux.it>
Fri, 10 Jan 2003 09:28:49 +0000 (09:28 +0000)
committerSimone Piccardi <piccardi@gnulinux.it>
Fri, 10 Jan 2003 09:28:49 +0000 (09:28 +0000)
correzioni al riguardo

ipc.tex
sources/DirMonitor.c
sources/ReadMonitor.c [new file with mode: 0644]

diff --git a/ipc.tex b/ipc.tex
index c93fe7a870ed40a662b7a1c01a4464c359566e7a..6cfa2e68acc5e89dce280798d8d02f41d0d45e93 100644 (file)
--- a/ipc.tex
+++ b/ipc.tex
@@ -3085,10 +3085,10 @@ int ComputeValues(struct dirent * direntry)
     if (S_ISREG(data.st_mode)) shmptr->tot_regular++;
     if (S_ISFIFO(data.st_mode)) shmptr->tot_fifo++;
     if (S_ISLNK(data.st_mode)) shmptr->tot_link++;
     if (S_ISREG(data.st_mode)) shmptr->tot_regular++;
     if (S_ISFIFO(data.st_mode)) shmptr->tot_fifo++;
     if (S_ISLNK(data.st_mode)) shmptr->tot_link++;
-    if (S_ISDIR(data.st_mode)) shmptr->tot_dir;
-    if (S_ISBLK(data.st_mode)) shmptr->tot_block;
-    if (S_ISCHR(data.st_mode)) shmptr->tot_char;
-    if (S_ISSOCK(data.st_mode)) shmptr->tot_sock;
+    if (S_ISDIR(data.st_mode)) shmptr->tot_dir++;
+    if (S_ISBLK(data.st_mode)) shmptr->tot_block++;
+    if (S_ISCHR(data.st_mode)) shmptr->tot_char++;
+    if (S_ISSOCK(data.st_mode)) shmptr->tot_sock++;
     return 0;
 }
 /*
     return 0;
 }
 /*
@@ -3186,8 +3186,6 @@ int main(int argc, char *argv[])
     }
     /* main loop */
     MutexLock(mutex);                                  /* lock shared memory */
     }
     /* main loop */
     MutexLock(mutex);                                  /* lock shared memory */
-    printf("File presenti %d file, per un totale di %d byte\n",
-           shmptr->tot_files, shmptr->tot_size);
     printf("Ci sono %d file dati\n", shmptr->tot_regular);
     printf("Ci sono %d directory\n", shmptr->tot_dir);
     printf("Ci sono %d link\n", shmptr->tot_link);
     printf("Ci sono %d file dati\n", shmptr->tot_regular);
     printf("Ci sono %d directory\n", shmptr->tot_dir);
     printf("Ci sono %d link\n", shmptr->tot_link);
@@ -3195,6 +3193,8 @@ int main(int argc, char *argv[])
     printf("Ci sono %d socket\n", shmptr->tot_sock);
     printf("Ci sono %d device a caratteri\n", shmptr->tot_char);
     printf("Ci sono %d device a blocchi\n", shmptr->tot_block);
     printf("Ci sono %d socket\n", shmptr->tot_sock);
     printf("Ci sono %d device a caratteri\n", shmptr->tot_char);
     printf("Ci sono %d device a blocchi\n", shmptr->tot_block);
+    printf("Totale  %d file, per %d byte\n",
+           shmptr->tot_files, shmptr->tot_size);
     MutexUnlock(mutex);                              /* unlock shared memory */
 }
     \end{lstlisting}
     MutexUnlock(mutex);                              /* unlock shared memory */
 }
     \end{lstlisting}
@@ -3220,6 +3220,10 @@ mutex (qui avviene il blocco se la memoria condivisa non 
 (\texttt{\small 32--40}) si stampano i vari valori in essa contenuti. Infine
 (\texttt{\small 41}) si rilascia il mutex. 
 
 (\texttt{\small 32--40}) si stampano i vari valori in essa contenuti. Infine
 (\texttt{\small 41}) si rilascia il mutex. 
 
+Verifichiamo allora il funzionamento del nostro monitor; 
+
+
+
 
 %% Per capire meglio il funzionamento delle funzioni facciamo ancora una volta
 %% riferimento alle strutture con cui il kernel implementa i segmenti di memoria
 
 %% Per capire meglio il funzionamento delle funzioni facciamo ancora una volta
 %% riferimento alle strutture con cui il kernel implementa i segmenti di memoria
@@ -3458,12 +3462,12 @@ usano i semafori, anche se le due interfacce non possono essere completamente
 equivalenti, specie per quanto riguarda la rimozione del mutex.
 
 La prima funzione (\texttt{\small 1--5}) è \func{CreateMutex}, e serve a
 equivalenti, specie per quanto riguarda la rimozione del mutex.
 
 La prima funzione (\texttt{\small 1--5}) è \func{CreateMutex}, e serve a
-creare il mutex; la funzione è estremamente semplice, si limita
+creare il mutex; la funzione è estremamente semplice, si limita
 (\texttt{\small 4}) a creare, con una opportuna chiamata ad \func{open}, il
 file che sarà usato per il successivo file locking, assicurandosi che non
 esista già (nel qual caso segnala un errore); poi restituisce il file
 descriptor che sarà usato dalle altre funzioni per acquisire e rilasciare il
 (\texttt{\small 4}) a creare, con una opportuna chiamata ad \func{open}, il
 file che sarà usato per il successivo file locking, assicurandosi che non
 esista già (nel qual caso segnala un errore); poi restituisce il file
 descriptor che sarà usato dalle altre funzioni per acquisire e rilasciare il
-mutex.  
+mutex.
 
 La seconda funzione (\texttt{\small 6--10}) è \func{FindMutex}, che, come la
 precedente, è stata definita per mantenere una analogia con la corrispondente
 
 La seconda funzione (\texttt{\small 6--10}) è \func{FindMutex}, che, come la
 precedente, è stata definita per mantenere una analogia con la corrispondente
@@ -3499,7 +3503,22 @@ disponibili fintanto che i relativi file descriptor restano aperti. Pertanto
 per rilasciare un mutex occorrerà prima chiamare \func{UnlockMutex} oppure
 chiudere il file usato per il lock.
 
 per rilasciare un mutex occorrerà prima chiamare \func{UnlockMutex} oppure
 chiudere il file usato per il lock.
 
-La sesta funzione (\texttt{\small 41--56}) legge lo stato del mutex.
+La sesta funzione (\texttt{\small 41--56}) è \func{ReadMutex} e serve a
+leggere lo stato del mutex. In questo caso si prepara (\texttt{\small 47--50})
+la solita struttura \var{lock} come l'acquisizione del lock, ma si effettua
+(\texttt{\small 52}) la chiamata a \func{fcntl} usando il comando
+\const{F\_GETLK} per ottenere lo stato del lock, e si restituisce
+(\texttt{\small 53}) il valore di ritorno in caso di errore, ed il valore del
+campo \var{l\_type} (che descrive lo stato del lock) altrimenti. Per questo
+motivo la funzione restituirà -1 in caso di errore e uno dei due valori
+\const{F\_UNLCK} o \const{F\_WRLCK}\footnote{non si dovrebbe mai avere il
+  terzo valore possibile, \const{F\_RDLCK}, dato che la nostra interfaccia usa
+  solo i write lock. Però è sempre possibile che siano richiesti altri lock
+  sul file al di fuori dell'interfaccia, nel qual caso si potranno avere,
+  ovviamente, interferenze indesiderate.} ) in caso di successo, ed indicare
+che il mutex è, rispettivamente libero o occupato.
+
+
 
 Basandosi sulla semantica dei file lock POSIX valgono tutte le precisazioni
 relative al comportamento di questi ultimi fatte in
 
 Basandosi sulla semantica dei file lock POSIX valgono tutte le precisazioni
 relative al comportamento di questi ultimi fatte in
index 47253548ab1f5ea769f91dd492f6306b10698c09..2f11833a800479a96f6c0bff6edbf34a03efc26d 100644 (file)
@@ -25,7 +25,7 @@
  *
  * Author: S. Piccardi Jan. 2003
  *
  *
  * Author: S. Piccardi Jan. 2003
  *
- * $Id: DirMonitor.c,v 1.3 2003/01/06 16:44:20 piccardi Exp $
+ * $Id: DirMonitor.c,v 1.4 2003/01/10 09:28:49 piccardi Exp $
  *
  *****************************************************************************/
 #include <sys/types.h>
  *
  *****************************************************************************/
 #include <sys/types.h>
@@ -137,10 +137,10 @@ int ComputeValues(struct dirent * direntry)
     if (S_ISREG(data.st_mode)) shmptr->tot_regular++;
     if (S_ISFIFO(data.st_mode)) shmptr->tot_fifo++;
     if (S_ISLNK(data.st_mode)) shmptr->tot_link++;
     if (S_ISREG(data.st_mode)) shmptr->tot_regular++;
     if (S_ISFIFO(data.st_mode)) shmptr->tot_fifo++;
     if (S_ISLNK(data.st_mode)) shmptr->tot_link++;
-    if (S_ISDIR(data.st_mode)) shmptr->tot_dir;
-    if (S_ISBLK(data.st_mode)) shmptr->tot_block;
-    if (S_ISCHR(data.st_mode)) shmptr->tot_char;
-    if (S_ISSOCK(data.st_mode)) shmptr->tot_sock;
+    if (S_ISDIR(data.st_mode)) shmptr->tot_dir++;
+    if (S_ISBLK(data.st_mode)) shmptr->tot_block++;
+    if (S_ISCHR(data.st_mode)) shmptr->tot_char++;
+    if (S_ISSOCK(data.st_mode)) shmptr->tot_sock++;
     return 0;
 }
 /*
     return 0;
 }
 /*
diff --git a/sources/ReadMonitor.c b/sources/ReadMonitor.c
new file mode 100644 (file)
index 0000000..80a77c7
--- /dev/null
@@ -0,0 +1,129 @@
+/* ReadMonitor.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 ReadMonitor: 
+ *
+ * An example for shared memory use: read data saved in a shared
+ * memory segment from the DirMonitor program
+ *
+ * Author: S. Piccardi Jan. 2003
+ *
+ * $Id: ReadMonitor.c,v 1.1 2003/01/10 09:28:49 piccardi Exp $
+ *
+ *****************************************************************************/
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>        /* directory */
+#include <stdlib.h>        /* C standard library */
+#include <unistd.h>
+
+#include "Gapil.h"
+#include "macros.h"
+
+/* Help printing routine */
+void usage(void);
+
+/* variables for shared memory segment */
+int shmid; 
+struct DirProp {
+    int tot_size;    
+    int tot_files;   
+    int tot_regular; 
+    int tot_fifo;    
+    int tot_link;    
+    int tot_dir;     
+    int tot_block;   
+    int tot_char;    
+    int tot_sock;
+};
+struct DirProp *shmptr;
+int mutex;
+
+int main(int argc, char *argv[]) 
+{
+    int i;
+    key_t key;
+    /*
+     * Input section: decode command line parameters 
+     * Use getopt function
+     */
+    opterr = 0;         /* don't want writing to stderr */
+    while ( (i = getopt(argc, argv, "hs:l:wrbf")) != -1) {
+       switch (i) {
+       /* 
+        * Handling options 
+        */ 
+       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
+     * 
+     * ***********************************************************/
+    /* create needed IPC objects */
+    key = ftok("./DirMonitor.c", 1);                         /* define a key */
+    shmid = shmget(key, 4096, 0);                  /* get a shared memory ID */
+    if (shmid < 0) {
+       perror("Cannot find shared memory");
+       exit(1);
+    }
+    if ( (shmptr = shmat(shmid, NULL, 0)) == NULL ) {   /* attach to process */
+       perror("Cannot attach segment");
+       exit(1);
+    }
+    if ((mutex = MutexFind(key)) == -1) {                   /* get the Mutex */
+       perror("Cannot find mutex");
+       exit(1);
+    }
+    /* main loop */
+    MutexLock(mutex);                                  /* lock shared memory */
+    printf("Ci sono %d file dati\n", shmptr->tot_regular);
+    printf("Ci sono %d directory\n", shmptr->tot_dir);
+    printf("Ci sono %d link\n", shmptr->tot_link);
+    printf("Ci sono %d fifo\n", shmptr->tot_fifo);
+    printf("Ci sono %d socket\n", shmptr->tot_sock);
+    printf("Ci sono %d device a caratteri\n", shmptr->tot_char);
+    printf("Ci sono %d device a blocchi\n", shmptr->tot_block);
+    printf("Totale  %d file, per %d byte\n",
+           shmptr->tot_files, shmptr->tot_size);
+    MutexUnlock(mutex);                              /* unlock shared memory */
+}
+/*
+ * routine to print usage info and exit
+ */
+void usage(void) {
+    printf("Program myls: list file in a directory \n");
+    printf("Usage:\n");
+    printf("  myls [-h] dirname \n");
+    printf("  -h          print this help\n");
+    exit(1);
+}