Inizio della shared memory
authorSimone Piccardi <piccardi@gnulinux.it>
Mon, 3 Feb 2003 14:27:58 +0000 (14:27 +0000)
committerSimone Piccardi <piccardi@gnulinux.it>
Mon, 3 Feb 2003 14:27:58 +0000 (14:27 +0000)
ipc.tex
sources/Gapil.h
sources/SharedMem.c

diff --git a/ipc.tex b/ipc.tex
index 78d612f91270dfb2a6e799ec7fd49f9d6df66c44..920787398b707674e396f1f056911993c7f8faf4 100644 (file)
--- a/ipc.tex
+++ b/ipc.tex
@@ -3817,15 +3817,16 @@ per i quali richiede che:
 \subsection{Code di messaggi}
 \label{sec:ipc_posix_mq}
 
 \subsection{Code di messaggi}
 \label{sec:ipc_posix_mq}
 
-Le code di messaggi non sono ancora supportate nel kernel
-ufficiale;\footnote{esiste però una proposta di implementazione di Krzysztof
-  Benedyczak, a partire dal kernel 2.5.50.}  inoltre esse possono essere
-implementate, usando la memoria condivisa ed i mutex, con funzioni di
-libreria.  In generale, come le corrispettive del SysV IPC, sono poco usate,
-dato che i socket\index{socket}, nei casi in cui sono sufficienti, sono più
-comodi, e negli altri casi la comunicazione può essere gestita direttamente
-con mutex e memoria condivisa. Per questo, in assenza di una implementazione
-uffiale, ne tralasciamo la descrizione.
+Le code di messaggi non sono ancora supportate nel kernel ufficiale, esiste
+però una implementazione sperimentale di Michal Wronski e Krzysztof
+Benedyczak,\footnote{i patch al kernel e la relativa libreria possono essere
+  trovati \href{http://www.mat.uni.torun.pl/~wrona/posix_ipc}
+  {http://www.mat.uni.torun.pl/\~wrona/posix_ipc}.}.  In generale, come le
+corrispettive del SysV IPC, sono poco usate, dato che i socket\index{socket},
+nei casi in cui sono sufficienti, sono più comodi, e negli altri casi la
+comunicazione può essere gestita direttamente con mutex e memoria condivisa.
+
+
 
 
 
 
 
 
@@ -3834,9 +3835,12 @@ uffiale, ne tralasciamo la descrizione.
 
 Dei semafori POSIX esistono sostanzialmente due implementazioni; una è fatta a
 livello di libreria ed è fornita dalla libreria dei thread; questa però li
 
 Dei semafori POSIX esistono sostanzialmente due implementazioni; una è fatta a
 livello di libreria ed è fornita dalla libreria dei thread; questa però li
-implementa solo a livello di thread e non di processi. Esiste un'altra
-versione, realizzata da Konstantin Knizhnik, che reimplementa l'interfaccia
-POSIX usando i semafori di SysV IPC.
+implementa solo a livello di thread e non di processi.\footnote{questo
+  significa che i semafori sono visibili solo all'interno dei thread creati da
+  un sngolo processo.} Esiste un'altra versione, realizzata da Konstantin
+Knizhnik, che reimplementa l'interfaccia POSIX usando i semafori di SysV IPC,
+e che non vale comunque la pena di usare visto che i problemi sottolineati in
+\secref{sec:ipc_sysv_sem} restano invariati.
 
 
 
 
 
 
@@ -3845,7 +3849,29 @@ POSIX usando i semafori di SysV IPC.
 \label{sec:ipc_posix_shm}
 
 La memoria condivisa è l'unico degli oggetti di IPC POSIX già presente nel
 \label{sec:ipc_posix_shm}
 
 La memoria condivisa è l'unico degli oggetti di IPC POSIX già presente nel
-kernel ufficiale. 
+kernel ufficiale. Per poterla utilizzare occorre abilitare il filesystem
+\texttt{tmpfs}, uno speciale filesystem che mantiene tutti i suoi contenuti in
+memoria,\footnote{il filesystem \texttt{tmpfs} è diverso da un normale RAM
+  disk, anch'esso disponibile attraverso il filesystem \texttt{ramfs}, proprio
+  perché realizza una interfaccia utilizzabile anche per la memoria condivisa;
+  esso infatti non ha dimensione fissa, ed usa direttamente la cache interna
+  del kernel (viene usato anche per la SysV shared memory). In più i suoi
+  contenuti, essendo trattati direttamente dalla memoria
+  virtuale\index{memoria virtuale} e possono essere salvati sullo swap
+  automaticamente.} abilitando l'opzione \texttt{CONFIG\_TMPFS} in fase di
+compilazione del kernel, e montando il filesystem aggiungendo una riga tipo:
+\begin{verbatim}
+tmpfs   /dev/shm        tmpfs   defaults        0      0
+\end{verbatim}
+ad \file{/etc/fstab}, oppure dove si preferisce con un comando del
+tipo:\footnote{il filesystem riconosce, oltre quelle mostrate, le opzioni
+  \texttt{uid} e \texttt{gid} che identificano rispettivamente utente e gruppo
+  cui assegnarne la titolarità, e \texttt{nr\_blocks} che permette di
+  specificarne la dimensione in blocchi, cioè in multipli di
+  \const{PAGECACHE\_SIZE}.}
+\begin{verbatim}
+mount -t tmpfs -o size=10G,nr_inodes=10k,mode=700 tmpfs /mytmpfs
+\end{verbatim}
 
 
 %%% Local Variables: 
 
 
 %%% Local Variables: 
index 401871c611792106fc9922a6405fb7a0bf642966..17f07fa56f3a553dc7d3080f199cca0efad07d45 100644 (file)
@@ -23,7 +23,7 @@
  *
  * Author: S. Piccardi
  *
  *
  * Author: S. Piccardi
  *
- * $Id: Gapil.h,v 1.6 2003/02/02 20:35:33 piccardi Exp $
+ * $Id: Gapil.h,v 1.7 2003/02/03 14:27:58 piccardi Exp $
  *
  *****************************************************************************/
 #include <sys/sem.h>                           /* IPC semaphore declarations */
  *
  *****************************************************************************/
 #include <sys/sem.h>                           /* IPC semaphore declarations */
@@ -115,8 +115,8 @@ void * ShmFind(key_t ipc_key, int shm_size);
 /* Function ShmRemove: remove a SysV shared memory */
 int ShmRemove(key_t ipc_key, void * shm_ptr);
 /* Function CreateShm: create a POSIX shared memory */
 /* Function ShmRemove: remove a SysV shared memory */
 int ShmRemove(key_t ipc_key, void * shm_ptr);
 /* Function CreateShm: create a POSIX shared memory */
-void * CreateShm(char * shm_name, int shm_size, int perm, char fill);
+void * CreateShm(char * shm_name, off_t shm_size, int perm, char fill);
 /* Function FindShm: find an existing POSIX shared memory */
 /* Function FindShm: find an existing POSIX shared memory */
-void * FindShm(char * shm_name, int shm_size);
+void * FindShm(char * shm_name, off_t shm_size);
 /* Function RemoveShm: remove a POSIX shared memory */
 int RemoveShm(char * shm_name);
 /* Function RemoveShm: remove a POSIX shared memory */
 int RemoveShm(char * shm_name);
index eab28f471368364e0ce34b950f81edd22712858f..35ba20caa2a3c323ea05fbdc7727c9b53ee2a42e 100644 (file)
@@ -26,7 +26,7 @@
  *
  * Author: S. Piccardi
  *
  *
  * Author: S. Piccardi
  *
- * $Id: SharedMem.c,v 1.3 2003/02/02 20:35:33 piccardi Exp $
+ * $Id: SharedMem.c,v 1.4 2003/02/03 14:27:58 piccardi Exp $
  *
  ***************************************************************/
 #include <sys/shm.h>                  /* SysV IPC shared memory declarations */
  *
  ***************************************************************/
 #include <sys/shm.h>                  /* SysV IPC shared memory declarations */
@@ -39,6 +39,8 @@
 #include <sys/mman.h>
 #include <string.h>
 #include <errno.h>
 #include <sys/mman.h>
 #include <string.h>
 #include <errno.h>
+
+#include "macros.h"
 /* *************************************************************************
  *
  *  Functions for SysV shared memory
 /* *************************************************************************
  *
  *  Functions for SysV shared memory
@@ -140,24 +142,27 @@ int ShmRemove(key_t ipc_key, void * shm_ptr)
  *         the fill value
  * Return: the address of the shared memory segment (NULL on error)
  */
  *         the fill value
  * Return: the address of the shared memory segment (NULL on error)
  */
-void * CreateShm(char * shm_name, int shm_size, int perm, char fill) 
+void * CreateShm(char * shm_name, off_t shm_size, mode_t perm, char fill) 
 {
     void * shm_ptr;
     int fd;
     int flag;
     /* first open the object, creating it if not existent */
 {
     void * shm_ptr;
     int fd;
     int flag;
     /* first open the object, creating it if not existent */
-    flag = O_RDWR|O_TRUNC|O_CREAT|O_EXCL;
+    flag = O_CREAT|O_EXCL|O_RDWR;
     fd = shm_open(shm_name, flag, perm);    /* get object file descriptor */
     if (fd < 0) { 
     fd = shm_open(shm_name, flag, perm);    /* get object file descriptor */
     if (fd < 0) { 
+       perror("errore in shm_open");
        return NULL;
     }
     /* set the object size */
     if (ftruncate(fd, shm_size)) {
        return NULL;
     }
     /* set the object size */
     if (ftruncate(fd, shm_size)) {
+       perror("errore in ftruncate");
        return NULL;
     }
     /* map it in the process address space */
     shm_ptr = mmap(NULL, shm_size, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
     if (shm_ptr == MAP_FAILED) {
        return NULL;
     }
     /* map it in the process address space */
     shm_ptr = mmap(NULL, shm_size, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
     if (shm_ptr == MAP_FAILED) {
+       perror("errore in mmap");
        return NULL;
     }
     memset((void *) shm_ptr, fill, shm_size);                /* fill segment */
        return NULL;
     }
     memset((void *) shm_ptr, fill, shm_size);                /* fill segment */
@@ -170,12 +175,13 @@ void * CreateShm(char * shm_name, int shm_size, int perm, char fill)
  *         the shared memory segment size
  * Return: the address of the segment (NULL on error)
  */
  *         the shared memory segment size
  * Return: the address of the segment (NULL on error)
  */
-void * FindShm(char * shm_name, int shm_size) 
+void * FindShm(char * shm_name, off_t shm_size) 
 {
     void * shm_ptr;
     int fd;                           /* ID of the IPC shared memory segment */
     /* find shared memory ID */
     if ((fd = shm_open(shm_name, O_RDWR|O_EXCL, 0)) < 0) {
 {
     void * shm_ptr;
     int fd;                           /* ID of the IPC shared memory segment */
     /* find shared memory ID */
     if ((fd = shm_open(shm_name, O_RDWR|O_EXCL, 0)) < 0) {
+       debug("Cannot open %s\n", shm_name);
        return NULL;
     }
     /* take the pointer to it */
        return NULL;
     }
     /* take the pointer to it */