Saltar al contenido

Problema del consumidor del productor en c usando el ejemplo de código de semáforos

Ya no tienes que buscar más por otras páginas porque estás al sitio exacto, contamos con la solución que quieres y sin liarte.

Ejemplo: c productor consumidor pthread semáforo

/* buffer.h */typedefint buffer_item;#defineBUFFER_SIZE5/* main.c */#include#include#include#include#include"buffer.h"#defineRAND_DIVISOR100000000#defineTRUE1/* The mutex lock */pthread_mutex_t mutex;/* the semaphores */sem_t full, empty;/* the buffer */
buffer_item buffer[BUFFER_SIZE];/* buffer counter */int counter;pthread_t tid;//Thread IDpthread_attr_t attr;//Set of thread attributesvoid*producer(void*param);/* the producer thread */void*consumer(void*param);/* the consumer thread */voidinitializeData()/* Create the mutex lock */pthread_mutex_init(&mutex,NULL);/* Create the full semaphore and initialize to 0 */sem_init(&full,0,0);/* Create the empty semaphore and initialize to BUFFER_SIZE */sem_init(&empty,0, BUFFER_SIZE);/* Get the default attributes */pthread_attr_init(&attr);/* init buffer */
   counter =0;/* Producer Thread */void*producer(void*param)
   buffer_item item;while(TRUE)/* sleep for a random period of time */int rNum =rand()/ RAND_DIVISOR;sleep(rNum);/* generate a random number */
      item =rand();/* acquire the empty lock */sem_wait(&empty);/* acquire the mutex lock */pthread_mutex_lock(&mutex);if(insert_item(item))fprintf(stderr," Producer report error conditionn");elseprintf("producer produced %dn", item);/* release the mutex lock */pthread_mutex_unlock(&mutex);/* signal full */sem_post(&full);/* Consumer Thread */void*consumer(void*param)
   buffer_item item;while(TRUE)/* sleep for a random period of time */int rNum =rand()/ RAND_DIVISOR;sleep(rNum);/* aquire the full lock */sem_wait(&full);/* aquire the mutex lock */pthread_mutex_lock(&mutex);if(remove_item(&item))fprintf(stderr,"Consumer report error conditionn");elseprintf("consumer consumed %dn", item);/* release the mutex lock */pthread_mutex_unlock(&mutex);/* signal empty */sem_post(&empty);/* Add an item to the buffer */intinsert_item(buffer_item item)/* When the buffer is not full add the item
      and increment the counter*/if(counter < BUFFER_SIZE)
      buffer[counter]= item;
      counter++;return0;else/* Error the buffer is full */return-1;/* Remove an item from the buffer */intremove_item(buffer_item *item)/* When the buffer is not empty remove the item
      and decrement the counter */if(counter >0)*item = buffer[(counter-1)];
      counter--;return0;else/* Error buffer empty */return-1;intmain(int argc,char*argv[])/* Loop counter */int i;/* Verify the correct number of arguments were passed in */if(argc !=4)fprintf(stderr,"USAGE:./main.out   n");int mainSleepTime =atoi(argv[1]);/* Time in seconds for main to sleep */int numProd =atoi(argv[2]);/* Number of producer threads */int numCons =atoi(argv[3]);/* Number of consumer threads *//* Initialize the app */initializeData();/* Create the producer threads */for(i =0; i < numProd; i++)/* Create the thread */pthread_create(&tid,&attr,producer,NULL);/* Create the consumer threads */for(i =0; i < numCons; i++)/* Create the thread */pthread_create(&tid,&attr,consumer,NULL);/* Sleep for the specified amount of time in milliseconds */sleep(mainSleepTime);/* Exit the program */printf("Exit the programn");exit(0);

Comentarios y puntuaciones

Si conservas algún reparo o forma de ascender nuestro reseña eres capaz de dejar una interpretación y con placer lo analizaremos.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)


Tags : /

Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *