Guest User

Untitled

a guest
Feb 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5. #include <semaphore.h>
  6. #include <unistd.h>
  7.  
  8.  
  9. #define WAIT_LIST 100
  10. #define OPERATORS 10
  11. #define SIZE 256
  12.  
  13. typedef struct c {
  14. char message [SIZE];
  15. int entry_number;
  16. } slot;
  17.  
  18. slot table [WAIT_LIST];
  19. int writepos = 0;
  20. int readpos = 0;
  21. int ret;
  22. pthread_mutex_t lock_write = PTHREAD_MUTEX_INITIALIZER;
  23. pthread_mutex_t lock_read = PTHREAD_MUTEX_INITIALIZER;
  24.  
  25. //Zona #1
  26. // Complete o código
  27. int empty = WAIT_LIST;
  28. pthread_cond_t c_empty = PTHREAD_COND_INITIALIZER;
  29. pthread_mutex_t lock_empty = PTHREAD_MUTEX_INITIALIZER;
  30.  
  31. int full = 0;
  32. pthread_cond_t c_full = PTHREAD_COND_INITIALIZER;
  33. pthread_mutex_t lock_full = PTHREAD_MUTEX_INITIALIZER;
  34.  
  35. //Fim da zona #1
  36.  
  37. void * producer (void * id_ptr);
  38. void * consumer (void *);
  39. void put_data_at (int pos, slot * regist);
  40. void get_data_at (int pos, slot * regist);
  41.  
  42. int main (){
  43.  
  44. int id[WAIT_LIST];
  45. pthread_t prods [WAIT_LIST];
  46. pthread_t cons;
  47. int i;
  48. srand(pthread_self());
  49. for (i=0; i<WAIT_LIST; i++) {
  50. id[i]=i;
  51. if (pthread_create (&prods[i], NULL, producer, &id[i])!=0)
  52. perror("Error creating producer!");
  53.  
  54. }
  55. for (i=0; i<OPERATORS; i++) {
  56. if (pthread_create (&cons, NULL, consumer, &id[i])!=0)
  57. perror("Error creating consumer!");
  58. }
  59.  
  60. for (i=0; i<WAIT_LIST; i++)
  61. pthread_join (prods[i], NULL);
  62.  
  63. pthread_join (cons, NULL);
  64.  
  65.  
  66. //Zona #2
  67. pthread_mutex_destroy(&lock_empty);
  68. pthread_mutex_destroy(&lock_full);
  69. pthread_mutex_destroy(&lock_write);
  70. pthread_mutex_destroy(&lock_read);
  71. pthread_cond_destroy(&c_empty);
  72. pthread_cond_destroy(&c_full);
  73. //Fim da zona #2
  74. pthread_exit (NULL);
  75.  
  76. }
  77.  
  78. void * producer (void * id_ptr){
  79.  
  80. int id = *((int *)id_ptr);
  81. slot * regist = (slot *)malloc(sizeof(slot));
  82.  
  83. int number = 0;
  84. while (1){
  85. sleep(rand()%10);
  86. number = rand()%1000000000;
  87. regist->entry_number = number;
  88. sprintf(regist->message, "service %d client %d", rand()%10, id);
  89.  
  90. // Zona #3
  91. if((ret = pthread_mutex_lock(&lock_empty)) != 0) {
  92. perror("Error locking empty mutex.\n");
  93. exit(1);
  94. }
  95.  
  96. while(empty <= 0)
  97. pthread_cond_wait(&c_empty, &lock_empty);
  98.  
  99. empty--;
  100. pthread_cond_signal(&c_empty);
  101. if((ret = pthread_mutex_unlock(&lock_empty)) != 0) {
  102. perror("Error unlocking empty mutex.\n");
  103. exit(1);
  104. }
  105.  
  106. // Fim da zona #3
  107.  
  108. pthread_mutex_lock (&lock_write);
  109. put_data_at(writepos, regist);
  110. writepos = (writepos+1)%WAIT_LIST;
  111. pthread_mutex_unlock (&lock_write);
  112.  
  113. // Zona #4
  114.  
  115. if((ret = pthread_mutex_lock(&lock_full)) != 0) {
  116. perror("Error locking full mutex.\n");
  117. printf("%d %d\n", pthread_self(), ret);
  118. printf("%d\n",EINVAL);
  119. exit(1);
  120. }
  121.  
  122. full++;
  123. pthread_cond_signal(&c_full);
  124. if((ret = pthread_mutex_unlock(&lock_full)) !=0) {
  125. perror("Error unlocking full mutex.\n");
  126. exit(1);
  127. }
  128.  
  129. // Fim da zona #4
  130. }
  131. pthread_exit (NULL);
  132. }
  133.  
  134. void * consumer (void * id){
  135. int readpos = 0;
  136. slot regist;
  137. while (1){
  138.  
  139. // Zona #5
  140.  
  141. if((ret = pthread_mutex_lock(&lock_full)) != 0) {
  142. perror("Error locking full mutex.\n");
  143. exit(1);
  144. }
  145.  
  146. while(full <= 0)
  147. pthread_cond_wait(&c_full, &lock_full);
  148.  
  149.  
  150. full--;
  151. pthread_cond_signal(&c_full);
  152. if((ret = pthread_mutex_unlock(&lock_full)) != 0) {
  153. perror("Error unlocking full mutex.\n");
  154. exit(1);
  155. }
  156.  
  157.  
  158. // Fim da zona #5
  159.  
  160. pthread_mutex_lock (&lock_read);
  161. get_data_at(readpos,&regist);
  162. readpos=(readpos+1)%WAIT_LIST;
  163. pthread_mutex_unlock (&lock_read);
  164.  
  165. // Zona #6
  166.  
  167.  
  168. if((ret = pthread_mutex_lock(&lock_empty)) != 0) {
  169. perror("Error locking empty mutex.\n");
  170. exit(1);
  171. }
  172.  
  173. empty++;
  174. pthread_cond_signal(&c_empty);
  175. if((ret = pthread_mutex_unlock(&lock_empty)) != 0) {
  176. perror("Error unlocking empty mutex.\n");
  177. exit(1);
  178. }
  179.  
  180. // Fim da zona #6
  181.  
  182. printf("Consumer %d: %d - %s\n", *((int*)id),regist.entry_number, regist.message);
  183.  
  184. }
  185. pthread_exit (NULL);
  186. }
  187.  
  188. void put_data_at (int pos, slot * regist) {
  189. memcpy(&(table[pos]), regist, sizeof(slot));
  190. }
  191.  
  192. void get_data_at (int pos, slot * regist) {
  193. memcpy(regist, &(table[pos]), sizeof(slot));
  194. }
Add Comment
Please, Sign In to add comment