Advertisement
jaimolias

Untitled

Mar 27th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. /**
  2. *@brief Ejercicio 9 de la Practica 2
  3. *@file Ejercicio9.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. #include <time.h>
  18. #include <math.h>
  19. #define N_PROC 4
  20. #define SEML "/sem_lecturaak"
  21. #define SEME "/sem_escriturahak"
  22. #define SEMC "/sem_contkah"
  23. #define SECS 1
  24.  
  25. int Leer(int * cantidades) {
  26. FILE *f = NULL;
  27. int id = 0, i = 0;
  28.  
  29. f = fopen("fichero.txt", "r");
  30. if (f == NULL) return -1;
  31. while (fscanf(f, "%d", &id) == 1) {
  32. i++;
  33. cantidades[id]++;
  34. if (cantidades[id] >= 20) {
  35. for (i = 0; i < N_PROC; i++)
  36. printf("El proceso %d se ha leido %d veces \n", i, cantidades[i]);
  37. printf("-------------------------------------------\n");
  38. printf("Se ha leido el proceso %d 20 veces\n", id);
  39. remove("fichero.txt");
  40. free(cantidades);
  41. return 1;
  42.  
  43. }
  44.  
  45. }
  46. for (i = 0; i < N_PROC; i++)
  47. printf("El proceso %d se ha leido %d veces \n", i, cantidades[i]);
  48.  
  49. fclose(f);
  50. remove("fichero.txt");
  51. printf("-------------------------------------------\n");
  52. return 0;
  53.  
  54. }
  55.  
  56. int Lectura(sem_t *sem_lectura, sem_t *sem_escritura, sem_t *cont, int * cantidades) {
  57. int sval = 0;
  58. sem_wait(sem_lectura); /*Protegemos esta zona Bajando el semaforo de lectura(inicialmente a 1), para que solo pueda acceder un proceso */
  59. sem_post(cont);
  60. sem_getvalue(cont, &sval);
  61. /*Incrementamos el semaforo que nos hace de contador*/
  62. if (sval == 1)/*Si el semaforo contador es 1(el primer proceso que llega), entra en la condicion y bloquea la zona de escritura*/
  63. sem_wait(sem_escritura);
  64. sem_post(sem_lectura); /*Salimos de la zona protegida, subiendo el semaforo de lectura*/
  65.  
  66. if (Leer(cantidades)) return 1;
  67.  
  68. sem_wait(sem_lectura); /*Volvemos a proteger otra zona de codigo bajando el semaforo de lectura*/
  69. sem_wait(cont); /*El proceso ya ha leido y por tanto sale y resta uno al contador de procesos*/
  70. sem_getvalue(cont, &sval);
  71. 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*/
  72. sem_post(sem_escritura);
  73. sem_post(sem_lectura);
  74. return 0;
  75. }
  76.  
  77. void Escribir(int i) {
  78. FILE *f = NULL;
  79. f = fopen("fichero.txt", "a");
  80. if (f == NULL) return;
  81.  
  82. fprintf(f, "%d\n", i);
  83. fclose(f);
  84. }
  85.  
  86. void Escritura(sem_t *sem_escritura, int i) {
  87. int num = 0;
  88.  
  89. while (1) {
  90.  
  91. 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*/
  92. Escribir(i);
  93. srand((unsigned) time(NULL));
  94. sem_post(sem_escritura);
  95.  
  96. usleep(rand() % 100000);
  97. }
  98.  
  99. }
  100.  
  101. void manejador_SIGTERM(int sig) {
  102. int i;
  103. printf("FIN DE LA CARRERA\n");
  104. for (i = 0; i < N_PROC; i++)
  105. wait(NULL);
  106. exit(EXIT_SUCCESS);
  107.  
  108. }
  109.  
  110. int main(void) {
  111. sem_t *sem_lectura, *sem_escritura, *cont = NULL;
  112. pid_t pid = 1;
  113. int id, *cantidades;
  114. int i;
  115. struct sigaction act;
  116.  
  117. cantidades = (int *) malloc(N_PROC * sizeof (int));
  118. if (cantidades == NULL) return 0;
  119.  
  120. for (i = 0; i < N_PROC; i++)
  121. cantidades[i] = 0;
  122. act.sa_handler = manejador_SIGTERM;
  123.  
  124. if (sigaction(SIGTERM, &act, NULL) < 0) {/*Se prepara la señal SIGTERM que se enviara al hijo*/
  125. perror("ERROR en el HIjo");
  126. exit(EXIT_FAILURE);
  127.  
  128. }
  129.  
  130. act.sa_flags = 0;
  131.  
  132. 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*/
  133. perror("sem_open");
  134. exit(EXIT_FAILURE);
  135. }
  136.  
  137. 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*/
  138. perror("sem_open");
  139. exit(EXIT_FAILURE);
  140. }
  141.  
  142. if ((cont = sem_open(SEMC, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0)) == SEM_FAILED) {/*Se inicializa el semaforo del contador a 0*/
  143. perror("sem_open");
  144. exit(EXIT_FAILURE);
  145. }
  146.  
  147. for (i = 0; i < N_PROC; i++) {
  148.  
  149. if (pid) {
  150.  
  151. pid = fork();
  152. if (!pid)
  153. id = i;
  154. }
  155. }
  156. while (1) {
  157. if (pid == 0) {
  158.  
  159. Escritura(sem_escritura, id);
  160. } else {
  161.  
  162. if (Lectura(sem_lectura, sem_escritura, cont, cantidades) == 1) {
  163. sem_close(sem_lectura); /*Se cierra el semaforo de lectura*/
  164. sem_unlink(SEML);
  165.  
  166. sem_close(sem_escritura); /*Se cierra el semaforo de escritura*/
  167. sem_unlink(SEME);
  168.  
  169. sem_close(cont); /*Se cierra el semaforo contador*/
  170. sem_unlink(SEMC);
  171. kill(0, SIGTERM);
  172. }
  173.  
  174. sleep(SECS);
  175. }
  176.  
  177. }
  178.  
  179.  
  180. return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement