X-Git-Url: https://git.llucax.com/z.facultad/75.74/practicos.git/blobdiff_plain/2d0aa03246e924c53fb2e5c3a3af3dcaa3a7eab5..865c286c8c17594d33075e9696d85968eea8dd3b:/practicas/practica2/P02e1101.cpp?ds=sidebyside diff --git a/practicas/practica2/P02e1101.cpp b/practicas/practica2/P02e1101.cpp index 4c5cd03..f593405 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(1000); // espero que el 1er proceso inicialice + if (proc == 0) molinete_init(*molinete); // Maxima cantidad de iteraciones (puede venir por parametro) - int max_iter = 100000; + int max_iter = 100; if (argc > 2) max_iter = atoi(argv[2]);