-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainteste.c
386 lines (342 loc) · 10.2 KB
/
mainteste.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/* Includes */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <semaphore.h>
#include <sys/wait.h>
#include <sys/stat.h> /* For mode constants */
#include <string.h>
#include <time.h>
/* Defines */
#define NUM_PROCESS 4
#define NUM_PAGES 32
#define NUM_ACCESS 120
#define MEMORY_SIZE 16
#define NUM_ROUNDS 50
#define SHM_NAME "/my_shared_memory"
#define SEM_NAME "/my_semaphore"
#define SHM_SIZE sizeof(SharedData)
#define RANDOM_PAGE() (rand() % (NUM_PAGES))
#define RANDOM_ACCESS() (rand() % 2)
/* Estrutura para os dados compartilhados */
typedef struct
{
int flag;
int pageNum;
char accessType;
} SharedData;
void gmv(int algorithm, int set, int printFlag);
/* Access Log Generator Declaration */
int accessLogsGen(char **paths);
/* Page Algorithms Declarations */
int subs_NRU(int memorySeg); // Not Recently Used (NRU)
int subs_2nCh(int memorySeg); // Second Chance
int subs_LRU(int memorySeg); // Aging (LRU)
int subs_WS(int set, int memorySeg); // Working Set (k)
int main(int argc, char **argv)
{
int algorithm;
// ARGV error checking
if (argc < 3)
{
fprintf(stderr, "Usage ERROR: <Print Flag> <algorithm> {if WS}:[parameters]\n");
exit(EXIT_FAILURE);
}
if (strcmp(argv[2], "NRU") == 0)
{
algorithm = 0;
}
else if (strcmp(argv[2], "2nCH") == 0)
{
algorithm = 1;
}
else if (strcmp(argv[2], "LRU") == 0)
{
algorithm = 2;
}
else if (strcmp(argv[2], "WS") == 0)
{
algorithm = 3;
}
else if (strcmp(argv[2], "REFRESH") == 0)
{
algorithm = 4;
}
else
{
fprintf(stderr, "Invalid algorithm. Please choose between NRU, 2nCH, LRU or WS.\n");
exit(EXIT_FAILURE);
}
if (atoi(argv[1]) != 0 && atoi(argv[1]) != 1)
{
fprintf(stderr, "Invalid print flag. Please choose between 0 or 1.\n");
exit(EXIT_FAILURE);
}
// Paths for access logs files
char *processesPaths[NUM_PROCESS] = {"P1_AccessesLog.txt",
"P2_AccessesLog.txt",
"P3_AccessesLog.txt",
"P4_AccessesLog.txt"};
// Print Flag Declaration / Instantiation
int printFlag = atoi(argv[1]);
srand(time(NULL) ^ getpid());
int shm_fd;
SharedData *shared_data;
sem_t *sem;
// Remove o objeto de memória compartilhada caso exista
shm_unlink(SHM_NAME);
// Cria ou abre a memória compartilhada POSIX
shm_fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0666);
if (shm_fd == -1)
{
perror("shm_open falhou");
exit(EXIT_FAILURE);
}
// Define o tamanho da memória compartilhada
if (ftruncate(shm_fd, SHM_SIZE) == -1)
{
perror("ftruncate falhou");
shm_unlink(SHM_NAME);
exit(EXIT_FAILURE);
}
// Mapeia a memória compartilhada
shared_data = mmap(NULL, SHM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (shared_data == MAP_FAILED)
{
perror("mmap falhou");
shm_unlink(SHM_NAME);
exit(EXIT_FAILURE);
}
// Inicializa a estrutura compartilhada
shared_data->flag = 0;
// Remove o semáforo caso exista
sem_unlink(SEM_NAME);
// Cria o semáforo
sem = sem_open(SEM_NAME, O_CREAT, 0644, 1);
if (sem == SEM_FAILED)
{
perror("sem_open falhou");
shm_unlink(SHM_NAME);
munmap(shared_data, SHM_SIZE);
exit(EXIT_FAILURE);
}
// Access Log Generator
if (strcmp(argv[2], "REFRESH") == 0)
{
// Running page replacement algorithms
printf("Gerando novos logs de accesso...\n");
usleep(500000);
// Generating process access logs
if (accessLogsGen(processesPaths) == 1)
{
perror("Error when loading access logs");
exit(1);
}
}
else
{
pid_t pid = fork();
if (pid == 0)
{
// Processo filho
sem_close(sem); // Fecha o semáforo no processo filho (será reaberto em gmv)
gmv(algorithm, atoi(argv[3]), printFlag);
exit(0);
}
else if (pid > 0)
{
// Processo pai
// Abrindo os arquivos
FILE *files[NUM_PROCESS];
for (int i = 0; i < NUM_PROCESS; i++)
{
files[i] = fopen(processesPaths[i], "r");
if (files[i] == NULL)
{
perror("Erro ao abrir os arquivos de log");
for (int j = 0; j < i; j++)
{
fclose(files[j]);
}
exit(EXIT_FAILURE);
}
}
printf("Arquivos abertos com sucesso!\n\n");
usleep(1000000);
printf("Algoritmo Escolhido: %s\n", argv[2]);
usleep(500000);
if (strcmp(argv[2], "WS") == 0)
{
if (argc > 3)
{
printf("Parâmetro do Working Set: %s\n", argv[3]);
}
else
{
fprintf(stderr, "Missing parameter k for Working Set algorithm. Please provide a valid integer.\n");
exit(EXIT_FAILURE);
}
}
usleep(500000);
printf("Numero de rodadas: %d\n\n", NUM_ROUNDS);
usleep(500000);
int totalAccesses = 0;
char accessType;
int pageNum;
int aux;
while (1)
{
aux = fscanf(files[totalAccesses % NUM_PROCESS], "%d %c", &pageNum, &accessType);
if (aux != 2)
{
break;
}
sem_wait(sem);
if (shared_data->flag == 1)
{
usleep(500000);
shared_data->flag = 0;
shared_data->pageNum = pageNum;
shared_data->accessType = accessType;
}
sem_post(sem);
totalAccesses++;
usleep(100000); // Pequena espera para evitar loop rápido demais
}
sem_wait(sem);
shared_data->flag = 2; // Sinaliza para o filho terminar
sem_post(sem);
// Aguarda o filho terminar
wait(NULL);
// Limpa recursos
sem_close(sem);
sem_unlink(SEM_NAME);
munmap(shared_data, SHM_SIZE);
shm_unlink(SHM_NAME);
for (int j = 0; j < NUM_PROCESS; j++)
{
fclose(files[j]);
}
}
else
{
perror("fork falhou");
exit(EXIT_FAILURE);
}
}
return 0;
}
void gmv(int algorithm, int set, int printFlag)
{
srand(time(NULL) ^ getpid());
int shm_fd;
SharedData *shared_data;
sem_t *sem;
// Abre a memória compartilhada existente
shm_fd = shm_open(SHM_NAME, O_RDWR, 0666);
if (shm_fd == -1)
{
perror("shm_open falhou no filho");
exit(EXIT_FAILURE);
}
// Mapeia a memória compartilhada
shared_data = mmap(NULL, SHM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (shared_data == MAP_FAILED)
{
perror("mmap falhou no filho");
exit(EXIT_FAILURE);
}
// Abre o semáforo existente
sem = sem_open(SEM_NAME, 0);
if (sem == SEM_FAILED)
{
perror("sem_open falhou no filho");
munmap(shared_data, SHM_SIZE);
exit(EXIT_FAILURE);
}
printf("Filho iniciado\n");
while (1)
{
sem_wait(sem);
if (shared_data->flag == 0)
{
usleep(500000);
shared_data->flag = 1;
printf("Acesso: %d %c\n", shared_data->pageNum, shared_data->accessType);
}
else if (shared_data->flag == 2)
{
sem_post(sem);
break; // Sinal para terminar
}
sem_post(sem);
usleep(100000); // Pequena espera para evitar loop rápido demais
}
// Limpa recursos
sem_close(sem);
munmap(shared_data, SHM_SIZE);
printf("Filho terminou\n");
}
/************************************************************************************************/
/* Access Log Generator */
int accessLogsGen(char **paths)
{
FILE *file;
srand(time(NULL));
for (int i = 0; i < NUM_PROCESS; i++)
{
file = fopen(paths[i], "a+");
if (file == NULL)
{
return 1;
}
else
{
for (int j = 0; j < NUM_ACCESS; j++)
{
if (RANDOM_ACCESS())
{ // Read Access
fprintf(file, "%d R\n", RANDOM_PAGE());
}
else
{ // Write Access
fprintf(file, "%d W\n", RANDOM_PAGE());
}
}
}
fclose(file);
}
return 0;
}
/************************************************************************************************/
/* Page Algorithms */
/************************************************************************************************/
/* Not Recently Used (NRU) */
int subs_NRU(int memorySeg)
{
return -1;
};
/************************************************************************************************/
/************************************************************************************************/
/* Second Chance */
int subs_2nCh(int memorySeg)
{
return -1;
};
/************************************************************************************************/
/************************************************************************************************/
/* Agin (LRU) */
int subs_LRU(int memorySeg)
{
return -1;
};
/************************************************************************************************/
/************************************************************************************************/
/* Working Set (K) */
int subs_WS(int set, int memorySeg)
{
return -1;
};
/************************************************************************************************/