Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <semaphore.h>
- #include <unistd.h>
- #define WAIT_LIST 100
- #define OPERATORS 10
- #define SIZE 256
- typedef struct c {
- char message [SIZE];
- int entry_number;
- } slot;
- slot table [WAIT_LIST];
- int writepos = 0;
- int readpos = 0;
- int ret;
- pthread_mutex_t lock_write = PTHREAD_MUTEX_INITIALIZER;
- pthread_mutex_t lock_read = PTHREAD_MUTEX_INITIALIZER;
- //Zona #1
- // Complete o código
- int empty = WAIT_LIST;
- pthread_cond_t c_empty = PTHREAD_COND_INITIALIZER;
- pthread_mutex_t lock_empty = PTHREAD_MUTEX_INITIALIZER;
- int full = 0;
- pthread_cond_t c_full = PTHREAD_COND_INITIALIZER;
- pthread_mutex_t lock_full = PTHREAD_MUTEX_INITIALIZER;
- //Fim da zona #1
- void * producer (void * id_ptr);
- void * consumer (void *);
- void put_data_at (int pos, slot * regist);
- void get_data_at (int pos, slot * regist);
- int main (){
- int id[WAIT_LIST];
- pthread_t prods [WAIT_LIST];
- pthread_t cons;
- int i;
- srand(pthread_self());
- for (i=0; i<WAIT_LIST; i++) {
- id[i]=i;
- if (pthread_create (&prods[i], NULL, producer, &id[i])!=0)
- perror("Error creating producer!");
- }
- for (i=0; i<OPERATORS; i++) {
- if (pthread_create (&cons, NULL, consumer, &id[i])!=0)
- perror("Error creating consumer!");
- }
- for (i=0; i<WAIT_LIST; i++)
- pthread_join (prods[i], NULL);
- pthread_join (cons, NULL);
- //Zona #2
- pthread_mutex_destroy(&lock_empty);
- pthread_mutex_destroy(&lock_full);
- pthread_mutex_destroy(&lock_write);
- pthread_mutex_destroy(&lock_read);
- pthread_cond_destroy(&c_empty);
- pthread_cond_destroy(&c_full);
- //Fim da zona #2
- pthread_exit (NULL);
- }
- void * producer (void * id_ptr){
- int id = *((int *)id_ptr);
- slot * regist = (slot *)malloc(sizeof(slot));
- int number = 0;
- while (1){
- sleep(rand()%10);
- number = rand()%1000000000;
- regist->entry_number = number;
- sprintf(regist->message, "service %d client %d", rand()%10, id);
- // Zona #3
- if((ret = pthread_mutex_lock(&lock_empty)) != 0) {
- perror("Error locking empty mutex.\n");
- exit(1);
- }
- while(empty <= 0)
- pthread_cond_wait(&c_empty, &lock_empty);
- empty--;
- pthread_cond_signal(&c_empty);
- if((ret = pthread_mutex_unlock(&lock_empty)) != 0) {
- perror("Error unlocking empty mutex.\n");
- exit(1);
- }
- // Fim da zona #3
- pthread_mutex_lock (&lock_write);
- put_data_at(writepos, regist);
- writepos = (writepos+1)%WAIT_LIST;
- pthread_mutex_unlock (&lock_write);
- // Zona #4
- if((ret = pthread_mutex_lock(&lock_full)) != 0) {
- perror("Error locking full mutex.\n");
- printf("%d %d\n", pthread_self(), ret);
- printf("%d\n",EINVAL);
- exit(1);
- }
- full++;
- pthread_cond_signal(&c_full);
- if((ret = pthread_mutex_unlock(&lock_full)) !=0) {
- perror("Error unlocking full mutex.\n");
- exit(1);
- }
- // Fim da zona #4
- }
- pthread_exit (NULL);
- }
- void * consumer (void * id){
- int readpos = 0;
- slot regist;
- while (1){
- // Zona #5
- if((ret = pthread_mutex_lock(&lock_full)) != 0) {
- perror("Error locking full mutex.\n");
- exit(1);
- }
- while(full <= 0)
- pthread_cond_wait(&c_full, &lock_full);
- full--;
- pthread_cond_signal(&c_full);
- if((ret = pthread_mutex_unlock(&lock_full)) != 0) {
- perror("Error unlocking full mutex.\n");
- exit(1);
- }
- // Fim da zona #5
- pthread_mutex_lock (&lock_read);
- get_data_at(readpos,®ist);
- readpos=(readpos+1)%WAIT_LIST;
- pthread_mutex_unlock (&lock_read);
- // Zona #6
- if((ret = pthread_mutex_lock(&lock_empty)) != 0) {
- perror("Error locking empty mutex.\n");
- exit(1);
- }
- empty++;
- pthread_cond_signal(&c_empty);
- if((ret = pthread_mutex_unlock(&lock_empty)) != 0) {
- perror("Error unlocking empty mutex.\n");
- exit(1);
- }
- // Fim da zona #6
- printf("Consumer %d: %d - %s\n", *((int*)id),regist.entry_number, regist.message);
- }
- pthread_exit (NULL);
- }
- void put_data_at (int pos, slot * regist) {
- memcpy(&(table[pos]), regist, sizeof(slot));
- }
- void get_data_at (int pos, slot * regist) {
- memcpy(regist, &(table[pos]), sizeof(slot));
- }
Add Comment
Please, Sign In to add comment