forked from danielss24/SOPER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared_memory.c
68 lines (52 loc) · 1.9 KB
/
shared_memory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "shared_memory.h"
char * crea_o_asocia_shm(int key,int * semaforo, int tamannio){
unsigned short array_comun[2] = {1, 1}; /*1 semaforo,inicializados a 1*/
struct info * informacion;
int id = 0;
semaforo = (int*) malloc(sizeof (int));
if (semaforo == NULL || tamannio < 0) {
printf("Linea %d - Error al reservar memoria\n", __LINE__);
}
if (-1 == Crear_Semaforo(IPC_PRIVATE, 1, semaforo)) {/*1 semaforo*/
printf("Linea %d - Error al crear el semaforo\n", __LINE__);
return NULL;
}
if (Inicializar_Semaforo(*semaforo, array_comun) == -1) {
printf("\n Linea %d - Error al inicializar el semaforo\n", __LINE__);
return NULL;
}/* else {*/
/* printf("Semaforo inicializado correctamente\n");
}*/
if((id=shmget(key,tamannio + (sizeof(int) + sizeof(int *)),IPC_CREAT|IPC_EXCL|0660))==-1){
/*printf("El segmento de memoria compartida ya existe\n");
printf(" Abriendo como cliente\n");*/
if((id=shmget(key,tamannio + (sizeof(int) + sizeof(int *) ),0))==-1){
printf("Error al abrir el segmento\n");
}
sleep(2);
informacion = shmat (id, (char *)0, 0);
if (informacion == NULL) {
return NULL;
}else{
return (char *) informacion;
}
/* } else {*/
/* printf("Nuevo segmento creado\n");*/
}
informacion = shmat (id, (char *)0, 0);
if (informacion == NULL) {
fprintf (stderr, "Error reserve shared memory \n");
return NULL;
}
informacion->shmid = id;
informacion->semaforo = semaforo ;
informacion->contenido = (char *) (informacion + sizeof(int) +sizeof(int*)) ;/*situamos el puntero*/
return (char*)informacion;
}
void destruye_shm(char * informacion){
int id;
Borrar_Semaforo(*( (struct info *)informacion)->semaforo) ;
id = ( (struct info *)informacion)->shmid;
shmdt ((char *)informacion);
shmctl (id, IPC_RMID, (struct shmid_ds *)NULL);
}