Está en la página 1de 7

UNIVERSIDAD NACIONAL JORGE BASADRE

GROHMANN

FACULTAD DE INGENIERÍA
ESCUELA PROFESIONAL DE INFORMÁTICA Y SISTEMAS

“CODIFICACIÓN DE PRODUCTOR-CONSUMIDOR EN CODEBLOCKS”

CURSO: ALGORITMOS Y PROGRAMACION PARALELA

DOCENTE: ING. DRA. ANA CORI MORON

ESTUDIANTES: ALDO ANTERO CENTENO MAQUERA (2019-119022)


ALVARO ALEJANDRO RIVERA RAMIREZ (2019-119068)
JOHAN MARCOS CONDORI ORELLANA (2019-119045)

CICLO: TERCERO

TURNO: TARDE

TACNA-PERU

2020
PRODUCTOR/CONSUMIDOR CON USO DE UN BUFFER

CODIGO FUENTE:

#include <iostream>
#include <pthread.h>
#include <semaphore.h>
#define n 2
#define x 10
using namespace std;
sem_t mutex,libres,ocupados;
int elemento,final,frente,cola[x];
void *productor(void *)
{
for(final=0;final<10;final++)
{
elemento=rand()%10;
sem_wait(&libres);
sem_wait(&mutex);
cola[final]=elemento;
cout<<"Productor produce el siguiente dato: "<<elemento<<endl;
sem_post(&mutex);
sem_post(&ocupados);
}
}
void *consumidor (void *)
{
for(frente=0;frente<10;frente++)
{
sem_wait(&ocupados);
sem_wait(&mutex);
elemento=cola[frente];
sem_post(&mutex);
sem_post(&libres);
cout<<"Consumidor utiliza el siguiente dato: "<<elemento<<endl;
}
}
int main(){
pthread_t tcons,tpro;
sem_init(&mutex,0,1);
sem_init(&libres,0,n);
sem_init(&ocupados,0,0);
pthread_create(&tcons,NULL,consumidor,NULL);
pthread_create(&tpro,NULL,productor,NULL);
pthread_join(tcons,NULL);
pthread_join(tpro,NULL);
sem_destroy(&mutex);
sem_destroy(&libres);
sem_destroy(&ocupados);
return 0;
}

EJECUCION DEL CODIGO:


PRODUCTOR/CONSUMIDOR SIN EL USO DE UN BUFFER

CODIGO FUENTE:

#include <iostream>
#include <pthread.h>
#include <semaphore.h>
using namespace std;
sem_t mutex,ocupados;
int elemento;
void *productor(void *)
{
for(int i=0;i<10;i++)
{
sem_wait(&mutex);
elemento=rand()%10;
cout<<"Productor produce el siguiente dato: "<<elemento<<endl;
sem_post(&ocupados);
}
}
void *consumidor (void *)
{
for(int j=0;j<10;j++)
{
sem_wait(&ocupados);
cout<<"Consumidor utiliza el siguiente dato: "<<elemento<<endl;
sem_post(&mutex);
}
}
int main()
{
pthread_t tcons,tpro;
sem_init(&mutex,0,1);//leido
sem_init(&ocupados,0,0);//datos
pthread_create(&tcons,NULL,consumidor,NULL);
pthread_create(&tpro,NULL,productor,NULL);
pthread_join(tcons,NULL);
pthread_join(tpro,NULL);
sem_destroy(&mutex);
sem_destroy(&ocupados);
return 0;
}
EJECUCION DEL CODIGO:
N PRODUCTORES Y N CONSUMIDORES

CODIGO FUENTE:

#include <iostream>
#include<thread>
#include<semaphore.h>
#define n 8
#define N 20
using namespace std;
int cola[N];
int frente=0;
int fin=0;
int elemento=0;
sem_t mutex,libres,ocupados;
void produce(int j)
{
while(true){
sem_wait(&libres);
sem_wait(&mutex);
elemento++;
cola[fin]=elemento;
cout<<"\nProductor "<<j+1<<" insertando elemento :"<<elemento;
fin=(fin+1)%N;
sem_post(&mutex);
sem_post(&ocupados);
this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
void consume (int j)
{
while(true){
sem_wait(&ocupados);
sem_wait(&mutex);
elemento=cola[frente];
frente=(frente+1)%N;
cout<<"\nConsumidor "<<j+1<<"consume elemento :"<<elemento;
sem_post(&mutex);
sem_post(&libres);
this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
int main()
{
thread tpro[n];
thread tcons[n];
sem_init(&mutex,0,1);
sem_init(&libres,0,N);
sem_init(&ocupados,0,0);
for(int i=0;i<n;i++)
{
tpro[i]=thread(&produce,i);
tcons[i]=thread(&consume,i);
}
for(int i=0;i<n;i++){
tpro[i].join();
tcons[i].join();
}
sem_destroy(&mutex);
sem_destroy(&ocupados);
sem_destroy(&libres);
return 0;
}

EJECUCION DEL CODIGO:

También podría gustarte