-
Notifications
You must be signed in to change notification settings - Fork 1
/
vista.c
182 lines (163 loc) · 7.05 KB
/
vista.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// This is a personal academic project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include "vista.h"
// Proceso Vista que lee la shm e imprime su datos por stdout
int main(int arg_c, char ** arg_v){
// FORMATO: /<shared_memory_name>\n
// <shared_memory_size>\n
// /<sem_name>\n
char * shared_memory_name = NULL, * shared_memory_size = NULL, * sem_name = NULL;
size_t shared_memory_name_len = 0, shared_memory_size_len = 0, sem_name_len = 0;
ssize_t len;
char * memory_to_free[3];
int memory_to_free_len=0;
switch (arg_c) {
// Si no se pasa por argumento, se obtiene por entrada estandar
case 1: {
if ((len = getline(&shared_memory_name, &shared_memory_name_len, stdin)) == -1) {
perror("ERROR - Se esperaba recibir el nombre de la shared memory - View");
exit(1);
}
shared_memory_name[len-1]='\0';
memory_to_free[memory_to_free_len++] = shared_memory_name;
if ((len = getline(&shared_memory_size, &shared_memory_size_len, stdin)) == -1){
perror("ERROR - Se esperaba recibir el tamaño de la shared memory - View");
free_strs(memory_to_free, memory_to_free_len);
exit(1);
}
shared_memory_size[len-1]='\0';
memory_to_free[memory_to_free_len++] = shared_memory_size;
if ((len = getline(&sem_name, &sem_name_len, stdin)) == -1) {
perror("ERROR - Se esperaba recibir el nombre del semaforo - View");
free_strs(memory_to_free, memory_to_free_len);
exit(1);
}
sem_name[len-1]='\0';
memory_to_free[memory_to_free_len++] = sem_name;
break;
}
// Se reciben los datos como argumentos
case 4:
shared_memory_name = arg_v[1];
shared_memory_size = arg_v[2];
sem_name = arg_v[3];
break;
// No se pasaron la cantidad de argumentos correcta
default:
fprintf(stderr, "ERROR - Solo se espera recibir tres o ningún argumento - View\n");
exit(1);
}
// Se conecta al semaforo con el cual se va a trabajar en la shared memory
sem_t * shared_memory_sem = sem_open(sem_name, O_RDWR);
if(shared_memory_sem == SEM_FAILED){
perror("Error - Al abrir el semaforo de la shm - View");
free_strs(memory_to_free, memory_to_free_len);
exit(1);
}
// Nos conectamos a la shared memory creado por el proceso master
int shared_memory_fd = shm_open(shared_memory_name, O_RDWR, 0);
if(shared_memory_fd == -1){
perror("ERROR - Conectando a la shared memory - View");
free_strs(memory_to_free, memory_to_free_len);
if(sem_close(shared_memory_sem) == -1){
perror("ERROR - Cerrando el semaforo - View");
}
exit(1);
}
// Obtenemos el tamaño de la shared memory
char * endptr=NULL;
size_t shm_size = strtoul(shared_memory_size, &endptr, 10);
if(shared_memory_size == endptr){
perror("ERROR - Lectura del tamaño de la shm por argumento - View");
free_strs(memory_to_free, memory_to_free_len);
if(sem_close(shared_memory_sem) == -1){
perror("ERROR - Cerrando el semaforo - View");
}
if(close(shared_memory_fd) == -1){
perror("ERROR - Cerrando el fd de la shared memory - View");
}
exit(1);
}
free_strs(memory_to_free, memory_to_free_len);
// Mapeamos la shared memory
void * shared_memory_map = mmap(NULL, shm_size, PROT_READ, MAP_SHARED, shared_memory_fd, 0);
if(shared_memory_map == MAP_FAILED){
perror("ERROR - Mapeando la shared memory - View");
if(sem_close(shared_memory_sem) == -1){
perror("ERROR - Cerrando el semaforo - View");
}
if(close(shared_memory_fd) == -1){
perror("ERROR - Cerrando el fd de la shared memory - View");
}
exit(1);
}
// Definimos la estructura de la shared memory con la informacion necesaria
shmADT shm;
if((shm = new_shm((char*)shared_memory_map,shared_memory_sem) ) == NULL){
perror("ERROR - Al crear el ADT - View");
exit(1);
}
// Leemos de la shared memory e imprimimos por STDOUT
int ret_value = read_shared_memory(shm);
// Desmapeamos la shared memory y cerramos su file descriptor
free_shm(shm);
if(munmap(shared_memory_map, shm_size) == -1){
perror("ERROR - Desmapeando la shared memory - View");
if(sem_close(shared_memory_sem) == -1){
perror("ERROR - Cerrando el semaforo - View");
}
if(close(shared_memory_fd) == -1){
perror("ERROR - Cerrando el fd de la shared memory - View");
}
ret_value = -1;
}
if(close(shared_memory_fd) == -1){
perror("ERROR - Cerrando el fd de la shared memory - View");
if(sem_close(shared_memory_sem) == -1){
perror("ERROR - Cerrando el semaforo - View");
}
ret_value = -1;
}
if( sem_close(shared_memory_sem) == -1) {
perror("ERROR - Cerrando el semaforo - View");
ret_value = -1;
}
exit(ret_value * -1);
}
// -------------------------------------------------------------------------------------------
// read_shared_memory: Dado un puntero a memoria, lee los datos que se encuentren en la shared memory
// y los imprime por STDOUT
// -------------------------------------------------------------------------------------------
// Argumentos:
// shm: ADT de la shared memory
// -------------------------------------------------------------------------------------------
// Retorno: 0 si no hubo error, -1 si hubo algun error
// -------------------------------------------------------------------------------------------
int read_shared_memory(shmADT shm){
// Formato de la shared memory
// <arch_1>,<md5_arch_1>,<slave_pid>\n<arch_2>,<md5_arch_2>,<slave_pid>\n...<arch_n>,<md5_arch_n>,<slave_pid>\n\EOT
char shm_output[ARCH_INFO_SIZE];
int status;
while((status = shm_read(shm_output,ARCH_INFO_SIZE, shm)) == 0){
printf("%s", shm_output);
}
if(status == -1){
perror("ERROR - Leyendo de la shm - View");
return -1;
}
return 0;
}
// -------------------------------------------------------------------------------------------
// free_strs: Libera la memoria dinamica utilizada para la lectura por STDIN
// -------------------------------------------------------------------------------------------
// Argumentos:
// memory_to_free: Arreglo con punteros a la memoria dinamica
// len: Cantidad de punteros a liberar
// -------------------------------------------------------------------------------------------
// Retorno: void
// -------------------------------------------------------------------------------------------
void free_strs(char ** memory_to_free, int len){
for(int i=0; i<len; i++){
free(memory_to_free[i]);
}
}