Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *@brief Ejercicio 9 de la Practica 2
- *@file Ejercicio9.c
- *@author Antonio Talavera Herranz
- *@author Jaime Velazquez Pazos
- *@grupo 2211
- *@version 1.0
- *@date 20-03-2019
- **/
- #include <stdio.h>
- #include <stdlib.h>
- #include <semaphore.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <time.h>
- #include <math.h>
- #define N_PROC 4
- #define SEML "/sem_lecturaak"
- #define SEME "/sem_escriturahak"
- #define SEMC "/sem_contkah"
- #define SECS 1
- int Leer(int * cantidades) {
- FILE *f = NULL;
- int id = 0, i = 0;
- f = fopen("fichero.txt", "r");
- if (f == NULL) return -1;
- while (fscanf(f, "%d", &id) == 1) {
- i++;
- cantidades[id]++;
- if (cantidades[id] >= 20) {
- for (i = 0; i < N_PROC; i++)
- printf("El proceso %d se ha leido %d veces \n", i, cantidades[i]);
- printf("-------------------------------------------\n");
- printf("Se ha leido el proceso %d 20 veces\n", id);
- remove("fichero.txt");
- free(cantidades);
- return 1;
- }
- }
- for (i = 0; i < N_PROC; i++)
- printf("El proceso %d se ha leido %d veces \n", i, cantidades[i]);
- fclose(f);
- remove("fichero.txt");
- printf("-------------------------------------------\n");
- return 0;
- }
- int Lectura(sem_t *sem_lectura, sem_t *sem_escritura, sem_t *cont, int * cantidades) {
- int sval = 0;
- sem_wait(sem_lectura); /*Protegemos esta zona Bajando el semaforo de lectura(inicialmente a 1), para que solo pueda acceder un proceso */
- sem_post(cont);
- sem_getvalue(cont, &sval);
- /*Incrementamos el semaforo que nos hace de contador*/
- if (sval == 1)/*Si el semaforo contador es 1(el primer proceso que llega), entra en la condicion y bloquea la zona de escritura*/
- sem_wait(sem_escritura);
- sem_post(sem_lectura); /*Salimos de la zona protegida, subiendo el semaforo de lectura*/
- if (Leer(cantidades)) return 1;
- sem_wait(sem_lectura); /*Volvemos a proteger otra zona de codigo bajando el semaforo de lectura*/
- sem_wait(cont); /*El proceso ya ha leido y por tanto sale y resta uno al contador de procesos*/
- sem_getvalue(cont, &sval);
- 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*/
- sem_post(sem_escritura);
- sem_post(sem_lectura);
- return 0;
- }
- void Escribir(int i) {
- FILE *f = NULL;
- f = fopen("fichero.txt", "a");
- if (f == NULL) return;
- fprintf(f, "%d\n", i);
- fclose(f);
- }
- void Escritura(sem_t *sem_escritura, int i) {
- int num = 0;
- while (1) {
- 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*/
- Escribir(i);
- srand((unsigned) time(NULL));
- sem_post(sem_escritura);
- usleep(rand() % 100000);
- }
- }
- void manejador_SIGTERM(int sig) {
- int i;
- printf("FIN DE LA CARRERA\n");
- for (i = 0; i < N_PROC; i++)
- wait(NULL);
- exit(EXIT_SUCCESS);
- }
- int main(void) {
- sem_t *sem_lectura, *sem_escritura, *cont = NULL;
- pid_t pid = 1;
- int id, *cantidades;
- int i;
- struct sigaction act;
- cantidades = (int *) malloc(N_PROC * sizeof (int));
- if (cantidades == NULL) return 0;
- for (i = 0; i < N_PROC; i++)
- cantidades[i] = 0;
- act.sa_handler = manejador_SIGTERM;
- if (sigaction(SIGTERM, &act, NULL) < 0) {/*Se prepara la señal SIGTERM que se enviara al hijo*/
- perror("ERROR en el HIjo");
- exit(EXIT_FAILURE);
- }
- act.sa_flags = 0;
- 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*/
- perror("sem_open");
- exit(EXIT_FAILURE);
- }
- 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*/
- perror("sem_open");
- exit(EXIT_FAILURE);
- }
- if ((cont = sem_open(SEMC, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0)) == SEM_FAILED) {/*Se inicializa el semaforo del contador a 0*/
- perror("sem_open");
- exit(EXIT_FAILURE);
- }
- for (i = 0; i < N_PROC; i++) {
- if (pid) {
- pid = fork();
- if (!pid)
- id = i;
- }
- }
- while (1) {
- if (pid == 0) {
- Escritura(sem_escritura, id);
- } else {
- if (Lectura(sem_lectura, sem_escritura, cont, cantidades) == 1) {
- sem_close(sem_lectura); /*Se cierra el semaforo de lectura*/
- sem_unlink(SEML);
- sem_close(sem_escritura); /*Se cierra el semaforo de escritura*/
- sem_unlink(SEME);
- sem_close(cont); /*Se cierra el semaforo contador*/
- sem_unlink(SEMC);
- kill(0, SIGTERM);
- }
- sleep(SECS);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement