Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5.  
  6. int nbloco; /* numero de elementos do bloco a processar */
  7. int nt; /* numero de tarefas */
  8. int n; /* numero inteiro a processar */
  9. int nt_somadoras; /* caso queira mais que uma somadora */
  10. int soma_global; /* a soma total dos elementso calculados */
  11. int items_buffer;
  12. typedef struct {
  13.  
  14. /* This structure is passed to the threads and contains all necessary
  15. information for computations */
  16.  
  17. pthread_mutex_t * mutex; /* Mutex */
  18. pthread_cond_t * cond; /* Main Condition variable */
  19. pthread_cond_t * produzir; // signaled when items are removed
  20. pthread_cond_t * consumir; // signaled when items are added
  21. int *buffer; /* buffer partilhado */
  22. int *matriz; /* Matriz com os valores a calcular */
  23. int numero_thread; /* número das threads */
  24. int inicio; /* Elemento que deve começar o cálculo */
  25.  
  26.  
  27. }estrutura_global;
  28.  
  29. void * calculadora(void *); /* Declaração da finção calculadora */
  30. void * somadora(void *); /* Declaração da tarefa somadora */
  31.  
  32.  
  33. int main(int argc, char ** arcv){
  34.  
  35. int a, i, z, x, y; /* defenição da variavel i */
  36.  
  37.  
  38. /* ler dados da linha de comendos*/
  39.  
  40. nt=atoi(arcv[1]);
  41. n=atoi(arcv[1]);
  42. nbloco=atoi(arcv[1]);
  43. nt_somadoras=1;
  44.  
  45. /* Verificação de dados */
  46.  
  47.  
  48. /* Alocação de memorias */
  49.  
  50.  
  51. estrutura_global * estrutura = malloc(sizeof(estrutura_global));
  52. estrutura->mutex = malloc(sizeof(pthread_mutex_t));
  53. estrutura->cond = malloc(sizeof(pthread_cond_t));
  54. estrutura->produzir = malloc(sizeof(pthread_cond_t));
  55. estrutura->consumir = malloc(sizeof(pthread_cond_t));
  56. pthread_t * calculadoras = malloc(sizeof(pthread_t)*nt);
  57. pthread_t * somadoras = malloc(sizeof(pthread_t)*nt_somadoras);
  58. pthread_attr_t * attr = malloc(sizeof(pthread_attr_t));
  59. estrutura->matriz = malloc(sizeof(int)*n);
  60. estrutura->buffer = malloc(sizeof(int)*nt);
  61.  
  62. /* Inicializações */
  63.  
  64. pthread_mutex_init(estrutura->mutex,NULL);
  65. pthread_cond_init(estrutura->cond,NULL);
  66. pthread_cond_init(estrutura->produzir,NULL);
  67. pthread_cond_init(estrutura->consumir,NULL);
  68. pthread_attr_init(attr);
  69. pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM); /* The thread competes for resources with all other threads in all processes on the system */
  70. soma_global=0;
  71. estrutura->numero_thread=0;
  72. int items_buffer=0;
  73.  
  74. /* Filling arrays with numbers */
  75.  
  76. for(a=0;a<n;++a){
  77. estrutura->matriz[a]=a+1;}
  78.  
  79.  
  80. /*Preparing to spawn threads. Mutex + condition variable are used
  81. to ensure that the thread is spawned and initialized before
  82. altering the thread structure variables for the next thread */
  83.  
  84. /* Lock the mutex to use pthread_cond_wait */
  85.  
  86. pthread_mutex_lock(estrutura->mutex);
  87.  
  88. /* Compute the number of elements to calculate for each thread and
  89. initialize the number to 0 */
  90.  
  91. if(nt<nbloco){
  92. nbloco = nbloco + ((n/nt)-nbloco);}
  93.  
  94. if(nt==nbloco && nbloco==n){
  95. nbloco = 1;}
  96.  
  97. /* Spawn threads */
  98.  
  99. for(i=0;i<nt_somadoras;++i){
  100. pthread_create(&somadoras[i],NULL,somadora,estrutura);}
  101.  
  102. for(z=0;z<nt;++z){
  103. pthread_create(&calculadoras[z],NULL,calculadora,estrutura);
  104. ++estrutura->numero_thread; /* Setting the thread number, start and count */
  105.  
  106. /* Here we deal with the situation when the array size is not a multiple of number of threads */
  107.  
  108. if(z==(nt-1) && n%nt){
  109. nbloco = n-nbloco*z;}
  110.  
  111. estrutura->inicio = z*nbloco;
  112.  
  113. pthread_cond_wait(estrutura->cond,estrutura->mutex);
  114.  
  115. }
  116.  
  117. /*Unlock the mutex, it will be used by the threads to add their partial sums to the sum */
  118.  
  119. pthread_mutex_unlock(estrutura->mutex);
  120.  
  121. /* Wait until the threads are done. */
  122. for(x=0;x<nt;++x){
  123. pthread_join(calculadoras[x],NULL);}
  124.  
  125. for(y=0;y<nt_somadoras;++y){
  126. pthread_join(somadoras[z],NULL);}
  127.  
  128.  
  129.  
  130. /* Print the result */
  131. printf("The sum of numbers from 1 to %d is %dn",n,soma_global);
  132.  
  133. free(estrutura->matriz);
  134. pthread_cond_destroy(estrutura->cond);
  135. free(estrutura->cond);
  136. pthread_cond_destroy(estrutura->produzir);
  137. free(estrutura->produzir);
  138. pthread_cond_destroy(estrutura->consumir);
  139. free(estrutura->consumir);
  140. pthread_attr_destroy(attr);
  141. free(attr);
  142. pthread_mutex_destroy(estrutura->mutex);
  143. free(estrutura->mutex);
  144. free(calculadoras);
  145. free(somadoras);
  146. free(estrutura);
  147.  
  148. return 0;
  149. }
  150.  
  151. void * calculadora(void * estrutura_calculadora){
  152.  
  153. int a, soma_parcial;
  154.  
  155. estrutura_global * const estrutura = estrutura_calculadora;
  156.  
  157. pthread_mutex_lock(estrutura->mutex);
  158.  
  159. int * const matriz = estrutura->matriz;
  160. int * const buffer = estrutura->buffer;
  161. int const numero_thread = estrutura->numero_thread;
  162. int const inicio = estrutura->inicio;
  163. pthread_cond_signal(estrutura->cond);
  164.  
  165. pthread_mutex_unlock(estrutura->mutex);
  166.  
  167. int const fim = nbloco + inicio;
  168.  
  169. for(a=inicio;a<fim;++a){
  170.  
  171. if(items_buffer == nt) { // full
  172. pthread_cond_wait(estrutura->produzir, estrutura->mutex); // wait until some elements are consumed
  173. }
  174.  
  175. soma_parcial= matriz[a]*matriz[a];
  176. buffer[items_buffer]=soma_parcial;
  177. printf("Sou a tarefa %d a minha soma parcial é %d e o buffer é %dn", numero_thread, soma_parcial, items_buffer);
  178.  
  179. pthread_cond_signal(estrutura->consumir);
  180. }
  181.  
  182. pthread_exit(NULL);
  183.  
  184. }
  185.  
  186. void * somadora(void * estrutura_somadora){
  187.  
  188. int c, b;
  189.  
  190. estrutura_global * const estrutura = estrutura_somadora;
  191.  
  192. pthread_mutex_lock(estrutura->mutex);
  193.  
  194. int * const buffer = estrutura->buffer;
  195.  
  196. pthread_cond_signal(estrutura->cond);
  197.  
  198. pthread_mutex_unlock(estrutura->mutex);
  199.  
  200. for(c=0;c<=n;++c){
  201.  
  202. pthread_mutex_lock(estrutura->mutex);
  203. if(items_buffer == 0) { // empty
  204. pthread_cond_wait(estrutura->consumir, estrutura->mutex); // wait for new items to be appended to the buffer
  205. }
  206.  
  207. for(b=0;b<=items_buffer;++b){
  208. soma_global+=buffer[c];
  209. printf("Sou a tarefa somadora a soma global é %dn",soma_global);
  210. }
  211. pthread_cond_signal(estrutura->produzir);
  212. pthread_mutex_unlock(estrutura->mutex);
  213. }
  214. pthread_exit(NULL);
  215.  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement