-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulacion.c
95 lines (80 loc) · 2.63 KB
/
simulacion.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
/*
Autores:
- Marc Link Cladera
- Carlos Gálvez Mena
- Jesús Castillo Benito
*/
#include "simulacion.h"
static int acabados = 0;
char nombre_directorio[88];
int main(int argc, char **args) {
if (argc != 2)
{
fprintf(
stderr,
RED
"ERROR: invalid syntax. Usage: ./simulacion <disco>\n"
RESET
);
return FALLO;
}
if (bmount(args[1]) < 0) return FALLO;
signal(SIGCHLD, reaper);
struct timeval tv;
gettimeofday(&tv, NULL);
struct tm *t = localtime(&tv.tv_sec);
sprintf(nombre_directorio, "/simul_%04d%02d%02d%02d%02d%02d/",
t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
t->tm_hour, t->tm_min, t->tm_sec);
if (mi_creat(nombre_directorio, 7) < 0) return FALLO;
for (int i = 0; i < NUMPROCESOS; i++) {
pid_t pid = fork();
if (pid == 0)
{
if(bmount(args[1]) < 0) return FALLO;
char nombre_directorio_hijo[128];
memset(nombre_directorio_hijo, 0, sizeof(nombre_directorio_hijo));
sprintf(nombre_directorio_hijo, "%sproceso_%d/", nombre_directorio, getpid());
if(mi_creat(nombre_directorio_hijo, 7) < 0) return FALLO;
char ruta_fichero[256];
memset(ruta_fichero, 0, sizeof(ruta_fichero));
sprintf(ruta_fichero, "%sprueba.dat", nombre_directorio_hijo);
if(mi_creat(ruta_fichero, 6) < 0) return FALLO;
srand(time(NULL) + getpid());
for (int nscritura = 1; nscritura <= NUMESCRITURAS; nscritura++)
{
struct REGISTRO registro;
gettimeofday(®istro.fecha, NULL);
registro.pid = getpid();
registro.nEscritura = nscritura;
registro.nRegistro = rand() % REGMAX;
mi_write(ruta_fichero, ®istro, registro.nRegistro * sizeof(struct REGISTRO), sizeof(struct REGISTRO));
usleep(50000);
}
if(bumount() < 0) return FALLO;
exit(0);
}
usleep(150000);
}
while (acabados < NUMPROCESOS) pause();
if (bumount() < 0) return FALLO;
return EXITO;
}
void reaper()
{
pid_t ended;
signal(SIGCHLD, reaper);
while ((ended = waitpid(-1, NULL, WNOHANG) > 0))
{
acabados++;
#if DEBUGN12
fprintf(
stderr,
GRAY
"[Proceso %d: Completadas 50 escrituras en %sproceso_%d/prueba.dat]\n"
RESET,
acabados, nombre_directorio, ended
);
#endif
}
}