Aggiunte delle note di Copyright su tutti i .tex
[gapil.git] / sources / SharedMem.c
1 /* SharedMem.c
2  * 
3  * Copyright (C) 2002 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /***************************************************************
20  *
21  * File SharedMem.c 
22  * Routine for Shared Memory use
23  *
24  * Author: S. Piccardi
25  *
26  * $Id: SharedMem.c,v 1.2 2002/12/03 22:30:11 piccardi Exp $
27  *
28  ***************************************************************/
29 #include <sys/shm.h>                  /* SysV IPC shared memory declarations */
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <stdio.h>                                 /* standard I/O functions */
33 #include <fcntl.h>
34 #include <signal.h>                          /* signal handling declarations */
35 /*
36  * Function ShmCreate:
37  * Create and attach a SysV shared memory segment to the current process.
38  *
39  * First call get a shared memory segment with KEY key access and size SIZE,
40  * by creating it with R/W privilege for the user (this is the meaning of
41  * the ored flags). The function return an identifier shmid used for any 
42  * further reference to the shared memory segment. 
43  * Second call attach the shared memory segment to this process and return a
44  * pointer to it (of void * type). 
45  * Then initialize shared memory to the given value
46  *
47  * Input:  an IPC key value
48  *         the shared memory segment size
49  * Return: the address of the segment
50  */
51 void * ShmCreate(key_t ipc_key, int shm_size, char fill) 
52 {
53     void * shptr;
54     int shmid;                        /* ID of the IPC shared memory segment */
55     shmid = shmget(ipc_key,shm_size,IPC_CREAT|0666);           /* get shm ID */
56     if (shmid < 0) { 
57         return (void *) shmid;
58     }
59     shptr = shmat(shmid,0,0);                      /* take the pointer to it */
60     if ( shptr == 0 ){    
61         perror("cannot attach shared memory");
62         exit(1);
63     }
64     memset((void *)shptr, fill, shm_size); /* second counter starts from "0" */
65     return shptr;
66 }
67 /*
68  * Function ShmFind:
69  * Find a shared memory segment 
70  * Input:  an IPC key value
71  *         the shared memory segment size
72  * Return: the address of the segment
73  */
74 void * ShmFind(key_t ipc_key, int shm_size) 
75 {
76     void * shptr;
77     int shmid;               /* ID of the IPC shared memory segment */
78     if ( (shmid=shmget(ipc_key,shm_size,0))<0 ){  /* find shared memory ID */
79         perror("cannot find shared memory");
80         exit(1);
81     }
82     if ( (shptr=shmat(shmid,0,0)) < 0 ){    /* take the pointer to it */
83         perror("cannot attach shared memory");
84         exit(1);
85     }
86     return shptr;
87 }