X-Git-Url: https://git.llucax.com/z.facultad/75.74/practicos.git/blobdiff_plain/d9531d496d0a655ceb86a210f6466a98df0ca7b6..15d27049e86bc6f836ff6163ea78a8a3e40b3c59:/practicas/practica2/P02e1101.cpp diff --git a/practicas/practica2/P02e1101.cpp b/practicas/practica2/P02e1101.cpp index 416abb2..4d8989e 100644 --- a/practicas/practica2/P02e1101.cpp +++ b/practicas/practica2/P02e1101.cpp @@ -90,12 +90,13 @@ int main(int argc, char *argv[]) } int proc = atoi(argv[1]); - int shm_id = shmget(SHM_KEY, sizeof(Molinete), IPC_CREAT | 0666); - if (shm_id == -1) + int shm_id; + do { - cerr << "Error al crea/obtener shared memory.\n"; - return 2; + shm_id = shmget(SHM_KEY, sizeof(Molinete), + (proc ? 0 : IPC_CREAT) | 0666); // crea solo si es el proceso 0 } + while (proc != 0 && shm_id == -1); Molinete* molinete = (Molinete*) shmat(shm_id, NULL, 0); if (molinete == (Molinete*) -1) { @@ -103,14 +104,11 @@ int main(int argc, char *argv[]) return 3; } - cout << "Shared memory id = " << shm_id << "\n"; - // Si somos el primer proceso inicializamos el molinete - if (!proc) molinete_init(*molinete); - else usleep(500); // espero que el 1er proceso inicialice + if (proc == 0) molinete_init(*molinete); // Maxima cantidad de iteraciones (puede venir por parametro) - int max_iter = 100; + int max_iter = 10; if (argc > 2) max_iter = atoi(argv[2]);