Advertisement
jaimolias

Untitled

Mar 26th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. /**
  2. *@brief Ejercicio 8 de la Practica 2
  3. *@file Ejercicio8.c
  4. *@author Antonio Talavera Herranz
  5. *@author Jaime Velazquez Pazos
  6. *@grupo 2211
  7. *@version 1.0
  8. *@date 20-03-2019
  9. **/
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <semaphore.h>
  13. #include <fcntl.h>
  14. #include <sys/stat.h>
  15. #include <sys/wait.h>
  16. #include <unistd.h>
  17. #define N_HIJOS 4
  18. #define SEML "/sem_lectura"
  19. #define SEME "/sem_escritura"
  20. #define SEMC "/sem_cont"
  21. #define SECS 1
  22.  
  23.  
  24. void Lectura(pid_t pid, sem_t *sem_lectura, sem_t *sem_escritura, sem_t *cont) {
  25. int sval;
  26. sem_wait(sem_lectura);/*Protegemos esta zona Bajando el semaforo de lectura(inicialmente a 1), para que solo pueda acceder un proceso */
  27. sem_post(cont);
  28. sem_getvalue(cont, &sval);
  29. /*Incrementamos el semaforo que nos hace de contador*/
  30. if (sval == 1)/*Si el semaforo contador es 1(el primer proceso que llega), entra en la condicion y bloquea la zona de escritura*/
  31. sem_wait(sem_escritura);
  32. sem_post(sem_lectura);/*Salimos de la zona protegida, subiendo el semaforo de lectura*/
  33.  
  34. Leer(pid);
  35.  
  36. sem_wait(sem_lectura);/*Volvemos a proteger otra zona de codigo bajando el semaforo de lectura*/
  37. sem_wait(cont);/*El proceso ya ha leido y por tanto sale y resta uno al contador de procesos*/
  38. sem_getvalue(cont, &sval);
  39. if (sval == 0)/*Si ya no quedan procesos leyendo (el valor del contador es de 0),se habilita la zona de escritura subiendo su semaforo*/
  40. sem_post(sem_escritura);
  41. sem_post(sem_lectura);
  42. }
  43.  
  44. void Leer(pid_t pid) {
  45. printf("R-INI %d \n", pid);/*Se simula el proceso de lectura*/
  46. sleep(1);
  47. printf("R-FIN %d \n ", pid);
  48. }
  49.  
  50. void Escritura(pid_t pid, sem_t *sem_escritura) {
  51.  
  52. sem_wait(sem_escritura);/*Se protege esta zona de codigo para que solo un proceso pueda leer a la vez, bajando el semaforo de escritura*/
  53. Escribir(pid);
  54.  
  55. sem_post(sem_escritura);
  56.  
  57. }
  58.  
  59. void Escribir(pid_t pid) {
  60. printf("W-INI %d \n", pid);/*Se simula el proceso de escritura*/
  61. sleep(1);
  62. printf("W-FIN %d \n", pid);
  63. }
  64.  
  65. void manejador_SIGINT(int sig) {
  66. int i;
  67.  
  68. kill(0, SIGTERM);
  69. for (i = 0; i < N_HIJOS; i++)
  70. wait(NULL);
  71. exit(EXIT_SUCCESS);
  72.  
  73.  
  74. }
  75.  
  76. void manejador_SIGTERM(int sig) {
  77. printf("Proceso %d terminando...\n", getpid());
  78. exit(EXIT_SUCCESS);
  79.  
  80. }
  81.  
  82. int main(void) {
  83. sem_t *sem_lectura, *sem_escritura, *cont = NULL;
  84. pid_t pid;
  85. int i;
  86. struct sigaction act;
  87. act.sa_handler = manejador_SIGINT;
  88. if (sigaction(SIGINT, &act, NULL) < 0) {
  89. perror("ERROR en el PADRE");
  90. exit(EXIT_FAILURE);
  91. }
  92. act.sa_flags = 0;
  93.  
  94. if ((sem_lectura = sem_open(SEML, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 1)) == SEM_FAILED) {/*Se inicializa el semanforo de lectura a 1*/
  95. perror("sem_open");
  96. exit(EXIT_FAILURE);
  97. }
  98.  
  99. if ((sem_escritura = sem_open(SEME, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 1)) == SEM_FAILED) {/*Se inicializa el semaforo de escritura a 1*/
  100. perror("sem_open");
  101. exit(EXIT_FAILURE);
  102. }
  103.  
  104. if ((cont = sem_open(SEMC, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0)) == SEM_FAILED) {/*Se inicializa el semaforo del contador a 0*/
  105. perror("sem_open");
  106. exit(EXIT_FAILURE);
  107. }
  108.  
  109. for (i = 0; i < N_HIJOS; i++) {/*bucle que crea N_HIJOS hijos*/
  110. pid = fork();
  111.  
  112. if (pid == 0) {
  113. act.sa_handler = manejador_SIGTERM;
  114.  
  115. if (sigaction(SIGTERM, &act, NULL) < 0) {/*Se prepara la señal SIGTERM que se enviara al hijo*/
  116. perror("ERROR en el HIjo");
  117. exit(EXIT_FAILURE);
  118.  
  119. }
  120. while (1) {/*Mientras la señal SIGTERM no llegue, se repite este bucle infinitas veces*/
  121. Lectura(getpid(), sem_lectura, sem_escritura, cont);
  122. sleep(SECS);
  123. }
  124. }
  125. }
  126. if (pid > 0) {
  127. while (1) {/*El proceso padre llamara a la funcion de escritura infinitas veces*/
  128. Escritura(getppid(), sem_escritura);
  129. sleep(SECS);
  130.  
  131. }
  132. sem_close(sem_lectura);/*Se cierra el semaforo de lectura*/
  133. sem_unlink(SEML);
  134.  
  135. sem_close(sem_escritura);/*Se cierra el semaforo de escritura*/
  136. sem_unlink(SEME);
  137.  
  138. sem_close(cont);/*Se cierra el semaforo contador*/
  139. sem_unlink(SEMC);
  140. }
  141.  
  142.  
  143. return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement