Inizio della shared memory
[gapil.git] / sources / SharedMem.c
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 */