Created
December 12, 2012 12:46
-
-
Save ACruz91/4267467 to your computer and use it in GitHub Desktop.
Problema del escritor y lector regulado con semáforos con la prioridad del escritor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <pthread.h> | |
#include <semaphore.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
/*Variables Globales*/ | |
int rc=0; | |
int wc=0; | |
typedef struct datos{ | |
char nombre[20]; | |
char apellido[20]; | |
int edad; | |
}Datos; | |
unsigned int num_escritores, num_lectores; | |
FILE * baseDatos2; | |
/*Semaforos*/ | |
sem_t mutex1,mutex2,rdb,wdb; | |
/*Funciones*/ | |
void *lector(void *p); | |
void *escritor(void *p); | |
void escribirenbasedatos(); | |
void leerenbasedatos(); | |
int main(int argc, char *argv[]){ | |
int estado,i; | |
extern FILE * baseDatos2; | |
baseDatos2=fopen("baseDatos2.txt","w"); | |
fclose(baseDatos2); | |
if ( argc < 3 ) { | |
printf("Numero de parametros incorrecto. Indicar el numero de datos a producir.\n") ; | |
exit(1); | |
} | |
num_escritores = atoi(argv[1]); | |
num_lectores = atoi(argv[2]); | |
int eid[num_escritores],lid[num_lectores]; | |
/*Declaración de las dos hebras*/ | |
pthread_t escritora[num_escritores],lectora[num_lectores]; | |
/*Inicialización de Semáforos*/ | |
sem_init(&mutex1,0,1); | |
sem_init(&mutex2,0,1); | |
sem_init(&rdb,0,1); | |
sem_init(&wdb,0,1); | |
for(i=0;i<num_lectores;i++){ | |
lid[i]=i; | |
estado=pthread_create(&lectora[i],NULL,lector,(void *) &lid[i]); | |
if(estado!=0) | |
printf("Error al cargar la hebra lectora\n"); | |
} | |
for(i=0;i<num_escritores;i++){ | |
eid[i]=i; | |
estado=pthread_create(&escritora[i],NULL,escritor,(void *) &eid[i]); | |
if(estado!=0) | |
printf("Error al cargar la hebra escritora\n"); | |
} | |
/*La hebra principal esperara a que terminen las otras 2 hebras.*/ | |
for(i=0;i<num_escritores;i++) | |
pthread_join(escritora[i],NULL); | |
for(i=0;i<num_lectores;i++) | |
pthread_join(lectora[i],NULL); | |
/*Destrucción de los semaforos*/ | |
sem_destroy(&mutex1); | |
sem_destroy(&mutex2); | |
sem_destroy(&rdb); | |
sem_destroy(&wdb); | |
return 0; | |
} | |
void escribirenbasedatos(){ | |
extern FILE *baseDatos2; | |
char c; | |
baseDatos2=fopen("baseDatos2.txt","a+"); | |
while ((c=getc(stdin))!=EOF){ | |
fputc(c,baseDatos2); | |
} | |
fclose(baseDatos2); | |
} | |
void leerenbasedatos(){ | |
extern FILE *baseDatos2; | |
Datos p; | |
baseDatos2=fopen("baseDatos2.txt","r"); | |
while (fscanf(baseDatos2,"%s\n%s\n%d\n",p.nombre,p.apellido,&p.edad)==3){ | |
printf("\nNombre:"); | |
puts(p.nombre); | |
printf("Apellidos:"); | |
puts(p.apellido); | |
printf("Edad:\n"); | |
printf("%d\n",p.edad); | |
} | |
fclose(baseDatos2); | |
} | |
void *escritor(void *p){ | |
int *id_es; | |
id_es=(int *)p; | |
sem_wait(&mutex1); | |
wc++; | |
if(wc == 1) | |
sem_wait(&rdb); | |
sem_post(&mutex1); | |
sem_wait(&wdb); | |
printf("\nEscritor %d Escribiendo...\n",*id_es); | |
escribirenbasedatos(*id_es); | |
sem_post(&wdb); | |
sem_wait(&mutex1); | |
wc--; | |
if(wc == 0) | |
sem_post(&rdb); | |
sem_post(&mutex1); | |
pthread_exit(NULL); | |
} | |
void *lector(void *p){ | |
int *id_le; | |
id_le=(int *)p; | |
sem_wait(&rdb); | |
sem_wait(&mutex2); | |
rc++; | |
if(rc == 1) | |
sem_wait(&wdb); | |
sem_post(&rdb); | |
printf("\nLector %d Leyendo...\n",*id_le); | |
leerenbasedatos(*id_le); | |
sem_post(&mutex2); | |
sem_wait(&mutex2); | |
rc--; | |
if(rc == 0) | |
sem_post(&wdb); | |
sem_post(&mutex2); | |
pthread_exit(NULL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment